package com.hongyegroup.cpt_ewallet.ui

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
import com.guadou.lib_baselib.bean.DataBindingConfig
import com.guadou.lib_baselib.engine.toast
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.ActivityPaymentPsdBinding
import com.hongyegroup.cpt_ewallet.mvvm.PaymentPsdViewModel
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * 支付密码的设置/修改页面
 *
 * 0为设置密码页面  1为重置密码页面 2.设置新密码 3确认密码页面
 */
class PaymentPsdActivity : YYBaseVDBActivity<PaymentPsdViewModel, ActivityPaymentPsdBinding>() {

    private val mClickProxy by lazy { ClickProxy() }
    private var mStatus = 0   //当前的页面状态

    companion object {
        fun startInstance(status: Int) {
            commContext().gotoActivity<PaymentPsdActivity>(bundle = arrayOf(Pair("status", status)))
        }
    }

    override fun getDataFromIntent(intent: Intent) {
        mStatus = intent.getIntExtra("status", 0)
    }

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

    override fun startObserve() {

    }

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

        setupTextWithStatus()
    }

    //根据状态更改文本样式
    private fun setupTextWithStatus() {

        //默认设置第一个选择的背景和对话框
        CommUtils.getHandler().postDelayed({
            KeyboardUtils.showSoftInput(mActivity, mBinding.editTextHidden)
        }, 250)
        mClickProxy.curSelectedIndex.value = 0

        when (mStatus) {
            0 -> {
                mBinding.tvSetPsd.text = "设置支付密码"
                mBinding.easyTitleBar.setTitle("设置支付密码")
                mBinding.tvForgetPsd.visibility = View.VISIBLE
            }
            1 -> {
                mBinding.tvSetPsd.text = "修改支付密码"
                mBinding.easyTitleBar.setTitle("修改支付密码")
                mBinding.tvForgetPsd.visibility = View.VISIBLE
            }
            2 -> {
                mBinding.tvSetPsd.text = "设置新密码"
                mBinding.easyTitleBar.setTitle("设置新密码")
                mBinding.tvForgetPsd.visibility = View.GONE
            }
            3 -> {
                mBinding.tvSetPsd.text = "确认新密码"
                mBinding.easyTitleBar.setTitle("确认新密码")
                mBinding.tvForgetPsd.visibility = View.GONE
            }

        }
    }

    //根据状态处理6位数验证码
    private fun handleCodebyStatus(code: String) {
        when (mStatus) {
            0 -> {
                //设置密码-调用接口设置支付密码
                mViewModel.setPaymentPassword(code).observe(this) {
                    toast("设置支付密码成功")
                    finish()
                }
            }
            1 -> {
                //修改密码-调用接口验证支付密码
                mViewModel.validPaymentPassword(code).observe(this) {
                    //验证成功去设置新密码
                    mClickProxy.resetETs()
                    mStatus = if (it) 2 else 1
                    setupTextWithStatus()
                }
            }
            2 -> {
                //设置新密码-记录第一次的密码
                mViewModel.mFirstPassword = code
                mClickProxy.resetETs()
                mStatus = 3
                setupTextWithStatus()
            }
            3 -> {
                //确认新密码-判断密码是否一样
                if (code == mViewModel.mFirstPassword) {
                    mViewModel.setPaymentPassword(code).observe(this) {
                        finish()
                    }
                } else {
                    toast("确认密码失败")
                }
            }

        }
    }


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

        var curSelectedIndex = MutableStateFlow(0)  //当前输入的框框索引
        var et1Code = MutableStateFlow("")
        var et2Code = MutableStateFlow("")
        var et3Code = MutableStateFlow("")
        var et4Code = MutableStateFlow("")
        var et5Code = MutableStateFlow("")
        var et6Code = MutableStateFlow("")

        val onHidenChanged: (String) -> Unit = { hiden ->
            when (hiden.length) {
                0 -> {
                    curSelectedIndex.value = 0
                    et1Code.value = ""
                    et2Code.value = ""
                    et3Code.value = ""
                    et4Code.value = ""
                    et5Code.value = ""
                    et6Code.value = ""
                }
                1 -> {
                    curSelectedIndex.value = 1
                    et1Code.value = hiden[0].toString()
                }
                2 -> {
                    curSelectedIndex.value = 2
                    et2Code.value = hiden[1].toString()
                }
                3 -> {
                    curSelectedIndex.value = 3
                    et3Code.value = hiden[2].toString()
                }
                4 -> {
                    curSelectedIndex.value = 4
                    et4Code.value = hiden[3].toString()
                }
                5 -> {
                    curSelectedIndex.value = 5
                    et5Code.value = hiden[4].toString()
                }
                6 -> {
                    curSelectedIndex.value = 6
                    et6Code.value = hiden[5].toString()
                    KeyboardUtils.hideSoftInput(mBinding.editTextSixthCode)

                    //根据状态处理Code
                    handleCodebyStatus(hiden)
                }
            }

        }

        //上面6个输入框的焦点获取
        val onCodeETFocusGet: TextView.() -> Unit = {
            setHidenFocus()
            KeyboardUtils.showSoftkeyboard(mActivity)
        }

        //当按键Del触发的事件
        val onKeyDelEvent: () -> Unit = {
            val curCode = mBinding.editTextHidden.text.toString()
            when (curCode.length) {
                6 -> et6Code.value = ""
                5 -> et5Code.value = ""
                4 -> et4Code.value = ""
                3 -> et3Code.value = ""
                2 -> et2Code.value = ""
                1 -> et1Code.value = ""
            }
            if (curCode.isNotEmpty()) {
                mBinding.editTextHidden.setText(curCode.subSequence(0, curCode.length - 1))
                mBinding.editTextHidden.setSelection(mBinding.editTextHidden.text.length)
            }

        }

        //设置隐藏的Code框焦点
        private fun setHidenFocus() {
            mBinding.editTextHidden.isFocusable = true
            mBinding.editTextHidden.isFocusableInTouchMode = true
            mBinding.editTextHidden.requestFocus()
            mBinding.editTextHidden.setSelection(mBinding.editTextHidden.text.length)
        }

        fun resetETs() {
            curSelectedIndex.value = 0
            et1Code.value = ""
            et2Code.value = ""
            et3Code.value = ""
            et4Code.value = ""
            et5Code.value = ""
            et6Code.value = ""
            mBinding.editTextHidden.setText("")
        }
    }

}