package com.hongyegroup.cpt_ewallet.ui

import android.os.Bundle
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.cs_cptservices.popup.PaymentPasswordPopup
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
import com.guadou.lib_baselib.ext.checkNumberPoint
import com.guadou.lib_baselib.ext.commContext
import com.guadou.lib_baselib.ext.gotoActivity
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.KeyboardUtils
import com.hongyegroup.cpt_ewallet.BR
import com.hongyegroup.cpt_ewallet.R
import com.hongyegroup.cpt_ewallet.databinding.ActivityEwalletWithdrawBinding
import com.hongyegroup.cpt_ewallet.mvvm.EWalletWithdrawViewModel
import com.hongyegroup.cpt_ewallet.widget.WithdrawDisclaimersPopup
import com.lxj.xpopup.XPopup
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * 申请提现的页面
 */
class EWalletWithdrawActivity : YYBaseVDBActivity<EWalletWithdrawViewModel, ActivityEwalletWithdrawBinding>() {

    private var allMoney = "92400"   //全部的金额

    companion object {
        fun startInstance() {
            commContext().gotoActivity<EWalletWithdrawActivity>()
        }
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.activity_ewallet_withdraw, BR.viewModel, mViewModel)
            .addBindingParams(BR.click, ClickProxy())
    }

    override fun init(savedInstanceState: Bundle?) {
        setStatusBarBlackText()

        mViewModel.mAllMoney.value = allMoney

    }

    override fun startObserve() {

    }

    /**
     * DataBinding事件处理
     */
    inner class ClickProxy {

        val withdrawMethod = MutableStateFlow(0)
        var isSelectAll = MutableStateFlow(false)

        //金额的监听回调
        val onMoneyChanged: (String) -> Unit = { str ->
            var s1 = str
            if (!isSelectAll.value) {
                if (s1.startsWith(".")) s1 = "0$s1"
                //如果输入的值大于全部金额，那么就选全部金额
                if (s1.checkNumberPoint() &&
                    mViewModel.mAllMoney.value.checkNumberPoint() &&
                    s1.toDouble() > mViewModel.mAllMoney.value.toDouble()
                ) {
                    s1 = mViewModel.mAllMoney.value
                    //如果大于全部金额，那么ET也要展示最大金额，ET内容不能超过最大金额
                    CommUtils.getHandler().removeCallbacksAndMessages(null)
                    CommUtils.getHandler().postDelayed({
                        mBinding.etWithdrawalValue.setText(s1)
                        mBinding.etWithdrawalValue.setSelection(s1.length)
                    }, 1000)
                }
            } else {
                //已经勾选了All,并且值与AllValue不同,说明改值了，需要设置为非全选
                if (str != mViewModel.mAllMoney.value) {
                    isSelectAll.value = false
                }
            }
        }

        //选中银行卡提现
        fun chooseGrioMethod() {
            withdrawMethod.value = 1
        }

        fun selectCheckBox() {
            if (isSelectAll.value) {
                isSelectAll.value = false
            } else {
                isSelectAll.value = true
                mViewModel.mWithdrawMoney.value = mViewModel.mAllMoney.value
                CommUtils.getHandler().postDelayed({
                    mBinding.etWithdrawalValue.setSelection(mViewModel.mAllMoney.value.length)
                }, 50)
            }
        }

        //提交提现申请
        fun submitWithdraw() {
            KeyboardUtils.hideSoftkeyboard(mActivity)

            showDisclaimersDialog()
        }

        //提现的免责条例对话框
        private fun showDisclaimersDialog() {
            XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)
                .hasShadowBg(true)
                .dismissOnBackPressed(false)
                .dismissOnTouchOutside(false)
                .asCustom(WithdrawDisclaimersPopup(mActivity) {
                    verifyPaymentPassword()
                })
                .show()
        }

        //跳转到银行卡的信息填写页面
        private fun gotoBankWithdrawPage() {
            BankWithdrawActivity.startInstance(mViewModel.mWithdrawMoney.value)
        }

        //验证支付密码
        private fun verifyPaymentPassword() {
            XPopup.Builder(mActivity)
                .moveUpToKeyboard(true)
                .hasShadowBg(true)
                .isRequestFocus(true)
                .asCustom(PaymentPasswordPopup(mActivity) { code ->
                    toast("输入的密码：$code")

                    //调用接口验证支付密码
                    mViewModel.verifyPassword(code).observe(this@EWalletWithdrawActivity) {
                        gotoBankWithdrawPage()
                    }

                })
                .show()
        }

    }
}