//
//  TSGeneratorView.swift
//  AIEmoji
//
//  Created by 100Years on 2025/4/2.
//

import Kingfisher




let kTextLeading = 24.0

class TSGeneratorView: TSBaseView {
    
    enum Style {
        case loading    //loading
        case generalError //通用错误
        case sensitiveError   //敏感类的错误
        case netWorkError //网络错误
        case generateTooMuch //生成次数过多
    }
    
    var style:Style = .generalError
    var clickBackstageBlock:(()->Void)?
    var clickErrorBlock:((Style)->Void)?
    var clickCloseBlock:(()->Void)?
    var isUploadImage:Bool = true{
        didSet {
            errorView.isUploadImage = isUploadImage
        }
    }
    lazy var animationView: TSGeneratoringAnimationView = {
        let animationView = TSGeneratoringAnimationView()
        animationView.backgroundGenerateBtn.addTarget(self, action: #selector(clickBackstageBtn), for: .touchUpInside)
        return animationView
    }()
    
    lazy var errorView: TSGeneratorErrorView = {
        let errorView = TSGeneratorErrorView()
        errorView.isUploadImage = isUploadImage
        errorView.isHidden = true
        errorView.submitBtn.addTarget(self, action: #selector(clickErrorBtn), for: .touchUpInside)
        return errorView
    }()

    lazy var blurEffect: UIVisualEffectView = {
        let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
//        blurEffectView.alpha = 0.9
        return blurEffectView
    }()
    
    lazy var xBtn: UIButton = {
        let xBtn = UIButton.createButton(image: .close) { [weak self]  in
            guard let self = self else { return }
            clickCloseBlock?()
        }
  
        return xBtn
    }()
    

    override func creatUI() {
        contentView.addSubview(blurEffect)
        blurEffect.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.addSubview(animationView)
        animationView.snp.makeConstraints { make in
            make.leading.trailing.centerY.equalToSuperview()
        }
        
        contentView.addSubview(errorView)
        errorView.snp.makeConstraints { make in
            make.leading.trailing.centerY.equalToSuperview()
        }
        

        //关闭按钮
        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)
        }
    }
    

    override func dealThings() {
        // 监听应用生命周期事件
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleAppDidEnterBackground),
            name: UIApplication.didEnterBackgroundNotification,
            object: nil
        )
        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleAppWillEnterForeground),
            name: UIApplication.willEnterForegroundNotification,
            object: nil
        )
    }
    
    lazy var isRotating = false{
        didSet{
            if isRotating == true{
                animationView.startAnimation()
            }else{
                animationView.stopAnimation()
            }
        }
    }
    
    @objc private func handleAppDidEnterBackground() {
        animationView.stopAnimation()
    }

    @objc private func handleAppWillEnterForeground() {
        isRotating = isRotating
    }
    

    
    func showLoading(text:String){
        isRotating = true
        animationView.isHidden = false
        errorView.isHidden = true
        
        animationView.setProgressText(text: text)
    }
    
    func showError(text:String){
        isRotating = false
        animationView.isHidden = true
        errorView.isHidden = false
        errorView.style = style
        errorView.textLabel.text = text
    }

    func setBackgroundColor(color:UIColor){
        contentView.backgroundColor = color
    }
    
    //后台生成
    @objc func clickBackstageBtn() {
        clickBackstageBlock?()
    }
    
    @objc func clickErrorBtn() {
        clickErrorBlock?(self.style)
        errorView.submitBtn.preventMultipleTaps()
    }
    
}


extension TSGeneratorView{
    
    func updateShowProgress(text:String) {
        isHidden = false
        showLoading(text: text)
        isRotating = true
    }
    
    func updateShowLoading(text:String){
        isHidden = false
        showLoading(text: text)
        isRotating = true
    }
    
    func updateShowError(text:String,code:Int){
        self.style = TSNetWorkCode.getGeneratorStyle(code: code)
        isHidden = false
        showError(text: text.isEmpty ? kGenerateFailed : text)
        isRotating = false
        setBackgroundGenerateBtnHidden(true)
    }
    
    func updateShowSuccess(){
        isHidden = true
        isRotating = false
        setBackgroundGenerateBtnHidden(true)
    }
    
    func setBackgroundGenerateBtnHidden(_ isHidden:Bool){
        self.animationView.isShowBackGeneration = !isHidden
    }
}
