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

class TSGenmojiGennerateCell : TSBaseCollectionCell{
    lazy var robotImageView: UIImageView = {
        let robotImageView = UIImageView.createImageView(imageName: "genmoji_robot")
        return robotImageView
    }()
    
    lazy var customTextView: TSCustomTextView = {
        let customTextView = TSCustomTextView(
            placeholder: "Type your idea here.",
            text: "",
            font: .font(size: 12),
            textColor: .white,
            backgroundColor: "#333333".uiColor,
            textInsets: UIEdgeInsets(top: 21, left: 16, bottom: 21, right: 16)
        )
        customTextView.delegate = self
        customTextView.layer.cornerRadius = 12
        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
    }()
    
    
    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(customTextView)
        customTextView.snp.makeConstraints { make in
            make.top.equalTo(57)
            make.leading.equalTo(0)
            make.trailing.equalTo(0)
            make.height.equalTo(182.0*kDesignScale)
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(customTextView.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)
        submitBtn.setTitle(getVipText(), for: .normal)
    }
}

extension TSGenmojiGennerateCell: UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {
        submitBtn.isEnabled = textView.text.replacingOccurrences(of: " ", with: "") .count > 0
    }
    
    // 实现 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
    }
}
