package com.guadou.cs_cptservices.popup

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.FragmentActivity
import com.guadou.cs_cptservices.BR
import com.guadou.cs_cptservices.R
import com.guadou.cs_cptservices.databinding.PopupPaymentPasswordBinding
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.KeyboardUtils
import com.guadou.lib_baselib.utils.log.YYLogUtils
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import kotlinx.coroutines.flow.MutableStateFlow

/**
 * 校验支付密码的弹窗
 */
@SuppressLint("ViewConstructor")
class PaymentPasswordPopup(
    private val activity: FragmentActivity,
    private val onCodeEntered: (String) -> Unit
) : CenterPopupView(activity) {

    private lateinit var mBinding: PopupPaymentPasswordBinding
    private val mClickProxy by lazy { ClickProxy() }

    //我需要使用DataBinding，所以我没有使用getImplLayoutId，我直接使用addInnerContent添加布局的时候绑定DataBinding
    override fun addInnerContent() {

        mBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.popup_payment_password, centerPopupContainer, false)
        mBinding.setVariable(BR.click, mClickProxy)
        mBinding.lifecycleOwner = activity

        contentView = mBinding.root
        val params = contentView.layoutParams as LayoutParams
        params.gravity = Gravity.CENTER
        centerPopupContainer.addView(contentView, params)
    }

    override fun getMaxWidth(): Int {
        return XPopupUtils.getWindowWidth(CommUtils.getContext())
    }

    override fun onCreate() {
        super.onCreate()

        setupTextWithStatus()
    }

    private fun setupTextWithStatus() {
        //默认设置第一个选择的背景和对话框
        mClickProxy.curSelectedIndex.value = 0

        //需要输入密码的类似，是支付还是提现？
    }

    override fun onDismiss() {
        KeyboardUtils.hideSoftInput(mBinding.editTextHidden)
        super.onDismiss()
    }

    /**
     * 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 ->
            YYLogUtils.w("当前的值：" + 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.editTextHidden)

                    onCodeEntered(hiden)

                    dismiss()
                }
            }

        }

        //上面6个输入框的焦点获取
        val onCodeETFocusGet: TextView.() -> Unit = {
            YYLogUtils.w("焦点获取")
            setHidenFocus()
            KeyboardUtils.showSoftInput(activity, mBinding.editTextHidden)
        }

        //当按键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 dismissPopup() {
            dismiss()
        }

    }

}