package com.gamedog.vididin.features.withdraw

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import androidx.activity.viewModels
import com.ama.core.architecture.appBase.AppViewsActivity
import com.ama.core.architecture.util.setOnClickBatch
import com.gamedog.vididin.R
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.features.withdraw.widget.WithDrawItemView
import com.gamedog.vididin.main.interfaces.OnTabStyleListener
import com.gamedog.vididin.router.Router
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
import com.gamedog.vididin.databinding.ActivityWithdrawBinding as ViewBinding
import com.gamedog.vididin.main.MainUiState as UiState
import com.gamedog.vididin.main.MainViewModel as ViewModel


@AndroidEntryPoint
class WithDrawActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnTabStyleListener {

    override val mViewModel: ViewModel by viewModels()
    override fun inflateViewBinding(inflater: LayoutInflater) = ViewBinding.inflate(inflater)

    private val mItemViewList: MutableList<WithDrawItemView> = mutableListOf()
    private var mCurSelectedIndex: Int = 0



    override fun ViewBinding.initWindowInsets() {
        setImmerseRootView(contentRoot)
    }

    override fun ViewBinding.initViews() {
        titlebar.setTitleText(R.string.sacar)

        mItemViewList.add(withdraw01)
        mItemViewList.add(withdraw10)
        mItemViewList.add(withdraw20)
        mItemViewList.add(withdraw50)
        mItemViewList.add(withdraw100)
        mItemViewList.add(withdraw300)

        withdraw01.setNumAndAction(0, 0.1F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdraw10.setNumAndAction(1, 10F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdraw20.setNumAndAction(2, 20F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdraw50.setNumAndAction(3, 50F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdraw100.setNumAndAction(4, 100F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdraw300.setNumAndAction(5, 300F,
            { itemIndex->
                updateUIItemSelectStates(itemIndex)
            })

        withdrawPix2.setIconAndText(R.mipmap.pix2_big, R.string.pix2, {

        })


        withdrawPix2.setSelectedState(true)
        updateUIItemSelectStates(0)

        setOnClickBatch(tvSacar, withdrawRecord) {
            when(this) {
                tvSacar -> {
                    val hasBindBank = AccountManager.hasValidBankInfo()
                    val cashNum = mItemViewList.get(mCurSelectedIndex).getCashNum()
                    if (!hasBindBank) {
                        WithdrawBindBankDialog(this@WithDrawActivity).setWithDrawCashNum(cashNum).show()
                    } else {
                        WithdrawInfoConfirmDialog(this@WithDrawActivity).setWithDrawCashNum(cashNum).show()
                    }
                }

                withdrawRecord -> {
                    Router.WithdrawRecord.startActivity(this@WithDrawActivity)
                }
            }
        }

        updateUICashTotal()
    }

    private fun updateUIItemSelectStates(itemIndex: Int) {
        mCurSelectedIndex = itemIndex
        mItemViewList.forEachIndexed { index, view ->
            view.setSelectedState(index == mCurSelectedIndex)
        }
    }

    private fun updateUICashTotal() {
        binding.tvCashTotal.text = AccountManager.getCash().toString()
    }

    override fun ViewBinding.initListeners() {
        registerEvents({ data->
            updateUICashTotal()
        }, VididinEvents.Event_Account_Cash_Changed)
    }

    override fun ViewBinding.initObservers() {
        //TODO("Not yet implemented")
    }

    override fun ViewBinding.onUiStateCollect(uiState: UiState) {
        //TODO("Not yet implemented")
    }

    override fun onTabIsDarkFont(isDarkFont: Boolean) {
        //TODO("Not yet implemented")
    }


    companion object {
        internal fun startActivity(activity: Activity) {
            activity.startActivity(Intent(activity.applicationContext, WithDrawActivity::class.java))
        }
    }

}