//
//  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 400.0
        }
    }
    
    var reloadUIBlock:(()->Void)?
    var refreshGeneratedBlock:((Int)->Void)?
    
    lazy var viewModel: TSTextGeneralRintoneVM = {
        let viewModel:TSTextGeneralRintoneVM = TSTextGeneralRintoneVM()
        viewModel.isCanGennerateBlock = { [weak self] enabled in
            guard let self = self else { return }
            setCreatBtnEnabled()
        }
        return viewModel
    }()
    
    //###################################### cusStackView ######################################
    lazy var cusStackView: TSCustomStackView = {
        let cusStackView = TSCustomStackView(axis: .vertical,spacing: 0)
        cusStackView.scrollView.isScrollEnabled = false
        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()
        }
        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.leading.bottom.trailing.equalTo(0)
            make.top.equalTo(16)
        }

        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() {
        NotificationCenter.default.addObserver(forName: .kBaseOperationQueueCountChanged, object: nil, queue: nil) { [weak self] notification in
            guard let self = self else { return }
            setCreatBtnEnabled()
        }
    }

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

extension TSTextGeneralRintoneVC {
    func generateImage() {

        let gennerateVC = TSGeneralRintoneVC(prompt:viewModel.prompt,promptSort: viewModel.prompt)
        {[weak self] model in
            guard let self = self else { return }
            reloadUIBlock?()
        }
        
        kPresentModalVC(target: self, modelVC: gennerateVC,transitionStyle: .crossDissolve)
    }

    func setCreatBtnEnabled() {
        let isAvailability = TSGenerateRintoneOperationQueue.shared.isAvailability
        if viewModel.isCanGennerate,isAvailability {
            creatBtnView.setBtnEnabled(isEnabled: true)
            creatBtnView.loadingAnimation(loading: false)
        }else{
            creatBtnView.setBtnEnabled(isEnabled: false)
            creatBtnView.loadingAnimation(loading: !isAvailability)
        }
    }
}
