//
//  TSPurchaseTool.swift
//  AIRingtone
//
//  Created by 100Years on 2025/3/16.
//

/// 免费生成图片次
public enum VipFreeNumType: String, CaseIterable {
    case none                = "kNone"
    case ringtones           = "kRingtonesFreeNum"
    case posetr              = "kPosetrFreeNum"
    case photo               = "kPhotoFreeNum"
}


let kPurchaseToolShared = TSPurchaseTool.shared
class TSPurchaseTool {
    private let kFreeNumKey = "kFreeNumKey"
    static let shared = TSPurchaseTool()

    init() {
        initializeForFree()
    }
    
    // 免费使用会员的次数
    var freeDict:[String:Int] = [:]
    
    var isVip:Bool{
//        #if DEBUG
//            return true
//        #endif
        return kPurchaseDefault.isVip
    }
    /// 使用一次免费次数
    func useOnceForFree(type:VipFreeNumType){
        
//        if isVip {
//            return
//        }
        
        var freeNum = freeDict[type.rawValue] ?? 0
        if freeNum > 0 {
            freeNum-=1
        }
        
        if freeNum < 0 {
            freeNum = 0
        }
        
        freeDict[type.rawValue] = freeNum
        saveForFree()
        
        NotificationCenter.default.post(name: .kVipFreeNumChanged, object: nil, userInfo: ["VipFreeNumType": type])
    }
    
    func freeNum(type:VipFreeNumType) -> Int{
        let freeNum = freeDict[type.rawValue] ?? 0
        return freeNum
    }
    
    func saveForFree(){
        UserDefaults.standard.set(freeDict, forKey: kFreeNumKey)
        UserDefaults.standard.synchronize()
    }
    
    func initializeForFree(){
        if let dict = UserDefaults.standard.dictionary(forKey: kFreeNumKey) as? [String:Int]{
            freeDict = dict
        }else{
            freeDict = [
                VipFreeNumType.ringtones.rawValue:1,
                VipFreeNumType.posetr.rawValue:1,
                VipFreeNumType.photo.rawValue:1
            ]
            saveForFree()
        }
    }
    
    /// 免费次数是否可用
    func freeNumAvailable(type:VipFreeNumType) -> Bool{
        if isVip == true {
            return true
        }else{
            if let freeNum = freeDict[type.rawValue],freeNum > 0 {
                return true
            }
        }
        return false
    }
    
    /// 是否展示生成类的会员图标
    func generateVipShow(type:VipFreeNumType) -> Bool{
        if isVip == false, freeNum(type: type) > 0 {
            return false
        }
        return true
    }
}

extension TSPurchaseTool{
    
    
    func luanchuPrchase() {
        kPurchaseDefault.AppleSharedKey = "4ed5151881304025bfee0295ee459422"
        kPurchaseDefault.purchaseProducts = [
            PurchaseProduct(productId: "01", period:.month)
        ]
        kPurchaseDefault.requestProducts()
        
    }
    
}

extension TSPurchaseTool{
    func kJudgeVipFreeType(vipFreeNumType:VipFreeNumType,
                   vc:UIViewController,
                   closePageBlock:(()->Void)? = nil) -> Bool {
        //判断 vip
        return kJudgeVip(externalBool: kPurchaseToolShared.freeNumAvailable(type: vipFreeNumType) == false, vc: vc,closePageBlock: closePageBlock)
    }


    func kJudgeVip(externalBool:Bool,
                   vc:UIViewController,
                   closePageBlock:(()->Void)? = nil) -> Bool {
        //判断 vip
        if externalBool,
           kPurchaseToolShared.isVip == false
        {
            TSPurchaseVC.show(target: vc, closePageBlock: nil)
            return true
        }
        return false
    }

    
    func kshareBand(needVip:Bool,
                   vc:UIViewController,
                    urlString:String,
                    fileName:String,
                    completion:((Bool)->Void)? = nil) -> Bool {
        //判断 vip
        if needVip,
           kPurchaseToolShared.isVip == false
        {
            TSPurchaseVC.show(target: vc, closePageBlock: nil)
            return true
        }
        TSBandRingTool.creatBandRingTool().shareBandVC(vc: vc, fileURLString: urlString, fileName: fileName,completion: completion)
        return false
    }
}

