//
//  TSTGPTextView.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/4.
//


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 titleView: TSTGPTitleView = {
        let titleView = TSTGPTitleView()
        titleView.titleLab.text = "Enter a prompt".localized
        return titleView
    }()
  
    lazy var randomTextPicker: TSRandomTextPicker = {
        let textPicker = TSRandomTextPicker(texts: randomTextArray)
        return textPicker
    }()
    
    lazy var textBgView: UIView = {
        let textBgView = UIView()
        
        let bgImageView = UIImageView.createImageView(imageName: "textView_bg",contentMode: .scaleToFill)
        textBgView.addSubview(bgImageView)
        bgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.width.equalTo(343*kDesignScale)
            make.height.equalTo(160*kDesignScale)
        }

        return textBgView
    }()
    
    lazy var customTextView: TSPlaceholderTextView = {
        let customTextView = TSPlaceholderTextView(
            placeholder: "Type your idea here.",
            text: "",
            font: .font(size: 16),
            textColor: .white,
            backgroundColor: .clear
        )
        customTextView.delegate = self
        customTextView.returnKeyType = .done
        return customTextView
    }()
    
 
    lazy var inspirationBtn: UIButton = {
        let inspirationBtn = UIButton.createButton(
            title: "Hint Inspiration".localized,
            image: UIImage(named: "textView_hint"),
            font: .font(size: 14),
            titleColor: .themeColor
        )
        { [weak self]  in
            guard let self = self else { return }
            customTextView.text = randomTextPicker.getRandomText()
            textViewDidChange(customTextView)
        }
        inspirationBtn.contentEdgeInsets = UIEdgeInsets(top: 4, left: 40, bottom: 4, right: 50)
        inspirationBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)
        return inspirationBtn
    }()
    
    
    lazy var clearBtn: TSUIExpandedTouchButton = {
        let clearBtn = TSUIExpandedTouchButton()
        clearBtn.setUpButton(image: UIImage(named: "clear_text"))
        { [weak self]  in
            guard let self = self else { return }
            customTextView.text = ""
            textViewDidChange(customTextView)
        }
        clearBtn.setImage(UIImage(named: "clear_gary_text"), for: .disabled)
        clearBtn.isEnabled = false
        return clearBtn
    }()
    
    let maxNum:Int = 1000
    var currenMun:Int = 0 {
        didSet{
            currenMunLab.text = "\(currenMun)"
        }
    }
    lazy var currenMunLab: UILabel = {
        let numLab = UILabel.createLabel(text: "\(currenMun)",font: .font(name: .PingFangSC,size: 12),textColor: .white)
        return numLab
    }()
    
    lazy var numView: UIView = {
        let numView = UIView()
        
        numView.addSubview(currenMunLab)
        
        let numLab = UILabel.createLabel(text: "/\(maxNum)",font: .font(name: .PingFangSC,size: 12),textColor: .white.withAlphaComponent(0.4))
        numView.addSubview(numLab)
        
        let lineView = UIView()
        lineView.cornerRadius = 0.5
        lineView.backgroundColor = .white
        numView.addSubview(lineView)
        
        currenMunLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(0)
        }
        
        numLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(currenMunLab.snp.trailing)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.trailing.bottom.equalToSuperview()
            make.leading.equalTo(numLab.snp.trailing).offset(8)
            make.width.equalTo(1)
            make.height.equalTo(12)
        }
        
        return numView
    }()
    
    override func creatUI() {
        let titleViewH = titleView.viewH
        
        addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.trailing.equalTo(0)
            make.height.equalTo(titleViewH)
        }
        
        contentView.snp.updateConstraints { make in
            make.top.equalTo(titleViewH)
        }
    
        contentView.addSubview(textBgView)
        textBgView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.height.equalTo(160.0*kDesignScale)
            make.bottom.equalTo(-10)
        }
   
        textBgView.addSubview(customTextView)
        customTextView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.leading.equalTo(16)
            make.trailing.equalTo(-16)
            make.bottom.equalTo(-48)
        }
        
        textBgView.addSubview(inspirationBtn)

        inspirationBtn.snp.makeConstraints { make in
            make.height.equalTo(32)
            make.bottom.equalTo(0)
            make.leading.equalTo(0)
        }
        
        textBgView.addSubview(clearBtn)
        clearBtn.snp.makeConstraints { make in
            make.width.height.equalTo(14)
            make.bottom.equalTo(-14)
            make.trailing.equalTo(-14)
        }
        
        textBgView.addSubview(numView)
        numView.snp.makeConstraints { make in
            make.centerY.equalTo(clearBtn)
            make.trailing.equalTo(clearBtn.snp.leading).offset(-8)
        }
    
    }

    func getVipText()->String{
        return "Generate"
    }
    
}

extension TSPromptTextView: UITextViewDelegate{

    func textViewDidChange(_ textView: UITextView) {
        currenMun = textView.text.count
        clearBtn.isEnabled = currenMun > 0
        textChangedBlock(textView.text)
    }
    
    // 监听键盘的 Return 键（Done 键）
     func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
         if text == "\n" { // 检测到 Return 键
             textView.resignFirstResponder() // 收起键盘
             return false // 阻止换行
         }
         return true
     }
    
}
