//
//  TSTextGeneralRintoneVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/6.
//


class TSTextGeneralRintoneVC: TSBaseVC {
    
    var viewH:CGFloat{
        get {
            if cusStackView.viewH > 0{
                return cusStackView.viewH
            }
            return 455.0
        }
    }
    
    var reloadUIBlock:(()->Void)?
    
    lazy var viewModel: TSTextGeneralRintoneVM = {
        let viewModel:TSTextGeneralRintoneVM = TSTextGeneralRintoneVM()
        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 promptTextView: TSPromptTextView = {
        let promptTextView = TSPromptTextView(randomTextArray: kRandomTextToRintone) { [weak self] text in
            guard let self = self else { return }
            viewModel.promptText = text
        }
        return promptTextView
    }()
    
    //###################################### Prompt类型 ######################################
    lazy var promptStyleView: TSPromptStyleView = {
        let promptStyleView = TSPromptStyleView()
        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:TSAppBtnView  = {
        let creatBtnView = TSAppBtnView()
        creatBtnView.setUpButton(style: .generate, vipFreeNumType: .ringtones) { [weak self] in
            guard let self = self else { return }
            generateImage()
        }
        creatBtnView.setBtnEnabled(isEnabled: false)
        return creatBtnView
    }()
    
    //###################################### Generate History ######################################
    lazy var historyTitleView: TSTGPTitleView = {
        let historyTitleView = TSTGPTitleView()
        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(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)
    }
}

extension TSTextGeneralRintoneVC {
    func generateImage() {

        let gennerateVC = TSGeneralRintoneVC(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)
    }

}
