//
//  WalletApi.swift

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

import Moya

let WalletProvider = APIConfig.useMock && WalletAPI.useMock
    ? MoyaProvider<WalletAPI>(endpointClosure: myEndpointClosure, stubClosure: { target in
        let data = target.sampleData
        if data.count > 0 {
            return .delayed(seconds: 0.7)
        } else {
            return .never
        }
    })
    : MoyaProvider<WalletAPI>(requestClosure: myRequestClosure)

enum WalletAPI {
    static let useMock: Bool = true
    /// 钱包详情
    case myWallet
    /// 流水
    case billList(params: [String: Any])
    /// 充值预下单
    case tradePrecharge(params: [String:Any])
    /// Coin上传苹果票据
    case iapUploadCoinReceipt(params: [String: Any])
    /// 订阅上传苹果票据
    case iapUploadSubscribeAppleReceipt(params: [String:Any])
    /// 会员详情
    case vipDetail
    
    /// 订阅档位列表
    case vipTierProducts(params: [String: Any])
    /// 虚拟货币充值档位
    case coinTierProducts(params: [String: Any])
}

extension WalletAPI: TargetType {
    var baseURL: URL {
        return URL(string: APIConfig.lion)!
    }

    var path: String {
        switch self {
        case .myWallet:
            return "/web/pay/account/wallet"
        case .billList:
            return "/web/pay/account/bill-list"
        case .tradePrecharge:
            return "/web/pay/trade/pre-charge-iap"
        case .iapUploadCoinReceipt:
            return "/web/pay/webhooks/iap"
        case .iapUploadSubscribeAppleReceipt:
            return "/web/pay/subscribe/upload-apple-receipt"
        case .vipDetail:
            return "/web/member/detail"
        case .vipTierProducts:
            return "/web/pay/config/sub-product-list"
        case .coinTierProducts:
            return "/web/pay/config/charge-product-list"
        
        }
    }

    var method: Moya.Method {
        return .post
    }

    var task: Task {
        var mParams = [String: Any]()

        switch self {
        case .myWallet:
            break
        case .billList(let params):
            mParams = params
        case .tradePrecharge(let params):
            mParams = params
        case .iapUploadCoinReceipt(let params):
            mParams = params
        case .iapUploadSubscribeAppleReceipt(let params):
            mParams = params
        case .vipDetail:
            break
        case .vipTierProducts(let params):
            mParams = params
        case .coinTierProducts(let params):
            mParams = params
        }

        mParams.updateValue("IOS", forKey: "appClient")
        mParams.updateValue(UIDevice.UUID, forKey: "deviceCode")

        return .requestParameters(parameters: mParams, encoding: JSONEncoding.default)
    }

    var headers: [String: String]? {
        return APIConfig.apiHeaders()
    }

    var sampleData: Data {
        switch self {
        case .myWallet:
            return Data()
        default:
            return Data()
        }
    }
}
