package com.adealink.weparty.wallet.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
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 kotlinx.coroutines.launch

class WalletViewModel : BaseViewModel(), IWalletViewModel, IWalletListener {
    override val coinLD: LiveData<Long> = MutableLiveData()
    override val diamondLD: LiveData<Long> = 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.refreshWalletDat()
        }
    }

    override fun onCurrencyChanged(currency: Map<Currency, Long>) {
        coinLD.send(currency[Currency.COIN] ?: 0)
        diamondLD.send(currency[Currency.DIAMOND] ?: 0)
    }

}