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

class TSTTPInputVC: TSBaseVC {
    
    var viewH:CGFloat{
        get {
            if cusStackView.viewH > 0{
                return cusStackView.viewH
            }
            return 647.0
        }
    }
    
    var reloadUIBlock:(()->Void)?
    var jumpPTPBlock:(()->Void)?
    lazy var viewModel: TSTTPInputVM = {
        let viewModel:TSTTPInputVM = TSTTPInputVM()
        viewModel.isCanGennerateBlock = { [weak self] enabled in
            guard let self = self else { return }
            creatBtnView.setBtnEnabled(isEnabled: enabled)
        }
        return viewModel
    }()
    
    //###################################### cusStackView ######################################
    lazy var cusStackView: TSCustomStackView = {
        let cusStackView = TSCustomStackView(axis: .vertical,spacing: 8)
        return cusStackView
    }()
    
    //###################################### 输入框 ######################################
    lazy var enterView: TSPTPEnterView = {
        let enterView = TSPTPEnterView()
        enterView.bannerBtn.addTarget(self, action: #selector(clickPTPEnterView), for: .touchUpInside)
        return enterView
    }()
    
    //###################################### 输入框 ######################################
    lazy var promptTextView: TSPromptTextView = {
        let promptTextView = TSPromptTextView(randomTextArray: kRandomTextArray) { [weak self] text in
            guard let self = self else { return }
            viewModel.promptText = text
        }
        return promptTextView
    }()
    
    //###################################### Prompt类型 ######################################
    lazy var promptStyleView: TSTTPStyleView = {
        let promptStyleView = TSTTPStyleView()
        promptStyleView.dataArray = viewModel.ptpStyleModels
        promptStyleView.selectedValueBlock = { [weak self] model in
            guard let self = self else { return }
            viewModel.selectPromptModel = model
        }
        return promptStyleView
    }()
    
    
    //###################################### Button ######################################
    lazy var creatBtnView:TSGeneralBtnView  = {
        let creatBtnView = TSGeneralBtnView {[weak self] in
            guard let self = self else { return }
            generateImage()
        }
        creatBtnView.setBtnEnabled(isEnabled: false)
        return creatBtnView
    }()
    
    //###################################### Generate History ######################################
    lazy var historyTitleView: TSTitleView = {
        let historyTitleView = TSTitleView()
        historyTitleView.titleLab.text = "Generate History".localized
        return historyTitleView
    }()
    


    override func createView() {
        view.backgroundColor = .clear
        
        setNavBarViewHidden(true)
    
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickView))
        tapGesture.cancelsTouchesInView = false
        view.addGestureRecognizer(tapGesture)
        
        contentView.addSubview(cusStackView)
        cusStackView.snp.makeConstraints { make in
            make.top.leading.bottom.trailing.equalTo(0)
        }

        
        cusStackView.addSubviewToStack(enterView)
        cusStackView.addSubviewToStack(promptTextView)
        
        cusStackView.addSubviewToStack(promptStyleView)

        cusStackView.addSubviewToStack(creatBtnView)
        creatBtnView.snp.makeConstraints { make in
            make.height.equalTo(creatBtnView.viewH)
            make.width.equalTo(k_ScreenWidth)
        }
    }
    
    override func dealThings() {
     
    }
    
    @objc func clickView() {
        view.endEditing(true)
    }

    @objc func clickCollectionView() {
        view.endEditing(true)
    }
    
    @objc func clickPTPEnterView() {
        jumpPTPBlock?()
    }
    
}

extension TSTTPInputVC {
    func generateImage() {
        
        //判断 vip
        if kJudgeVip(externalBool: kPurchaseDefault.freeNumAvailable(type: .textGeneratePic) == false, vc: self) {[weak self] in
            guard let self = self else { return }
        }{ return }
        
        let gennerateVC = TSTextPicGennerateVC(aiText: viewModel.prompt) {[weak self] model in
            guard let self = self else { return }
            model.request.promptSort = viewModel.promptText
            viewModel.saveModel(model: model)
            reloadUIBlock?()
        }
        
        kPresentModalVC(target: self, modelVC: gennerateVC,transitionStyle: .crossDissolve)
    }

}
