//
//  TSPromptTextView.swift
//  AIEmoji
//
//  Created by 100Years on 2025/3/11.
//

class TSPromptTextView : TSBaseView{
    
    var randomTextArray:[String]
    var textChangedBlock:((String)->Void)
    
    init(randomTextArray: [String], textChangedBlock: @escaping (String) -> Void) {
        self.randomTextArray = randomTextArray
        self.textChangedBlock = textChangedBlock
        super.init(frame: .zero)
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    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: TSPlaceholderTextView = {
        let customTextView = TSPlaceholderTextView(
            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 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 = false
        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 = ""
            textViewDidChange(customTextView)
        }
        clearBtn.isHidden = true
        return clearBtn
    }()
    
    override func creatUI() {
        
        contentView.addSubview(robotImageView)
        robotImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(16)
            make.width.height.equalTo(82)
        }
        
        contentView.addSubview(textBgView)
        textBgView.snp.makeConstraints { make in
            make.top.equalTo(57)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.height.equalTo(182.0*kDesignScale)
//            make.bottom.equalTo(-20)
            make.bottom.equalTo(0)
        }
   
        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)
        }

    }

    func getVipText()->String{
        if kPurchaseDefault.isVip {
            return "Generate"
        }
        return "Generate (\(kPurchaseDefault.freeNum(type: .generatePic)))"
    }
    
}

extension TSPromptTextView: UITextViewDelegate{
    
//    func textViewDidBeginEditing(_ textView: UITextView) {
//        self.colComponent?.collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredVertically, animated: true)
//    }
    
    func textViewDidChange(_ textView: UITextView) {
        let text = textView.text.replacingOccurrences(of: " ", with: "")
        clearBtn.isHidden = text.count > 0 ? false : true
        textChangedBlock(textView.text)
    }
    
}
