//
//  TSChatInputFullScreenVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/2/14.
//

import UIKit

class TSChatInputFullScreenVC: TSBaseVC, UITextViewDelegate {
    
    var sendComplete:(([Any])->Void)?
    var closeComplete:((String)->Void)?
    var text:String?
    
    lazy var InputBarView:UIView = {
        let InputBarView = UIView()
        InputBarView.backgroundColor = "#222222".uiColor
        InputBarView.cornerRadius = 16.0
        return InputBarView
    }()
    
    lazy var sendBtn: UIButton = {
        let sendBtn = UIButton.createButton(image: UIImage(named: "chat_send")) { [weak self]  in
            guard let self = self else { return }
            if let string = textView.text {
                sendComplete?([string])
            }
            textView.text = ""
        }
        return sendBtn
    }()
    
    lazy var minificationBtn: UIButton = {
        let minificationBtn = UIButton.createButton(image: UIImage(named: "chat_send_minify")) { [weak self]  in
            guard let self = self else { return }
            closeComplete?(textView.text)
            self.dismiss(animated: true)
        }
        return minificationBtn
    }()
    
    private let minHeight: CGFloat = 24
    private let maxHeight: CGFloat = 154
    lazy var textView: UITextView = {
        let textView = UITextView()
        textView.backgroundColor = .clear
        textView.textColor = .white
        textView.delegate = self
        textView.font = .font(size: 16)
        textView.clipsToBounds = true
        textView.isScrollEnabled = true
        textView.tintColor = .themeColor
        textView.textContainerInset = UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 0)
        return textView
    }()

  
    override func createView() {
        setNavBarViewHidden(true)
        edgesForExtendedLayout = []
        
        contentView.addSubview(InputBarView)
        InputBarView.addSubview(textView)
        InputBarView.addSubview(minificationBtn)
        InputBarView.addSubview(sendBtn)
        
        InputBarView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalTo(12+k_Height_NavBar)
            make.bottom.equalTo(-12-k_Height_safeAreaInsetsBottom())
        }
        
        textView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-60)
            make.top.equalTo(16)
            make.bottom.equalTo(-16)
            make.height.equalTo(minHeight)
        }
        
        sendBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.bottom.equalTo(-16)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        minificationBtn.snp.makeConstraints { make in
            make.trailing.equalTo(-16)
            make.top.equalTo(16)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }

    }
    
    
    override func dealThings() {
        // 监听文本变化事件
//        NotificationCenter.default.addObserver(self, selector: #selector(textDidChange), name: UITextView.textDidChangeNotification, object: textView)
        textView.text = text
    }
//
//
//    @objc private func textDidChange() {
//        // 计算输入框的内容高度
//        let sizeToFit = CGSize(width: textView.bounds.width, height: .greatestFiniteMagnitude)
//        let estimatedSize = textView.sizeThatFits(sizeToFit)
//
//        // 根据内容高度调整输入框的高度
//        var newHeight = estimatedSize.height
//        if newHeight < minHeight {
//            newHeight = minHeight
//        } else if newHeight > maxHeight {
//            newHeight = maxHeight
//            textView.isScrollEnabled = true
//        } else {
//            textView.isScrollEnabled = false
//        }
//
//        // 更新输入框的高度
//        textView.snp.updateConstraints { make in
//            make.height.equalTo(newHeight)
//        }
//    }

    deinit {
        // 移除通知监听
        NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: textView)
    }
    

}
