//
//  TSGeneralPicVC.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/5.
//

class TSGeneralPicVC: TSBottomAlertVC {
    
    

    var imageModel:TSActionInfoModel?
    var complete:((TSActionInfoModel)->Void)
    
    var aiText:String
    var gennerateType:TSGennerateType
    init(aiText: String,gennerateType:TSGennerateType,complete:@escaping ((TSActionInfoModel)->Void)) {
        self.aiText = aiText
        self.gennerateType = gennerateType
        self.complete = complete
        super.init()
    }
    
    lazy var viewModel: TSGenneralPicVM = {
        let viewModel:TSGenneralPicVM = TSGenneralPicVM()
        viewModel.aiText = aiText
        viewModel.gennerateType = gennerateType
        return viewModel
    }()
    
    lazy var contactsTool: TSContactsTool = {
        let contactsTool = TSContactsTool(targetVC: self)
        return contactsTool
    }()
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    lazy var netWorkImageView : UIImageView = {
        let netWorkImageView = UIImageView.createImageView(imageName: "",corner: 12.0)
        return netWorkImageView
    }()
    
    //############################## 生成动画 ##############################
    lazy var generateInView : TSCommonloadingView = {
        let generateInView = TSCommonloadingView()
        generateInView.cancelBtn.addTarget(self, action: #selector(closePage), for: .touchUpInside)
        return generateInView
    }()

    
    override func createView() {
        super.createView()
        
        view.addSubview(generateInView)
        generateInView.snp.makeConstraints { make in
            make.top.bottom.leading.trailing.equalTo(0)
        }

        diyView.addSubview(netWorkImageView)
        netWorkImageView.snp.makeConstraints { make in
            make.top.equalTo(netWorkImageTop)
            make.centerX.equalToSuperview()
            make.size.equalTo(netWorkImageSize)
        }
        
        diyView.addSubview(regenerateBtn)
        regenerateBtn.snp.makeConstraints { make in
            make.top.equalTo(netWorkImageView.snp.bottom).offset(16)
            make.centerX.equalToSuperview()
        }
        
        saveBtn.setTitle(saveBtnText, for: .normal)
        resultIconImageView.image = resultIcon
        bottomViewH = 540.0
        bgImageView.addShadow(shadowColor: shadowColor.cgColor, shadowOffset: .zero, shadowRadius: 20, shadowOpacity: 0.8)
        
        netWorkImageView.cornerRadius = netWorkImageCorner
    }
    
    @objc override func closePage() {
        viewModel.cancelAllRequest()
        self.dismiss(animated: true, completion: nil)
    }
    
    override func clickAgainBtn() {
        //判断 vip
        if kPurchaseToolShared.kJudgeVipFreeType(vipFreeNumType: vipFreeNumType, vc: self){ return }
        viewModel.creatImageEmoji(text:aiText)
    }
    
    @objc override func clickSubmitBtn(){
        if let image = getSuccessImage() {
            
            if gennerateType == .poster {
                PhotoManagerShared.saveImageToAlbum(image) { success, error in
                    if success {
                        kSavePhotoSuccesswShared.show(atView:self.view)
                    }else{
                        debugPrint(error)
                    }
                }
            }else if gennerateType == .photo {
                contactsTool.setContactsAvatar(avatarImage: image) { data, error in
                    if error == nil {
                        kSavePhotoSuccesswShared.show(atView: self.view,showViewBtn: false)
                    }
                }
            }
        }
    }

    override func dealThings() {
        //判断 vip
        if kPurchaseToolShared.kJudgeVipFreeType(vipFreeNumType: vipFreeNumType, vc: self){ return }
        viewModel.creatImageEmoji(text: self.aiText)
        viewModel.$stateDatauPblished.receive(on: DispatchQueue.main).sink {[weak self]  (state,model) in
            guard let self = self else { return }
            self.upDateView(state: state, model: model)
        }.store(in: &cancellable)
    }
}

