package com.gamedog.vididin.features.withdraw.dialogs


import android.app.Activity
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.ResUtil
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.widget.BindingDialog
import com.gamedog.statisticreporter.StatisticUtil
import com.viddin.videos.free.R
import com.gamedog.vididin.core.login.login.AccountManager
import com.viddin.videos.free.databinding.DialogWithdrawInfoConfirmBinding as ViewBinding


class WithdrawInfoConfirmDialog(context: Activity, private val onConfirmed: (cashNum: Double)->Unit) : BindingDialog<ViewBinding>(context, ViewBinding::inflate) {

    private var mWithdrawCashNum: Double = 0.0
    private var mWithdrawCashNumStr: String = ""

    init {
        build()
    }


    private fun build() {
        with()
        setCenter()
        setMaskValue(0.8f)
        setCanCancel(false)

        mBinding.run {
            setOnClickBatch(tvActionAlter, tvActionApply, ivClose) {
                when (this) {
                    ivClose ->  {
                        dismiss()
                    }

                    tvActionAlter ->  {
                        val dialogBuilder = WithdrawBindBankDialog(activity = mActivity, onConfirmed)
                        if (!mWithdrawCashNumStr.isEmpty()) {
                            dialogBuilder.setWithDrawCashNumStr(mWithdrawCashNumStr).show()
                        } else {
                            dialogBuilder.setWithDrawCashNum(mWithdrawCashNum).show()
                        }
                        dismiss()
                    }

                    tvActionApply ->  {
                        onConfirmed.invoke(mWithdrawCashNum)
                        StatisticUtil.reportEvents(StatisticUtil.KEY_Withdrawal_Apply,
                            mapOf("Withdrawal_Position" to mWithdrawCashNum,
                                "Withdrawal_Day" to 1))
                        dismiss()
                    }
                }
            }
        }
    }


    fun setWithDrawCashNum(withdrawNum: Double): WithdrawInfoConfirmDialog {
        mWithdrawCashNum = withdrawNum

        mBinding.tvCashNum.text = buildString {
            append(ResUtil.getString(R.string.cash))
            append(" ")
            append(mWithdrawCashNum)
        }
        mBinding.tvCpfAccount.text = AccountManager.getBankInfo()?.bankAccount

        return this
    }

    fun setWithDrawCashNumStr(withdrawNumStr: String): WithdrawInfoConfirmDialog {
        mWithdrawCashNumStr = withdrawNumStr

        mBinding.tvCashNum.text = buildString {
            append(mWithdrawCashNumStr)
        }
        mBinding.tvCpfAccount.text = AccountManager.getBankInfo()?.bankAccount

        return this
    }

}

