package com.adealink.weparty.profile.me.comp

import androidx.lifecycle.LifecycleOwner
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.ViewComponent
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.module.wallet.Wallet
import com.adealink.weparty.module.wallet.WalletModule
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.profile.databinding.LayoutMeWalletBinding
import com.adealink.weparty.util.formatNumberStr

class MeWalletComp(
    lifecycleOwner: LifecycleOwner,
    val binding: LayoutMeWalletBinding
) : ViewComponent(lifecycleOwner) {
    private val walletViewModel by fastLazy { WalletModule.getWalletViewModel(requireActivity()) }

    override fun onCreate() {
        super.onCreate()
        initView()
        observeViewModel()
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    private fun initView() {
        binding.clCoin.onClick {
            goWallet(Currency.COIN)
        }
        binding.clDiamond.onClick {
            goWallet(Currency.DIAMOND)
        }
    }

    private fun observeViewModel() {
        walletViewModel?.coinLD?.observe(viewLifecycleOwner) {
            binding.tvCoin.text = formatNumberStr(it)
        }
        walletViewModel?.diamondLD?.observe(viewLifecycleOwner) {
            binding.tvDiamond.text = formatNumberStr(it)
        }
    }

    private fun loadData() {
        walletViewModel?.refreshWalletData()
    }

    private fun goWallet(currency: Currency) {
        val act = activity ?: return
        Router.build(act, Wallet.Wallet.PATH).start()
    }
}