//
//  TSGenmojiGennerateCell.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//

class TSGenmojiGennerateCell : TSBaseCollectionCell{
    lazy var robotImageView: UIImageView = {
        let robotImageView = UIImageView.createImageView(imageName: "aichat_avatar")
        return robotImageView
    }()
    
    
    lazy var randomTextPicker: TSRandomTextPicker = {
        let textPicker = TSRandomTextPicker(texts: kRandomTextArray)
        return textPicker
    }()
    
    lazy var textBgView: UIView = {
        let textBgView = UIView()
        textBgView.backgroundColor = "#333333".uiColor
        textBgView.cornerRadius = 12
        return textBgView
    }()
    
    lazy var customTextView: TSCustomTextView = {
        let customTextView = TSCustomTextView(
            placeholder: "Type your idea here.",
            text: "",
            font: .font(size: 14),
            textColor: .white,
            backgroundColor: "#333333".uiColor
        )
        customTextView.delegate = self
        customTextView.returnKeyType = .send
        return customTextView
    }()
    
    lazy var submitBtn: UIButton = {
        let submitBtn = kCreateNormalSubmitBtn(title: "Generate".localized) { [weak self]  in
            guard let self = self else { return }
            actionHandler(any: customTextView.text)
        }
        submitBtn.cornerRadius = 24.0
        submitBtn.isEnabled = false
        return submitBtn
    }()
    
    
    lazy var inspirationBtn: UIButton = {
        let inspirationBtn = UIButton.createButton(
            title: "Hint Inspiration".localized,
            image: UIImage(named: "inspiration_yellow"),
            backgroundColor:"#FECB34".uiColor.withAlphaComponent(0.1),
            font: .font(size: 12),
            titleColor: .themeColor,
            corner: 8.0)
        { [weak self]  in
            guard let self = self else { return }
            customTextView.text = randomTextPicker.getRandomText()
            textViewDidChange(customTextView)
        }
        inspirationBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 7, bottom: 4, right: 7)
        inspirationBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -4, bottom: 0, right: 0)
        inspirationBtn.isHidden = true
        return inspirationBtn
    }()
    
    
    lazy var clearBtn: UIButton = {
        let clearBtn = UIButton.createButton(
            image: UIImage(named: "clear_text")
        )
        { [weak self]  in
            guard let self = self else { return }
            customTextView.text = ""
        }
        clearBtn.isHidden = true
        return clearBtn
    }()
    
    override func creatUI() {
        
        contentView.addSubview(robotImageView)
        robotImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(0)
            make.width.height.equalTo(82)
        }
        
        contentView.addSubview(textBgView)
        textBgView.snp.makeConstraints { make in
            make.top.equalTo(57)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(182.0*kDesignScale)
        }
   
        textBgView.addSubview(customTextView)
        customTextView.snp.makeConstraints { make in
            make.top.equalTo(21)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.bottom.equalTo(-21)
        }
        
        textBgView.addSubview(inspirationBtn)
        inspirationBtn.snp.makeConstraints { make in
            make.height.equalTo(28)
            make.bottom.equalTo(-16)
            make.leading.equalTo(16)
        }
        
        textBgView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { make in
            make.width.height.equalTo(16)
            make.bottom.equalTo(-16)
            make.trailing.equalTo(-16)
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(textBgView.snp.bottom).offset(16)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(48)
        }
    }

    func getVipText()->String{
        if kPurchaseDefault.isVip {
            return "Generate"
        }
        return "Generate (\(kPurchaseDefault.freeNum(type: .generatePic)))"
    }
    
    override func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
        super.renderView(with: object, component: component, attributes: attributes)
        
        var title = "Generate"
        if let itemModel = object as? TSGenmojiCoLItemModel{
            if itemModel.style == .generate{
                title = "Generate (\(kPurchaseDefault.freeNum(type: .generatePic)))"
                inspirationBtn.isHidden = true
                customTextView.snp.updateConstraints { make in
                    make.bottom.equalTo(-31)
                }
            }else if itemModel.style == .textPicGenerate{
                title = "Generate (\(kPurchaseDefault.freeNum(type: .textGeneratePic)))"
                inspirationBtn.isHidden = false
                customTextView.snp.updateConstraints { make in
                    make.bottom.equalTo(-50)
                }
                
            }
        }
        
        if kPurchaseDefault.isVip == true {
            title = "Generate"
        }
        submitBtn.setTitle(title, for: .normal)
        
        
        
        

    }
}

extension TSGenmojiGennerateCell: UITextViewDelegate{
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        self.colComponent?.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredVertically, animated: true)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        submitBtn.isEnabled = textView.text.replacingOccurrences(of: " ", with: "") .count > 0
        clearBtn.isHidden = !submitBtn.isEnabled
    }
    
    // 实现 UITextViewDelegate 协议方法，控制 return 键行为
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            // 当输入为换行符（即按下 return 键）时，执行相应操作
            if submitBtn.isEnabled {
                actionHandler(any: customTextView.text)
                textView.text = ""
                textView.resignFirstResponder()
            }
            return false
        }
        return true
    }
}
