//
//  TSPlogUploadPhotoVC.swift
//  KittensTravelNotes
//
//  Created by 100Years on 2025/7/7.
//

import IQKeyboardManagerSwift
class TSPlogUploadPhotoVC: TSBaseVC {
    var generatorStyle:TSGeneratorStyle
    var dataModel:Any?
    init(upLoadImage:UIImage?,generatorStyle:TSGeneratorStyle,dataModel:Any? = nil) {
        self.upLoadImage = upLoadImage
        self.generatorStyle = generatorStyle
        self.dataModel = dataModel
        super.init()
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //###### UI ######
    lazy var cusStackView: TSCustomStackView = TSCustomStackView()

    //上传区域
    lazy var uploadImageView: UIImageView = creatUploadImageView()
    lazy var uploadImageBgView: UIView = creatUploadImageBgView()

    //提交 btn
    lazy var submitBtn: UIButton = creatSubmitBtn()
    //删除按钮
    lazy var deleteBtn: UIButton = creatDeleteBtn()
    lazy var topView: TSPlogTravelTopView = TSPlogTravelTopView()
    lazy var placesView: TSPlogTravelPlacesView = creatPlacesView()


    //###### 数据区 ######
    var additionalPrompt = ""
    var uploadImageViewMaxHeight:CGFloat = k_ScreenHeight-76-k_Height_safeAreaInsetsBottom()-k_Nav_Height
    let lineSpacing = 6.0
    lazy var photoPickerManager: TSPhotoPickerManager = TSPhotoPickerManager(viewController: self)
    var hintVC:TSPlogUploadHintVC = TSPlogUploadHintVC()
    var upLoadImage:UIImage? = nil {
        didSet{
            if let image = upLoadImage {
                uploadImageView.isHidden = false
                uploadImageView.image = image
                
                deleteBtn.isHidden = false
            }else {
                uploadImageView.image = nil
                uploadImageView.isHidden = true
                
                deleteBtn.isHidden = true
            }
            
            checkBtnEnabled()
        }
    }


    
    override func createView() {
        addGradientColorTop(height: 200,alpha: 0.8)
        setPageTitle("")
        
        IQKeyboardManager.shared.isEnabled = 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.trailing.equalToSuperview()
            make.bottom.equalTo(0)
//            make.bottom.equalTo(submitBtn.snp.top).offset(-16)
        }
        
        contentView.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(250*kDesignScale)
            make.height.equalTo(56)
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom())
        }
        
        setUpStackView()
        
        if let generateModel = dataModel as? TSGenerateModel{
            additionalPrompt = generateModel.inputText
            placesView.textFiled.text = additionalPrompt
            topView.setTitle(string: additionalPrompt)
        }
        
        let image = upLoadImage
        upLoadImage = image
    }

    
    override func dealThings() {
        NotificationCenter.default.addObserver(self, selector: #selector(updateVipView), name: .kPurchaseDidChanged, object: nil)
        updateVipView()
    
    }
    
    @objc func updateVipView() {
        kMainAsync() {
//            kSetBtnVipIcon(btn: self.submitBtn, show: self.generatorStyle == .creatVideo ? true: kPurchaseBusiness.generateVipShow(type: .general))
            kSetBtnVipIcon(btn: self.submitBtn, show: kPurchaseBusiness.generateVipShow(type: .general))
        }
    }
    
    func setUpStackView(){
//        if generatorStyle == .creatVideo {
//            setUpVideoUploadView()
//        }else{
        
        cusStackView.addSubviewToStack(topView,length: 113)
        cusStackView.addSpacing(length: 14)
        cusStackView.addSubviewToStackWhiteBoard(uploadImageBgView)
        uploadImageBgView.snp.remakeConstraints { make in
            make.width.equalTo(343*kDesignScale)
            make.height.equalTo(343*kDesignScale)
            make.centerX.top.bottom.equalToSuperview()
        }
        cusStackView.addSubviewToStack(placesView)
    }
    
    func checkBtnEnabled() {
        if upLoadImage == nil || additionalPrompt.replacingOccurrences(of: " ", with: "").isEmpty{
            submitBtn.isEnabled = false
        }else{
            submitBtn.isEnabled = true
        }
    }
    
    func generateImage() {
        
//        if generatorStyle == .creatVideo {
//            if kJudgeVip(externalBool: true, vc: self){ return } //判断 vip
//        }else{
            if kPurchaseBusiness.kJudgeVipFreeType(vipFreeNumType: .general){ return }//判断 vip
//        }
        
        guard let upLoadImage = upLoadImage else { return }
        let generatorModel = TSAIListPhotoGeneratorModel(upLoadImage: upLoadImage, generatorStyle: generatorStyle,additionalPrompt: additionalPrompt)

        if let generateModel = dataModel as? TSGenerateModel
        {
            generatorModel.prompt = generateModel.prompt + additionalPrompt
            generatorModel.model = generateModel.model
        }else{
            generatorModel.prompt = catPrompt + additionalPrompt
            generatorModel.model = kie
        }
        generatorModel.inputText = additionalPrompt
        let gennerateVC = TSAIPhotoGeneratorVC(generatorModel:generatorModel){ [weak self] model in
            guard let self = self else { return }
            updateVipView()
        }
        
        gennerateVC.backstageBlock = { [weak self]  in
            guard let self = self else { return }
            TSRecollectionVC.showPosition()
            self.navigationController?.popToRootViewController(animated: false)
        }
        kPresentModalVC(target: self, modelVC: gennerateVC,transitionStyle: .crossDissolve)
    }
}

extension TSPlogUploadPhotoVC {

    @objc func clickView() {
        self.view.endEditing(true)
    }
    
    @objc func clickBgView() {
        clickView()
        pickSinglePhoto{ [weak self] image in
            guard let self = self else { return }
            upLoadImage = image
        }
    }
}
