//
//  TSAIThinkingView.swift
//  AIEmoji
//
//  Created by 100Years on 2025/3/12.
//

class TSAIThinkingView: TSChatMsgBaseView {
 
    var isFold:Bool = false {
        didSet{
            self.textBgView.isHidden = isFold
            arrowImageView.image = UIImage(named: isFold ? "chat_down_arrow" : "chat_up_arrow")
            if isFold{
                textBgView.snp.removeConstraints()
            }else{
                textBgView.snp.remakeConstraints { make in
        //            make.top.equalTo(topView.snp.bottom).offset(12)
                    make.top.equalTo(40)
                    make.leading.equalTo(16)
                    make.trailing.equalTo(-32)
                    make.bottom.equalTo(-12)
                }
            }
         
            updateViewH()
        }
    }
    lazy var aiIconImageView: UIImageView = {
        let iconImageView = UIImageView.createImageView(imageName: "deepseek_icon")
        return iconImageView
    }()
    
    lazy var aiInfoLabel: UILabel = {
        let aiInfoLabel = UILabel.createLabel(font: .font(size: 14), textColor: "#4D6BFE".uiColor, textAlignment: .left, numberOfLines: 1)
        return aiInfoLabel
    }()
    
    lazy var arrowImageView: UIImageView = {
        let arrowImageView = UIImageView.createImageView(imageName: "chat_up_arrow")
        return arrowImageView
    }()
    lazy var topView: UIView = {
        let topView = UIView()
        topView.backgroundColor = "#4D6BFE".uiColor.withAlphaComponent(0.1)
        topView.cornerRadius = 14
        topView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickTopView)))
        topView.addSubview(aiIconImageView)
        aiIconImageView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        topView.addSubview(aiInfoLabel)
        aiInfoLabel.snp.makeConstraints { make in
            make.leading.equalTo(aiIconImageView.snp.trailing).offset(4)
            make.centerY.equalToSuperview()
        }

        topView.addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { make in
            make.leading.equalTo(aiInfoLabel.snp.trailing).offset(8)
            make.trailing.equalTo(-12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(16)
        }
        
        return topView
    }()
    
    
    
    lazy var textLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.numberOfLines = 0
        textLabel.font = .font(size: 14)
        textLabel.textColor = .white.withAlphaComponent(0.5)
        return textLabel
    }()
    
    lazy var textBgView: UIView = {
        let textBgView = UIView()
        
        let lineView = UIView()
        lineView.backgroundColor = "#D8D8D8".uiColor.withAlphaComponent(0.2)
        textBgView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.leading.equalTo(0)
            make.bottom.equalTo(4)
            make.top.equalTo(-4)
            make.width.equalTo(1)
        }
        
        textBgView.addSubview(textLabel)
        textLabel.snp.makeConstraints { make in
            make.trailing.top.equalTo(0)
            make.leading.equalTo(12)
        }
        
        return textBgView
    }()
    
    override func creatUI() {
        
        contentView.addSubview(topView)
        contentView.addSubview(textBgView)
        
        topView.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.top.equalTo(0)
            make.height.equalTo(28)
        }
        
        textBgView.snp.makeConstraints { make in
//            make.top.equalTo(topView.snp.bottom).offset(12)
            make.top.equalTo(40)
            make.leading.equalTo(16)
            make.trailing.equalTo(-32)
            make.bottom.equalTo(-12)
        }
    }
    
    
    @objc func clickTopView(){
//        isFold = !isFold
//        self.uiBaseModel?.msgModel?.thinkingFold = isFold
        
        if let isFold = self.uiBaseModel?.msgModel?.thinkingModel?.thinkingFold {
            self.uiBaseModel?.msgModel?.thinkingModel?.thinkingFold = !isFold
            self.isFold = !isFold
            self.didTapCustomViewBlock?(.refreshCell)
        }
        
        
    }
    
    func setText(text:String){
        textLabel.text = text
        updateViewH()
    }
    
    
    func updateViewH(){
        let topH:CGFloat = 28.0 + 12.0
        if isFold {
            viewH = topH
            //dePrint("modelView.viewH text =\(textLabel.text)")
            //dePrint("modelView.viewH viewH =\(viewH)")
        }else {
            let size = textLabel.sizeThatFits(CGSize(width: k_ScreenWidth - 60, height: .greatestFiniteMagnitude))
            viewH = topH + size.height + 12.0
            //dePrint("modelView.viewH text1 =\(textLabel.text)")
            //dePrint("modelView.viewH viewH1 =\(viewH)")
        }
        
//        self.frame = CGRect(x: 0, y: 0, width: k_ScreenWidth - 48, height: viewH)
    }

    
    override func updataView(uiBaseModel: TSChatMessageUIBaseModel) {
        self.uiBaseModel = uiBaseModel
        guard let thinkingModel = uiBaseModel.msgModel?.thinkingModel else { return  }
        
       
        aiIconImageView.image = UIImage(named: thinkingModel.thinkingIcon)
        textLabel.text = thinkingModel.thinkingString
        aiInfoLabel.text = "\(thinkingModel.thinkingName) \(thinkingModel.thinkingState) "
        self.isFold = thinkingModel.thinkingFold
        //dePrint("modelView.viewH text =\(textLabel.text)")
    
        
     
    }
}
