package com.adealink.weparty.wallet.recharge.fragment

import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.widget.CommonDialog
import com.adealink.weparty.config.viewmodel.ApprovingVersionViewModel
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.module.webview.Web
import com.adealink.weparty.url.UrlConfig
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.wallet.R
import com.adealink.weparty.wallet.databinding.FragmentRechargeBeanBinding
import com.adealink.weparty.wallet.recharge.dialog.ConvertCurrencyDialog
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.adealink.weparty.R as APP_R

class BeanExchangedFragment : BaseFragment(R.layout.fragment_recharge_bean) {

    private val binding by viewBinding(FragmentRechargeBeanBinding::bind)
    private val walletViewModel by viewModels<WalletViewModel> { WalletViewModelFactory() }
    private val approvingViewModel by activityViewModels<ApprovingVersionViewModel>()

    override fun initViews() {
        super.initViews()
        binding.vBeanCard.btnBeanMore.onClick {
            showMore()
        }
        binding.vBeanCard.ivUnavailableTips.onClick {
            showUnSettledTips()
        }
        binding.btnConvertCoin.onClick {
            convertToCoin()
        }
        binding.btnConvertDiamond.onClick {
            convertToDiamond()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        walletViewModel.beanLD.observe(viewLifecycleOwner) {
            binding.vBeanCard.tvBalance.text = formatNumberStr(it, false)
        }
        walletViewModel.availableBeanLD.observe(viewLifecycleOwner) {
            binding.vBeanCard.tvAvailableBalance.text = formatNumberStr(it, false)
        }
        walletViewModel.unsettledBeanLD.observe(viewLifecycleOwner) {
            binding.vBeanCard.tvUnsettledBalance.text = formatNumberStr(it, false)
        }
        approvingViewModel.approvingLD.observe(viewLifecycleOwner) { approving ->
            if (ProfileModule.getMyUserInfo()?.isPlaymate() == true) {
                binding.vBeanCard.btnBeanMore.show(!approving)
            } else {
                binding.vBeanCard.btnBeanMore.gone()
            }
        }
    }

    override fun loadData() {
        super.loadData()
        walletViewModel.getWalletData()
        approvingViewModel.refreshApproving()
    }

    private fun convertToCoin() {
        ConvertCurrencyDialog().apply {
            fromCurrency = Currency.BEAN
            toCurrency = Currency.COIN
        }.show(childFragmentManager, "ConvertCurrencyDialog")
    }

    private fun convertToDiamond() {
        ConvertCurrencyDialog().apply {
            fromCurrency = Currency.BEAN
            toCurrency = Currency.DIAMOND
        }.show(childFragmentManager, "ConvertCurrencyDialog")
    }

    private fun showUnSettledTips() {
        CommonDialog.Builder()
            .message(getCompatString(R.string.wallet_bean_unsettled_tips))
            .positiveText(getCompatString(APP_R.string.common_i_know))
            .setShowDefaultCancel(false)
            .show(childFragmentManager)
    }

    private fun showMore() {
        val act = activity ?: return
        Router.build(act, Web.FullScreen.PATH)
            .putExtra(Web.Common.EXTRA_URL, UrlConfig.beanMore)
            .start()
    }
}