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


class TSAIPhotoGeneratoBaserVC: TSBaseVC {
    var deleteBlock:(()->Void)?
    var infoModel:TSActionInfoModel?{
        didSet{
            updateImageView()
        }
    }

    lazy var bottomViewH = 60+k_Height_safeAreaInsetsBottom()
    lazy var networkImageView : UIImageView = UIImageView()
//    lazy var shareBtn: TSVerticalButton = creatShareBtn()
    lazy var saveBtn: UIButton = creatSaveBtn()
    var navRightBtn:UIButton = UIButton()
    var isSavePhotoMark:Bool = false
    
    let locationLabel: UILabel = UILabel.createLabel(font: .font(name: .PoppinsBoldItalic,size: 20),textColor: .mainText,textAlignment: .center,numberOfLines: 1,adjustsFontSizeToFitWidth: true)
    
    lazy var xBtn: UIButton = {
        let xBtn = UIButton.createButton(image: .close) { [weak self]  in
            guard let self = self else { return }
            closePage()
        }
        return xBtn
    }()
    

    override func createView() {
        setNavBarViewHidden(true)
        contentView.backgroundColor = .mainBg
        setUpPhotoSomeView()
        
        let bottomBtnTop:CGFloat = -69-k_Height_safeAreaInsetsBottom()
//        contentView.addSubview(shareBtn)
//        shareBtn.snp.makeConstraints { make in
//            make.leading.equalTo(16)
//            make.width.equalTo(shareBtn.intrinsicContentSize.width)
//            make.height.equalTo(48)
//            make.bottom.equalTo(bottomBtnTop)
//        }
        
        contentView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.leading.equalTo(16)
            make.trailing.equalTo(-16.0)
            make.height.equalTo(56)
            make.bottom.equalTo(bottomBtnTop)
        }
        
        //关闭按钮
        contentView.addSubview(xBtn)
        xBtn.snp.makeConstraints { make in
            make.top.equalTo(k_Height_StatusBar + 4)
            make.leading.equalTo(16)
            make.width.equalTo(32)
            make.height.equalTo(32)
        }
    }
    
    //分享功能
    @objc func clickShare(){
        if kPurchaseBusiness.kJudgeVip(externalBool: true, vc: self){ return }
        guard let infoModel = infoModel else { return }
        if infoModel.isVideo{
//            TSDownloadManager.getDownLoadVideo(urlString: infoModel.response.resultUrl) { url, success in
//                if let url = url {
//                    kShareContent(target: self, anyData: url)
//                }
//            }
        }else{
            TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.response.resultUrl) { image in
                if let image = image {
                    kShareContent(target: self, anyData: image)
                }
            }
        }
    }
    

    //保存功能
    @objc func clickSaveBtn(){
        if kPurchaseBusiness.kJudgeVip(externalBool: true, vc: self){ return }
        guard let infoModel = infoModel else { return }
        if infoModel.isVideo{
//            TSDownloadManager.getDownLoadVideo(urlString: infoModel.response.resultUrl) { url, success in
//                if let url = url {
//                    PhotoManagerShared.saveVideoToAlbum(videoURL: url) { [weak self] success, error in
//                        guard let self = self else { return }
//                        if success {
//                            isSavePhotoMark = true
//                            kSaveSuccesswShared.show(atView:self.view)
//                        }else{
//                            debugPrint(error)
//                        }
//                    }
//                }
//            }
        }else{
            TSImageStoreTool.downloadImageWithProgress(urlString: infoModel.response.resultUrl) { image in
                if let image = image {
                    PhotoManagerShared.saveToAlbum(image) { [weak self] success, error in
                        guard let self = self else { return }
                        if success {
                            isSavePhotoMark = true
                            kSaveSuccesswShared.show(atView:self.view)
                        }else{
                            debugPrint(error)
                        }
                    }
                }
            }
        }
    }
    
    override func navBarClickLeftAction() {
        dismiss(animated: true)
    }
    
    func closePage() {
        dismiss(animated: true)
    }
    
    @objc func clickNavRight() {

        TSCustomAlertController.show(in: self, config: TSCustomAlertController.AlertConfig(
            message: "Are you sure to delete?".localized,
            cancelTitle: "Delete".localized,
            cancelColor: .red,
            confirmTitle: "Retain".localized,
            confirmColor: .white,
            cancelAction: { [weak self]  in
                guard let self = self else { return }
                self.deleteBlock?()
                self.navBarClickLeftAction()
            }
        ))
    }
}


extension TSAIPhotoGeneratoBaserVC {
    
    func updateImageView(){
        kMainAsync {
            guard let infoModel = self.infoModel else { return }
            if infoModel.isVideo {
//                self.setVideoURL()
            }else{
                self.updateNetWorkImageView()
            }
        }

    }
}
