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

import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.toast.util.showToast
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.FragmentRechargeCoinBinding
import com.adealink.weparty.wallet.recharge.adapter.RechargeItemViewBinder
import com.adealink.weparty.wallet.recharge.data.RechargeItemData
import com.adealink.weparty.wallet.recharge.dialog.ConvertCurrencyDialog
import com.adealink.weparty.wallet.recharge.viewmodel.RechargeViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory

class CoinRechargeFragment : BaseFragment(R.layout.fragment_recharge_coin) {

    companion object {
        private const val SPAN = 3
    }

    private val binding by viewBinding(FragmentRechargeCoinBinding::bind)

    private val walletViewModel by viewModels<WalletViewModel> { WalletViewModelFactory() }
    private val rechargeViewModel by viewModels<RechargeViewModel> { WalletViewModelFactory() }

    private val adapter by fastLazy { MultiTypeListAdapter<RechargeItemData>() }
    override fun initViews() {
        super.initViews()

        adapter.register(RechargeItemViewBinder(Currency.COIN) { it ->
            selectRecharge(it)
        })

        binding.rvTopUp.adapter = adapter
        binding.rvTopUp.layoutManager = GridLayoutManager(context, SPAN)
        binding.rvTopUp.addItemDecoration(
            GridSpacingItemDecoration(SPAN, 10.dp(), 16.dp(), false)
        )

        binding.vCoinCard.btnConvert.onClick {
            convertToDiamond()
        }
        binding.btnTopUp.onClick {
            goRecharge()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        walletViewModel.coinLD.observe(viewLifecycleOwner) {
            binding.vCoinCard.tvBalance.text = formatNumberStr(it)
        }
        rechargeViewModel.rechargeListLD.observe(viewLifecycleOwner) {
            adapter.submitList(it, true)
        }
    }

    override fun loadData() {
        super.loadData()
        walletViewModel.getWalletData()
        rechargeViewModel.pullRechargeList(Currency.COIN)
    }

    private fun selectRecharge(item: RechargeItemData) {
        rechargeViewModel.selectItem(item)
    }

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

    private fun goRecharge() {
        val selectItem = rechargeViewModel.getSelectedItem()
        if (selectItem == null) {
            showToast(R.string.wallet_recharge_not_selected)
            return
        }
        showLoading()
        walletViewModel.recharge(selectItem.data)
    }
}