//
//  TSButton.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/13.
//


class TSAppBtn: UIButton {
 
}

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


class TSNormalCancelBtn: TSAppBtn {

}

////常用取消按钮
//func kCreateNormalCancelBtn(title:String, action: (() -> Void)? = nil) -> UIButton {
//    let btn = UIButton.createButton(title:title,backgroundColor: .fromHex("#FFFFFF", alpha: 0.4),font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 30,action: action)
//    return btn
//}
////常用确定按钮
//func kCreateNormalConfirmBtn(title:String, action: (() -> Void)? = nil) -> UIButton {
//    let btn = UIButton.createButton(title:title,backgroundImage:UIImage(named: "submit_btn_small_bg"),font: UIFont.font(size: 14,weight: .medium),titleColor:.white,corner: 30,action: action)
//    return btn
//}


class TSAppBtnView: TSBaseView {
    enum ViewStyle {
        case normal
        case generate   //创造类的按钮
        case themeSet   //主题页设置按钮
        
    }
    
    var viewH:CGFloat = 64
    var style:ViewStyle = .normal
    var vipFreeNumType:VipFreeNumType = .none{
        didSet{
            updateVipView()
        }
    }
    var clickBlock:(()->Void)?
    
    //###################################### Button ######################################
    var button:UIButton = UIButton()

    override func creatUI() {

    }
    
    override func dealThings(){
        
    }

    func setUpButton(style:ViewStyle,vipFreeNumType:VipFreeNumType,clickBlock: @escaping () -> Void) {
        
        self.style = style
        self.vipFreeNumType = vipFreeNumType
        self.clickBlock = clickBlock
        
        contentView.removeAllSubviews()
        
        switch style {
        case .generate:
            setUpGenerate()
            launchVipLogic()
        case .themeSet:
            setUpThemeSet()
        default:
            break
        }
        
    }
}

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 .generate:
            setVip(vip: kPurchaseToolShared.generateVipShow(type: self.vipFreeNumType))
        default:
            break
        }
    }
    
    func setBtnEnabled(isEnabled:Bool) {
        button.isEnabled = isEnabled
        button.alpha = isEnabled ? 1.0 : 0.6
    }
    
    func setVip(vip:Bool) {
        if vip {
            button.setImage(UIImage(named: "vip_icon_white"), for: .normal)
        }else{
            button.setImage(nil, for: .normal)
        }
    }
    
}
//创造按钮
extension TSAppBtnView{

    func setUpGenerate() {
        button = kCreateNormalSubmitBtn(
            title: "Create Now".localized,
            frame: CGRectMake(0, 0, k_ScreenWidth - 32, 48),
            action: { [weak self]  in
            guard let self = self else { return }
            if let vc = WindowHelper.getCurrentViewController() {
                if kPurchaseToolShared.kJudgeVipFreeType(vipFreeNumType: vipFreeNumType, vc: vc){ return }
            }

            clickBlock?()
        })
        contentView.addSubview(button)
        button.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(button.width)
            make.height.equalTo(button.height)
        }
   
        
        viewH = 64
    }
    
    func setUpThemeSet() {
        button = kCreateNormalSubmitBtn(title: "Set Now".localized, frame: CGRectMake(0, 0, 200, 48),action: { [weak self]  in
            guard let self = self else { return }
            clickBlock?()
        })
        contentView.addSubview(button)
        button.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(button.width)
            make.height.equalTo(button.height)
        }
   
    }
}

//常用提交按钮
func kCreateNormalSubmitBtn(title:String,frame:CGRect,action: (() -> Void)? = nil) -> UIButton {
    let btn = TSNormalSubmitBtn()
    btn.setUpButton(title:title,font: UIFont.font(size: 16,weight: .regular),titleColor:.white,corner: 24,action: action)
    btn.setTitleImageSpace(spacing: 4)
    btn.frame = frame
    btn.addGradientBg(colors: ["#E961F6".uiColor.cgColor,"#7E57F4".uiColor.cgColor])
    return btn
}

