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

import UIKit

class TSChatInputBarVC: TSBaseVC, UITextViewDelegate {
    
    var sendComplete:(([Any])->Void)?
    
    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 magnifyBtn: UIButton = {
        let magnifyBtn = UIButton.createButton(image: UIImage(named: "chat_send_magnify")) { [weak self]  in
            guard let self = self else { return }
            let vc = TSChatInputFullScreenVC()
            vc.text = textView.text
            vc.sendComplete = { [weak self] date in
                guard let self = self else { return }
                sendComplete?(date)
            }
            
            vc.closeComplete = { [weak self] text in
                guard let self = self else { return }
                textView.text = text
                scrollTextViewToBottom()
            }
            
            kPresentModalVC(target: self, modelVC: vc)
        }
        magnifyBtn.isHidden = true
        return magnifyBtn
    }()
    
    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 = false
        textView.tintColor = .themeColor
        textView.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        return textView
    }()

  
    override func createView() {
        setNavBarViewHidden(true)
        
        view.backgroundColor = .clear
        
        contentView.addSubview(InputBarView)
        InputBarView.addSubview(textView)
        InputBarView.addSubview(magnifyBtn)
        InputBarView.addSubview(sendBtn)
        
        InputBarView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.top.equalTo(12)
            make.bottom.equalTo(-12)
        }
        
        textView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-60)
            make.top.equalTo(18)
            make.bottom.equalTo(-14)
            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)
        }
        
        magnifyBtn.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)
    }

    
    func sendEnabled(enabled:Bool){
        sendBtn.isEnabled = enabled
    }

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


extension TSChatInputBarVC {

    @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
            magnifyBtn.isHidden = false
        } else {
            textView.isScrollEnabled = false
            magnifyBtn.isHidden = true
        }

        // 更新输入框的高度
        textView.snp.updateConstraints { make in
            make.height.equalTo(newHeight)
        }
    }

    private func scrollTextViewToBottom() {
        let bottomOffset = CGPoint(x: 0, y: textView.contentSize.height - textView.bounds.size.height)
        textView.setContentOffset(bottomOffset, animated: true)
    }
    
}
