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

import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
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.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.wallet.data.Currency
import com.adealink.weparty.module.wallet.data.PayChannel
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.wallet.R
import com.adealink.weparty.wallet.databinding.DialogRechargeBinding
import com.adealink.weparty.wallet.pay.PayRes
import com.adealink.weparty.wallet.recharge.adapter.RechargeErrorItemViewBinder
import com.adealink.weparty.wallet.recharge.adapter.RechargeItemViewBinder
import com.adealink.weparty.wallet.recharge.data.BaseRechargeListItemData
import com.adealink.weparty.wallet.recharge.data.RechargeErrorItemData
import com.adealink.weparty.wallet.recharge.data.RechargeItemData
import com.adealink.weparty.wallet.recharge.data.RechargeListErrorType
import com.adealink.weparty.wallet.recharge.viewmodel.RechargeViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModel
import com.adealink.weparty.wallet.viewmodel.WalletViewModelFactory
import com.adealink.weparty.R as APP_R


class RechargeDialog : BottomDialogFragment(R.layout.dialog_recharge) {

    companion object {
        private const val SPAN = 3
    }

    private var currency: Currency = Currency.COIN

    private val binding by viewBinding(DialogRechargeBinding::bind)

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

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

    private val adapter by fastLazy { MultiTypeListAdapter<BaseRechargeListItemData>() }

    fun setCurrency(currency: Currency): RechargeDialog {
        this.currency = currency
        return this
    }

    override fun initViews() {
        super.initViews()
        if (currency == Currency.BEAN) {
            //不支持充值金豆
            dismiss()
        }

        adapter.register(RechargeItemViewBinder(currency) { it ->
            selectRecharge(it)
        })
        adapter.register(RechargeErrorItemViewBinder())

        binding.rvRecharge.adapter = adapter
        binding.rvRecharge.layoutManager = GridLayoutManager(context, SPAN).apply {
            spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    return if (adapter.items[position] is RechargeErrorItemData) {
                        3
                    } else {
                        1
                    }
                }
            }
        }
        binding.rvRecharge.addItemDecoration(
            GridSpacingItemDecoration(SPAN, 8.dp(), 10.dp(), false)
        )

        when (currency) {
            Currency.BEAN -> {

            }

            Currency.COIN -> {
                binding.ivWalletCurrency.setImageResource(APP_R.drawable.common_wallet_coin_ic)
            }

            Currency.DIAMOND -> {
                binding.ivWalletCurrency.setImageResource(APP_R.drawable.common_wallet_diamond_ic)
            }
        }

        binding.btnRecharge.onClick {
            goRecharge()
        }
    }

    override fun loadData() {
        super.loadData()
        walletViewModel.getWalletData()
        rechargeViewModel.setupPayChannel(PayChannel.Google).observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    when (rlt.error.serverCode) {
                        PayRes.FEATURE_NOT_SUPPORTED -> {
                            adapter.submitList(
                                listOf(
                                    RechargeErrorItemData(
                                        RechargeListErrorType.GoogleNoInstall,
                                        rlt.error
                                    )
                                ), true
                            )
                        }

                        else -> {
                            adapter.submitList(
                                listOf(
                                    RechargeErrorItemData(
                                        RechargeListErrorType.NetError,
                                        rlt.error
                                    )
                                ), true
                            )
                        }
                    }
                }

                is Rlt.Success -> {
                    rechargeViewModel.pullRechargeList(PayChannel.Google, currency)
                }
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        when (currency) {
            Currency.BEAN -> {

            }

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

            Currency.DIAMOND -> {
                walletViewModel.diamondLD.observe(viewLifecycleOwner) {
                    binding.tvWalletDetail.text = formatNumberStr(it, false)
                }
            }
        }
        rechargeViewModel.rechargeListRltLD.observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    adapter.submitList(
                        listOf(
                            RechargeErrorItemData(RechargeListErrorType.NetError, rlt.error)
                        ), true
                    )
                }

                is Rlt.Success -> {
                    //Ntd.
                }
            }
        }
        rechargeViewModel.rechargeListLD.observe(viewLifecycleOwner) {
            if (it.isNullOrEmpty()) {
                adapter.submitList(
                    listOf(
                        RechargeErrorItemData(RechargeListErrorType.RechargeListEmpty)
                    ), true
                )
            } else {
                adapter.submitList(it, true)
            }

        }
    }

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

    private fun goRecharge() {
        val act = activity ?: return
        val selectItem = rechargeViewModel.getSelectedItem()
        if (selectItem == null) {
            showToast(R.string.wallet_recharge_not_selected)
            return
        }
        showLoading()
        rechargeViewModel.buyProduct(act, selectItem.data).observe(viewLifecycleOwner) { rlt ->
            dismissLoading()
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    dismiss()
                }
            }
        }
    }

}