//
//  TSAppBtnView.swift
//  AIEmoji
//
//  Created by 100Years on 2025/4/23.
//


class TSAppBtn: UIButton {
 
}

class TSNormalSubmitBtn: TSAppBtn {
//    override var isEnabled: Bool {
//       didSet {
//           // 根据 isEnabled 的值设置 alpha
//           self.alpha = isEnabled ? 1.0 : 0.6
//       }
//    }
}

class TSNormalCancelBtn: TSAppBtn {

}

class TSAppBtnView: TSBaseView {
    enum ViewStyle {
        case vip   //创造类的按钮
    }
    
    var viewH:CGFloat = 64
    var style:ViewStyle = .vip
    var vipFreeNumType:VipFreeNumType = .none{
        didSet{
            updateVipView()
        }
    }
    var clickBlock:(()->Void)?
    var btnFrame:CGRect?
    var isIconVipBlock:(()->Bool)? //vip 图片显示
    var isClickVipBlock:(()->Bool)?  //点击是,是否需要弹出 vip
    
    
    
    var loading:Bool = false{
        willSet {
            if loading != newValue {
                loadingAnimation(loading: newValue)
                
                if loading == false {
                    self.resetBtnText()
                }
            }
        }
    }
    //###################################### Button ######################################
    var button:UIButton = UIButton()
    var title:String = ""
    override func creatUI() {

    }
    
    override func dealThings(){
        
    }

    func setUpButton(title:String,style:ViewStyle,vipFreeNumType:VipFreeNumType = .none,btnFrame:CGRect? = nil,clickBlock: @escaping () -> Void) {
        self.btnFrame = btnFrame
        self.style = style
        self.vipFreeNumType = vipFreeNumType
        self.clickBlock = clickBlock
        self.title = title
        contentView.removeAllSubViews()
        
        switch style {
        case .vip:
            setUpGenerate()
            launchVipLogic()
        }
        
    }
    
    
    func setLoadingText(text:String?){
        if loading {
            button.setTitle(text, for: .normal)
        }else{
            resetBtnText()
        }
    }
    
    func resetBtnText(){
        button.setTitle(title, for: .normal)
    }
}

extension TSAppBtnView{
    
    func launchVipLogic(){
        //监听 Vip 变化
        NotificationCenter.default.addObserver(forName: .kPurchaseDidChanged, object: nil, queue: OperationQueue.main) { [weak self] notification in
            guard let self = self else { return }
            updateVipView()
        }
        
        NotificationCenter.default.addObserver(forName: .kVipFreeNumChanged, object: nil, queue: OperationQueue.main) { [weak self] notification in
            guard let self = self else { return }
            if let userInfo = notification.userInfo as? [String: VipFreeNumType], let myEnum = userInfo["VipFreeNumType"] {
                if myEnum == self.vipFreeNumType {
                    self.updateVipView()
                }
            }
        }
        
        updateVipView()
    }

    func updateVipView() {
        switch style {
        case .vip:
            setVip(vip: isIconVipBlock?() ?? false)
        }
    }
    
    func setBtnEnabled(isEnabled:Bool) {
        button.isEnabled = isEnabled
//        button.alpha = isEnabled ? 1.0 : 0.6
    }
    
    var isEnabled:Bool{
        return button.isEnabled
    }
    
    func setVip(vip:Bool) {
        
        if loading { //loading,正在显示转圈动画
            return
        }
        
        if vip {
            button.setImage(UIImage(named: "btnImage_vip"), for: .normal)
        }else{
            button.setImage(nil, for: .normal)
        }
    }
    
}
//创造按钮
extension TSAppBtnView{

    func setUpGenerate() {
        button = kCreateNormalSubmitBtn(
            title: title,
            size: CGSizeMake(k_ScreenWidth - 32, 48),
            action: { [weak self]  in
            guard let self = self else { return }
            if let vc = WindowHelper.getCurrentViewController() {
                if kPurchaseBusiness.kJudgeVip(externalBool: isClickVipBlock?() ?? false, vc: vc) { return }  //判断 vip
            }

            clickBlock?()
        })
        contentView.addSubview(button)
        button.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(button.width)
            make.height.equalTo(button.height)
        }

        viewH = 48
    }
}
//创造按钮
extension TSAppBtnView{
    
    private func loadingAnimation(loading:Bool) {
        if loading {
            button.setImage(UIImage(named: "generated_loading"), for: .normal)
            self.button.imageView?.startRotating()
        }else {
            button.imageView?.stopRotating()
            updateVipView()
        }
    }
    
}

//常用提交按钮
func kCreateNormalSubmitBtn(
    title:String,
    font:UIFont = UIFont.font(name: .PoppinsBoldItalic,size: 16),
    backgroundColor:UIColor = "#111111".uiColor,
    backgroundDisabledColor:UIColor = "#BCBCBC".uiColor,
    size:CGSize = CGSize(width: 343, height: 56),
    action: (() -> Void)? = nil
)-> UIButton {
    let btn = UIButton.createButton(title:title,font: font,titleColor:.themeColor,corner: 28.0,action: action)
    btn.size = size
    
    btn.setBackgroundImage(UIImage.colorFrom(color:backgroundColor,size: size), for: .normal)
    btn.setBackgroundImage(UIImage.colorFrom(color:backgroundDisabledColor,size: size), for: .disabled)
    
    btn.setTitleColor(.themeColor, for: .normal)
    btn.setTitleColor(.white, for: .disabled)
    btn.setTitleImageSpace(spacing: 8)
    return btn
}


//给按钮设置 vip 图标
func kSetBtnVipIcon(btn:UIButton,vipImage:UIImage = .btnImageVip ,show:Bool){
    btn.setImage(show ? vipImage : nil, for: .normal)
    btn.setTitleImageSpace(spacing: show ? 8 : 0)
}
