package com.adealink.weparty.wallet.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.module.wallet.viewmodel.IWalletViewModel
import com.adealink.weparty.wallet.manager.IWalletListener
import com.adealink.weparty.wallet.manager.walletManager
import com.adealink.weparty.wallet.recharge.data.RechargeConfigData
import kotlinx.coroutines.launch

class WalletViewModel : BaseViewModel(), IWalletViewModel, IWalletListener {
    override val coinLD: LiveData<Double> = MutableLiveData()
    override val diamondLD: LiveData<Double> = MutableLiveData()
    override val beanLD: LiveData<Double> = MutableLiveData()

    init {
        walletManager.addListener(this)
    }

    override fun onCleared() {
        super.onCleared()
        walletManager.removeListener(this)
    }

    fun getWalletData() {
        viewModelScope.launch {
            walletManager.getWalletData()
        }
    }

    override fun refreshWalletData() {
        viewModelScope.launch {
            walletManager.refreshWalletData()
        }
    }

    override fun getCoin(): Double {
        return walletManager.getCurrencyCound(Currency.COIN)
    }

    override fun getDiamond(): Double {
        return walletManager.getCurrencyCound(Currency.DIAMOND)
    }

    override fun getBean(): Double {
        return walletManager.getCurrencyCound(Currency.BEAN)
    }

    override fun onCurrencyChanged(currency: Map<Currency, Double>) {
        coinLD.send(currency[Currency.COIN] ?: 0.0)
        diamondLD.send(currency[Currency.DIAMOND] ?: 0.0)
        beanLD.send(currency[Currency.BEAN] ?: 0.0)
    }

    fun loadConvertConfig(): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(walletManager.loadConvertConfig())
        }
        return liveData
    }

    fun calcConvertResult(
        fromCurrency: Currency,
        toCurrency: Currency,
        amount: Double
    ): Double {
        return walletManager.calcConvertResult(
            fromCurrency,
            toCurrency,
            amount
        )
    }

    fun convertTo(
        fromCurrency: Currency,
        toCurrency: Currency,
        amount: Double
    ): LiveData<Rlt<Double>> {
        val liveData = OnceMutableLiveData<Rlt<Double>>()
        viewModelScope.launch {
            liveData.send(
                walletManager.convertTo(fromCurrency, toCurrency, amount)
            )
        }
        return liveData
    }

    fun recharge(data: RechargeConfigData): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            liveData.send(
                walletManager.recharge(data)
            )
        }
        return liveData
    }
}