//
//  VIPViewModel.swift

//
//  Created by Leon on 2025/9/17.
//

class CLPurchase{
    static let shared = CLPurchase()
    
    var vipSubscribeProducts = [VIPSubcribeProduct]()
    var memberDetailOut : MemberDetailOutput?
    // VIP特权列表
    var privileges = [MemberPrivDict]()
    
    func loadVIPTiersProducts(completion: @escaping ([VIPSubcribeProduct]?) -> Void){
        if vipSubscribeProducts.count > 0 {
            completion(vipSubscribeProducts)
            return
        }

        var params = [String: Any]()
        params.updateValue("iOS", forKey: "platform")
        params.updateValue("1", forKey: "version")
        
        WalletProvider.request(.vipTierProducts(params: params), modelType: [VIPSubcribeProduct].self) { result in
            switch result {
            case .success(let model):
                self.vipSubscribeProducts = model ?? []
                completion(model)
            case .failure:
                completion(nil)
            }
        }
        
    }
    
    func loadCoinTiersProducts(completion: @escaping (IAPTiersResponse?) -> Void){
        var params = [String: Any]()
        params.updateValue("iOS", forKey: "platform")
        params.updateValue("1", forKey: "version")
        
        WalletProvider.request(.coinTierProducts(params: params), modelType: IAPTiersResponse.self) { result in
            switch result {
            case .success(let model):
                completion(model)
            case .failure:
                completion(nil)
            }
        }
    }
    
    func loadCurrentUserVipDetail(completion: @escaping (MemberDetailOutput?) -> Void){       
        WalletProvider.request(.vipDetail, modelType: MemberDetailOutput.self) {[weak self] result in
            Hud.hideIndicator()
            switch result {
            case .success(let model):
                self?.memberDetailOut = model
                self?.privileges = model?.memberPrivList ?? []
                completion(model)
            case .failure:
                completion(nil)
            }
        }
    }
    
    // MARK: - Funtions
    
    func showIAPBuyCoinSheet(){
        WalletCore.shared.refreshWallet() // 刷新下余额（注意调整位置）
        
        let buyCoinSheet = CoinsRechargeSheet()
        buyCoinSheet.show()
    }
    
    /// 余额不足，弹出余额弹窗 + chatModel提醒
    func showChatModelsAndIAPEntrySheet(){
        WalletCore.shared.refreshWallet() // 刷新下余额（注意调整位置）
        
        let buyCoinSheet = ChatModePickInsufficientCoinSheet()
        buyCoinSheet.show()
    }
    
    func showVIPSubscribeSheet(){
        let sheet = VIPSubscribeSheet()
        sheet.show()
    }
    
    func showBuyPhotoGeneratedCountSheet(){
        let sheet = BuyCreditsSheetView()
        sheet.show()
    }
    
    
}
