//
//  TSGeneralRintoneVC+Event.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/6.
//

extension UIViewController {
//    func findFirstTransitionView() -> UIView? {
//        var currentView = self.view
//        while currentView != nil {// 判断当前视图是否是 UITransitionView
//            if String(describing: type(of: currentView!)) == "UITransitionView" {
//                return currentView
//            }
//            currentView = currentView?.superview
//        }
//        return nil // 如果没有找到，返回 nil
//    }
}
extension TSGeneralRintoneVC {
    
    var resultIcon:UIImage?{
        return UIImage(named: "ai_rintone_icon")
    }
    
    func updateInfoModel(model:TSActionInfoModel?){
        if let model = model {
            infoModel = model
            complete(model)
            setRingViewData(model: model)
        }
    }
    
//    func hiddenSelfVC(hidden:Bool){
//        self.view.isHidden = hidden
//        findFirstTransitionView()?.isHidden = hidden
//    }

}
extension TSGeneralRintoneVC {
    func upDateView(state:TSProgressState,model:TSActionInfoModel?){
        updateInfoModel(model: model)
        switch state {
            case .failed(let errorStr):
                audioPlayer.stop()
//                hiddenSelfVC(hidden: false)
                showError(text: errorStr)
            case .success:
                audioPlayer.stop()
//                hiddenSelfVC(hidden: false)
                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: "Working on your ringtone...".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
        
        ringView.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

        infoModel = model
        isClickTheBlankClosePage = true
        
        ringView.isHidden = false
        regenerateBtn.isHidden = false
        
    }
}
