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

extension TSGeneralPicVC {
    
    var netWorkImageTop:CGFloat{
        if gennerateType == .photo{
            return 132
        }
        return 98
    }

    var netWorkImageSize:CGSize{
        if gennerateType == .photo{
            return CGSizeMake(200, 200)
        }
        return CGSizeMake(140, 272)
    }
    
    var netWorkImageCorner:CGFloat{
        if gennerateType == .photo{
            return 100.0
        }
        return 12.0
    }

    var resultIcon:UIImage?{
        if gennerateType == .photo{
            return UIImage(named: "theme_icon_photo")
        }
        return UIImage(named: "theme_icon_poster")
    }
    
    var saveBtnText:String{
        if gennerateType == .photo{
            return "Set Now".localized
        }
        return "Download".localized
    }
    
    var shadowColor:UIColor{
        if gennerateType == .photo{
            return "#67B4F1".uiColor
        }
        return "#5FE4B5".uiColor
    }
    
    func getSuccessImage()->UIImage?{
        if let image = netWorkImageView.image {
            return image.pngImage
        }
        return nil
    }
    
    
    var vipFreeNumType:VipFreeNumType{
        if gennerateType == .photo{
            return .photo
        }
        return .posetr
    }
}
extension TSGeneralPicVC {
    func upDateView(state:TSProgressState,model:TSActionInfoModel?){
        switch state {
            case .failed(let errorStr):
                showError(text: errorStr)
            case .success:
                if let model = model {
                    showSuccess(model: model)
                }else{
                    showError(text: nil)
                }
            case .progressString(let string):
                showProgress(text: string)
            default:
                showLoading()
        }
    }
    
    func showProgress(text:String) {
        generateInView.isHidden = false
        generateInView.showLoading(text: text)
        generateInView.isRotating = true
        
        bottomView.isHidden = true
        
        isClickTheBlankClosePage = false
    }
    
    func showLoading(){
        generateInView.isHidden = false
        generateInView.showLoading(text: "Generating...".localized)
        generateInView.isRotating = true
        
        bottomView.isHidden = true
        
        isClickTheBlankClosePage = false
    }
    
    func showError(text:String?){
        let msg = "Failed to Generate, please try later".localized
        generateInView.isHidden = true
        generateInView.isRotating = false
    
        bottomView.isHidden = false
   
        againBtn.isHidden = false//重试
        failView.isHidden = false
//        failLabel.text = msg
        saveBtn.isHidden = true
        successTopView.isHidden = true
        
        netWorkImageView.isHidden = true
        regenerateBtn.isHidden = true
        
        isClickTheBlankClosePage = true
    }
    
    func showSuccess(model:TSActionInfoModel){
        generateInView.isHidden = true
        generateInView.isRotating = false
        
        bottomView.isHidden = false
        
        againBtn.isHidden = true//重试
        failView.isHidden = true
        saveBtn.isHidden = false
        successTopView.isHidden = false

        imageModel = model
        isClickTheBlankClosePage = true
        
        netWorkImageView.isHidden = false
        regenerateBtn.isHidden = false
        self.netWorkImageView.setAsyncImage(urlString: model.response.resultUrl,placeholder:kPlaceholderImage,contentMode: .scaleAspectFill){ [weak self] image in
            guard let self = self else { return }
        }
        
        kPurchaseToolShared.useOnceForFree(type: vipFreeNumType)
        
        if let model = imageModel {
            complete(model)
        }
    }
}


