package com.hongyegroup.cpt_auth.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
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.countDown
import com.guadou.lib_baselib.utils.CommUtils
import com.guadou.lib_baselib.utils.KeyboardUtils
import com.hongyegroup.cpt_auth.BR
import com.hongyegroup.cpt_auth.R
import com.hongyegroup.cpt_auth.databinding.ActivityUserRegisterBinding
import com.hongyegroup.cpt_auth.mvvm.vm.UserRegisterViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancel

/**
 * 用户注册页面
 */
@AndroidEntryPoint
class UserRegisterActivity : YYBaseVDBActivity<UserRegisterViewModel, ActivityUserRegisterBinding>() {

    private var mCountDownScope: CoroutineScope? = null

    companion object {
        fun startInstance() {
            val context = CommUtils.getContext()
            val intent = Intent(context, UserRegisterActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
    }

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

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

        initListener()
    }


    private fun initListener() {
        mBinding.cbPsd.setOnCheckedChangeListener { _, isChecked ->
            //设置密码隐藏与显示
            mBinding.etPassword.transformationMethod =
                if (isChecked) HideReturnsTransformationMethod.getInstance() else PasswordTransformationMethod.getInstance()

        }
    }

    override fun startObserve() {

    }

    //开始倒计时
    @ExperimentalCoroutinesApi
    @SuppressLint("SetTextI18n")
    private fun startCountDown() {

        countDown(59, start = {
            mCountDownScope = it
        }, end = {
            mBinding.tvGetCode.apply {
                isEnabled = true
                text = "获取验证码"
            }
        }, next = {
            mBinding.tvGetCode.apply {
                isEnabled = false
                text = "重新获取($it)"
            }
        })
    }

    override fun onDestroy() {
        mCountDownScope?.cancel()
        super.onDestroy()
    }

    /**
     * DataBinding事件处理
     */

    inner class ClickProxy {

        //获取验证码
        @ExperimentalCoroutinesApi
        fun getVerityCode() {
            KeyboardUtils.hideSoftInput(mActivity)
            toast("获取验证码")
            startCountDown()
        }

        //执行注册
        fun doRegist() {
            KeyboardUtils.hideSoftInput(mActivity)
            val phone = mViewModel.mPhoneNumLD.value
            val verityCode = mViewModel.mVerityCodeLD.value
            val password = mViewModel.mPasswordLD.value
            val inviteCode = mViewModel.mInviteCodeLD.value

            toast("params phone:$phone verityCode:$verityCode password:$password inviteCode:$inviteCode")
        }

    }

}