package com.remax.visualnovel.repository.api

import com.remax.visualnovel.api.service.PayService
import com.remax.visualnovel.entity.request.ChargeOrderDTO
import com.remax.visualnovel.entity.request.ChargeProduct
import com.remax.visualnovel.entity.request.ChargeProductInfo
import com.remax.visualnovel.entity.request.SearchPage
import com.remax.visualnovel.entity.request.ValidateTransactionDTO
import com.remax.visualnovel.entity.response.UserSubInfo
import com.remax.visualnovel.entity.response.Wallet
import com.remax.visualnovel.entity.response.base.Response
import com.remax.visualnovel.repository.api.base.BaseRepository
import com.pengxr.modular.eventbus.generated.events.EventDefineOfWalletEvents
import javax.inject.Inject

/**
 * Created by HJW on 2022/11/8
 *
 * 支付相关
 */
class PayRepository @Inject constructor(
    private val accountService: PayService,
) : BaseRepository() {

    suspend fun getVipPrivilegeList() = executeHttp {
        accountService.getVipPrivilegeList()
    }

    suspend fun getTransactionList(request: SearchPage) = executeHttp {
        accountService.getTransactionList(request)
    }

    /**
     * 获取我的钱包
     */
    suspend fun getMyWallet(): Response<Wallet> = executeHttp(false) {
        accountService.getMyWallet()
    }.transformResult({
        EventDefineOfWalletEvents.buffBalanceUpdateSucceeded().post(it)
    })

    /**
     * 验证支付是否成功
     */
    suspend fun validateTranslation(dto: ValidateTransactionDTO): Response<Any> =
        executeHttp {
            accountService.validateTranslation(dto = dto)
        }

    /**
     * 获取充值产品
     */
    suspend fun getProducts(): Response<ChargeProductInfo> =
        executeHttp(false) {
            accountService.getChargeProducts()
        }

    /**
     * 创建一个google pay订单
     */
    suspend fun createOrder(product: ChargeProduct) = executeHttp {
        accountService.createOrder(dto = ChargeOrderDTO(product.chargeAmount, product.productId))
    }

    /**
     * 获取vip订阅价格列表
     */
    suspend fun getSubPriceList() = executeHttp(false) {
        accountService.getSubPriceList()
    }

    /**
     * 验证订阅是否成功
     */
    suspend fun uploadGoogleReceipt(dto: ValidateTransactionDTO): Response<String> = executeHttp {
        accountService.uploadGoogleReceipt(dto = dto)
    }

    /**
     * 订阅/升级VIP前查询订阅信息
     */
    suspend fun checkSubInfo(): Response<UserSubInfo> = executeHttp {
        accountService.checkSubInfo()
    }

}