//
//  TSGenmojiGennerateVC.swift
//  AIEmoji
//
//  Created by 100Years on 2025/1/17.
//

class TSGenmojiGennerateVC: TSBottomAlertVC {
    
    var imageModel:TSGenmojiModel?
    var complete:((TSGenmojiModel)->Void)
    var aiText:String
    init(aiText: String,complete:@escaping ((TSGenmojiModel)->Void)) {
        self.aiText = aiText
        self.complete = complete
        super.init()
    }
    
    var viewModel:TSGenmojiGennerateViewModel = TSGenmojiGennerateViewModel()
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var netWorkImageView : UIImageView = {
        let netWorkImageView = UIImageView.createImageView(imageName: "",corner: 24.0)
        return netWorkImageView
    }()
    
    
    lazy var generateInView : TSCommonloadingView = {
        let generateInView = TSCommonloadingView()
        return generateInView
    }()
    
    override func createView() {
        super.createView()
        
        bottomView.addSubview(generateInView)
        generateInView.snp.makeConstraints { make in
            make.top.equalTo(140)
            make.centerX.equalToSuperview()
        }
        
        bottomView.addSubview(netWorkImageView)
        netWorkImageView.snp.makeConstraints { make in
            make.top.equalTo(80)
            make.centerX.equalToSuperview()
            make.width.equalTo(200)
            make.height.equalTo(200)
        }
        
    }
    
    override func closePage() {
        self.dismiss(animated: true, completion: nil)
    }
    
    
    
    @objc override func clickSubmitBtn(){
        viewModel.creatImageEmoji(text:aiText)
    }
    
    @objc override func clickCancelBtn(){
        if let image = getSuccessImage() {
            UIDevice.copyImage(image: image)
            if let model = imageModel {
                complete(model)
            }
            closePage()
        }
    }
    
    @objc override func clickConfirmBtn(){
        if let image = getSuccessImage() {
            PhotoManagerShared.saveImageToAlbum(image) { success, error in
                if success {
                    kSavePhotoSuccesswShared.show(atView: self.view)
                    if let model = self.imageModel {
                        self.complete(model)
                    }
                    self.closePage()
                }else{
                    debugPrint(error)
                }
            }
        }
    }
    
    
    override func dealThings() {
        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)
    }
}
extension TSGenmojiGennerateVC {
    
    
    func getSuccessImage()->UIImage?{
        if let image = netWorkImageView.image {
            return image.pngImage
        }
        return nil
    }
    
}
extension TSGenmojiGennerateVC {
    func upDateView(state:TSProgressState,model:TSGenmojiModel?){
        switch state {
            case .failed(let _):
                showError()
            case .success:
                if let model = model {
                    showSuccess(model: model)
                }else{
                    showError()
                }
            default:
                showLoading()
        }
    }
    
    func showLoading(){
        generateInView.isHidden = false
        generateInView.showLoading(text: "generate in...".localized)
        generateInView.isRotating = true
        submitBtn.isHidden = true
        cancelBtn.isHidden = true
        confirmBtn.isHidden = true
        xBtn.isHidden = true
    }
    
    func showError(){
        generateInView.isHidden = false
        generateInView.showError(text: "Generation failed".localized)
        generateInView.isRotating = false
        submitBtn.isHidden = false
        cancelBtn.isHidden = true
        confirmBtn.isHidden = true
        xBtn.isHidden = false
    }
    
    func showSuccess(model:TSGenmojiModel){
        generateInView.isHidden = true
        generateInView.isRotating = false
        submitBtn.isHidden = true
        cancelBtn.isHidden = false
        confirmBtn.isHidden = false
        xBtn.isHidden = false
        imageModel = model
        //请求成功,则加载和保存图片
        self.netWorkImageView.setAsyncImage(urlString: model.response.resultUrl)
    }
}
