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

class TSTextGeneralPicVC: TSBaseVC {
    
    var viewH:CGFloat{
        get {
            cusStackView.viewH
        }
    }
    
    var selectedIndex:((Int)->Void)?
    
    lazy var viewModel: TSTextGeneralPicVM = {
        let viewModel:TSTextGeneralPicVM = TSTextGeneralPicVM()
        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: kRandomTextToPicArray) { [weak self] text in
            guard let self = self else { return }
            viewModel.promptText = text
        }
        return promptTextView
    }()
    
    //###################################### 图片类型 ######################################
    lazy var photoStyleView: TSTGPPhotoStyleView = {
        let photoStyleView = TSTGPPhotoStyleView()
        photoStyleView.selectedValueBlock = { [weak self] index in
            guard let self = self else { return }
            viewModel.gennerateType = TSGennerateType(rawValue: index) ?? .poster
            creatBtnView.vipFreeNumType = vipFreeNumType
            selectedIndex?(index)
        }
        photoStyleView.moreInfoBtn.addTarget(self, action: #selector(clickMoreInfoBtn), for: .touchUpInside)
        return photoStyleView
    }()
    
    //###################################### Prompt类型 ######################################
    lazy var promptStyleView: TSPromptStyleView = {
        let promptStyleView = TSPromptStyleView()
        promptStyleView.titleView.titleLab.text = "Style".localized
        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: .posetr) { [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(photoStyleView)
        photoStyleView.snp.makeConstraints { make in
            make.height.equalTo(photoStyleView.viewH)
        }
        
        cusStackView.addSubviewToStack(promptStyleView)

        cusStackView.addSubviewToStack(creatBtnView)
        creatBtnView.snp.makeConstraints { make in
            make.height.equalTo(creatBtnView.viewH)
        }
        
        cusStackView.addSubviewToStack(historyTitleView)
        historyTitleView.snp.makeConstraints { make in
            make.height.equalTo(historyTitleView.viewH)
        }
    }
    
    override func dealThings() {

    }
    
    func updateVipView() {
        
    }
    
    @objc func clickView() {
        view.endEditing(true)
    }

    @objc func clickMoreInfoBtn() {
        kPresentModalVC(target: self, modelVC: TSTutorialPopupVC(selectedIndex: tutorialPopupSelectedIndex),transitionStyle: .crossDissolve)
    }

}

extension TSTextGeneralPicVC {
    
    var vipFreeNumType:VipFreeNumType {
        var vipFreeNumType:VipFreeNumType = .posetr
        if viewModel.gennerateType == .poster {
            vipFreeNumType = VipFreeNumType.posetr
        }else if viewModel.gennerateType == .photo {
            vipFreeNumType = VipFreeNumType.photo
        }
        return vipFreeNumType
    }
    
    var tutorialPopupSelectedIndex:Int {
        var selectedIndex = 0
        if viewModel.gennerateType == .photo {
            selectedIndex = 1
        }
        return selectedIndex
    }
}
extension TSTextGeneralPicVC {
    func generateImage() {

        let gennerateVC = TSGeneralPicVC(aiText: viewModel.prompt, gennerateType: viewModel.gennerateType)
        {[weak self] model in
            guard let self = self else { return }
            model.request.promptSort = viewModel.promptText
            viewModel.saveModel(model:model)
            NotificationCenter.default.post(name: .kReloadUIData, object: nil, userInfo: ["TSGennerateType": viewModel.gennerateType])
        }
        
        kPresentModalVC(target: self, modelVC: gennerateVC,transitionStyle: .crossDissolve)
    }

}
