package com.adealink.weparty.wallet.recharge.dialog

import androidx.fragment.app.activityViewModels
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.wallet.R
import com.adealink.weparty.wallet.databinding.DialogConvertCurrencyBinding
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.adealink.weparty.R as APP_R

class ConvertCurrencyDialog : BottomDialogFragment(R.layout.dialog_convert_currency) {

    var fromCurrency: Currency = Currency.COIN
    var toCurrency: Currency = Currency.DIAMOND

    private val binding by viewBinding(DialogConvertCurrencyBinding::bind)

    private val walletViewModel by activityViewModels<WalletViewModel> { WalletViewModelFactory() }

    override fun initViews() {
        super.initViews()
        binding.tvTitle.text =
            getCompatString(R.string.wallet_convert_currency_title, toCurrency.desc())

        when (fromCurrency) {
            Currency.COIN -> {
                binding.tvFromCurrency.text = getCompatString(R.string.wallet_convert_from_coin)
                binding.ivFromCurrency.setImageResource(APP_R.drawable.common_wallet_coin_32_ic)
            }

            Currency.DIAMOND -> {
                binding.tvFromCurrency.text = getCompatString(R.string.wallet_convert_from_diamond)
                binding.ivFromCurrency.setImageResource(APP_R.drawable.common_wallet_diamond_32_ic)
            }

            Currency.BEAN -> {
                binding.tvFromCurrency.text = getCompatString(R.string.wallet_convert_from_bean)
                binding.ivFromCurrency.setImageResource(APP_R.drawable.common_wallet_bean_32_ic)
            }
        }

        when (toCurrency) {
            Currency.COIN -> {
                binding.tvToCurrency.text = getCompatString(R.string.wallet_convert_to_coin)
                binding.ivToCurrencyAmount.setImageResource(APP_R.drawable.common_wallet_coin_32_ic)
            }

            Currency.DIAMOND -> {
                binding.tvToCurrency.text = getCompatString(R.string.wallet_convert_to_diamond)
                binding.ivToCurrencyAmount.setImageResource(APP_R.drawable.common_wallet_diamond_32_ic)
            }

            Currency.BEAN -> {
                binding.tvToCurrency.text = getCompatString(R.string.wallet_convert_to_bean)
                binding.ivToCurrencyAmount.setImageResource(APP_R.drawable.common_wallet_diamond_32_ic)
            }
        }

        binding.btnAllConvert.onClick {
            allExchanged()
        }

        binding.btnExchange.onClick {
            confirmExchange()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        when (fromCurrency) {
            Currency.BEAN -> {
                walletViewModel.coinLD.observe(viewLifecycleOwner) {
                    binding.tvCurrentCurrency.text = formatNumberStr(it)
                }
            }

            Currency.COIN -> {
                walletViewModel.coinLD.observe(viewLifecycleOwner) {
                    binding.tvCurrentCurrency.text = formatNumberStr(it)
                }
            }

            Currency.DIAMOND -> {
                walletViewModel.diamondLD.observe(viewLifecycleOwner) {
                    binding.tvCurrentCurrency.text = formatNumberStr(it)
                }
            }
        }
    }

    override fun loadData() {
        super.loadData()
        walletViewModel.refreshWalletData()
        walletViewModel.loadConvertConfig().observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success<*> -> {
                    binding.etFromCurrencyAmount.isEnabled = true
                }
            }
        }
    }

    private fun allExchanged() {
        when (fromCurrency) {
            Currency.COIN -> {
                val toResult = walletViewModel.calcConvertResult(
                    Currency.COIN,
                    toCurrency,
                    walletViewModel.coinLD.value ?: 0.0
                )
                binding.tvToCurrencyAmount.text = formatNumberStr(toResult)
            }

            Currency.DIAMOND -> {
                val toResult = walletViewModel.calcConvertResult(
                    Currency.DIAMOND,
                    toCurrency,
                    walletViewModel.diamondLD.value ?: 0.0
                )
                binding.tvToCurrencyAmount.text = formatNumberStr(toResult)
            }

            Currency.BEAN -> {
                val toResult = walletViewModel.calcConvertResult(
                    Currency.BEAN,
                    toCurrency,
                    walletViewModel.diamondLD.value ?: 0.0
                )
                binding.tvToCurrencyAmount.text = formatNumberStr(toResult)
            }
        }

    }

    private fun confirmExchange() {

    }


}