package com.gamedog.vididin.features.withdraw



import android.app.Activity
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import androidx.core.view.isVisible
import com.ama.core.architecture.util.setOnClickBatch
import com.ama.core.architecture.widget.BindingDialog
import com.gamedog.vididin.R
import com.gamedog.vididin.core.login.login.AccountManager
import com.gamedog.vididin.databinding.DialogWithdrawBindingBankBinding as ViewBinding


class WithdrawBindBankDialog(activity: Activity) : BindingDialog<ViewBinding>(activity, ViewBinding::inflate) {

    private var mWithdrawCashNum: Float = 0F

    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())
                        WithdrawInfoConfirmDialog(mActivity).setWithDrawCashNum(mWithdrawCashNum).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
                ) {
                }

            })


        }

        checkBankAccountValidation("")
    }

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

    private fun checkBankAccountValidation(bankAccount: String) {
        // TODO - forTesting- contain "B"
        if (BankUtil.isValidCpf(bankAccount)  || bankAccount.contains("B")) {
            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: Float): WithdrawBindBankDialog {
        mWithdrawCashNum = withdrawNum
        return this
    }

}

