package com.adealink.weparty.wallet

import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner
import com.adealink.frame.spi.RegisterService
import com.adealink.weparty.module.wallet.IWalletService
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.module.wallet.viewmodel.IWalletViewModel
import com.adealink.weparty.wallet.manager.walletManager
import com.adealink.weparty.wallet.pay.payManager
import com.adealink.weparty.wallet.recharge.dialog.RechargeDialog
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory

@RegisterService(IWalletService::class)
class WalletServiceImpl : IWalletService {

    override fun onLogout() {
    }


    override fun getService(): IWalletService {
        return this
    }

    override fun init() {
        walletManager.init()
    }

    override fun getBalance(currency: Currency): Double {
        return walletManager.getBalance(currency)
    }

    override fun fetchCurrency() {
        walletManager.refreshWalletData()
    }

    override fun queryAndHandleUnDealPurchases() {
        payManager.queryAndHandleUnDealPurchases()
    }

    override fun getWalletViewModel(owner: ViewModelStoreOwner): IWalletViewModel {
        return ViewModelProvider(owner, WalletViewModelFactory())[WalletViewModel::class.java]
    }

    override fun showRechargeDialog(fm: FragmentManager, currency: Currency) {
        RechargeDialog()
            .setCurrency(currency)
            .show(fm, "RechargeDialog")
    }

}