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



import android.app.Activity
import android.text.Editable
import android.text.TextWatcher
import androidx.core.view.isVisible
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.gamedog.vididin.features.withdraw.BankUtil
import com.viddin.videos.free.databinding.DialogWithdrawBindingBankBinding as ViewBinding


class WithdrawBindBankDialog(activity: Activity, private val onConfirmedWithdraw: ((cashNum: Double)->Unit)?) : BindingDialog<ViewBinding>(activity, ViewBinding::inflate) {

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

    init {
        build()
    }


    private fun build() {
        with()
        setBottom()
        setMaskValue(0.9f)
        setCanCancel(false)

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

                    tvConfirm -> {
                        saveBankAccount(mBinding.tvCpfEdit.text.toString().trim())
                        if (onConfirmedWithdraw != null) {
                            val dialogBuilder = WithdrawInfoConfirmDialog(mActivity, onConfirmedWithdraw!!)
                            if (mWithdrawCashNum > 0F) {
                                dialogBuilder.setWithDrawCashNum(mWithdrawCashNum)
                            } else {
                                dialogBuilder.setWithDrawCashNumStr(mWithdrawCashNumStr)
                            }
                            dialogBuilder.show()
                        }

                        dismiss()
                    }
                }
            }



            tvCpfEdit.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    checkBankAccountValidation(s.toString())
                }

                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                }

            })


            AccountManager.getAccount()?.bankInfo?.bankAccount.also { tvCpfEdit.setText(it) }
        }

        checkBankAccountValidation("")
    }

    private fun saveBankAccount(bankAccount: String) {
        AccountManager.saveBankAccount(bankAccount)

        StatisticUtil.reportEvents(StatisticUtil.KEY_Withdrawal_Info, mapOf("Info_Type" to "CPF", "Withdrawal_Account" to bankAccount))
    }

    private fun checkBankAccountValidation(bankAccount: String) {
        if (BankUtil.isValidCpf(bankAccount) || (bankAccount.trim().length == 11)) {
            mBinding.ivState.isVisible = true
            mBinding.ivState.setImageResource(R.mipmap.icon_success)
            mBinding.tvConfirm.alpha = 1F
            mBinding.tvConfirm.isClickable = true
        } else {
            mBinding.ivState.isVisible = !bankAccount.isEmpty()
            mBinding.ivState.setImageResource(R.mipmap.icon_fail)
            mBinding.tvConfirm.alpha = 0.3F
            mBinding.tvConfirm.isClickable = false
        }
    }



    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }


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

        if (mWithdrawCashNum == 0.0) {
            mBinding.tvConfirm.text = ResUtil.getString(R.string.confirm)
        }
        return this
    }

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

        if (mWithdrawCashNum == 0.0) {
            mBinding.tvConfirm.text = ResUtil.getString(R.string.confirm)
        }
        return this
    }

}

