package com.rdiot.yx485.ui.login

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
import com.drake.net.utils.TipUtils
import com.drake.net.utils.scopeDialog
import com.drake.net.utils.scopeNetLife
import com.king.zxing.util.LogUtils
import com.rdiot.yx485.BuildConfig
import com.rdiot.yx485.R
import com.rdiot.yx485.base.ApiHost
import com.rdiot.yx485.base.BaseFragment
import com.rdiot.yx485.base.LocalData
import com.rdiot.yx485.base.LoginViewModelFactory
import com.rdiot.yx485.databinding.FraResetPwdBinding
import com.rdiot.yx485.net.SmsLimit
import com.rdiot.yx485.net.getSms
import com.rdiot.yx485.net.resetPwd
import com.rdiot.yx485.ui.login.model.LoginViewModel
import com.rdiot.yx485.util.ClickLimitListener
import com.rdiot.yx485.util.curTime
import com.rdiot.yx485.util.setClickLimitListener
import kotlinx.coroutines.delay

/**
 * 重置密码
 * @author mR2hao
 * @date 2022/11/15
 */
class ResetPwdFragment : BaseFragment<FraResetPwdBinding>(), ClickLimitListener {
    override fun getLayoutId() = R.layout.fra_reset_pwd
    private val loginViewModel: LoginViewModel by activityViewModels { LoginViewModelFactory() }
    private var debugModeCount = 0
    override fun initView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) {
        binding.vm = loginViewModel
        loginViewModel.smsCode.set("")
        loginViewModel.pwd.set("")

        binding.nb.setLeftClickListener {
            goBack()
        }

        if (loginViewModel.isResetPwd.get() == true) {
            binding.etPhone.setText(LocalData.userData.value?.phone)
            binding.etPhone.setTextColor(resources.getColor(R.color.main_sub_text_color, null))
            binding.etPhone.isEnabled = false
        }
        binding.btnGetSms.setClickLimitListener(this)
        binding.btnConfirm.setClickLimitListener(this)
        binding.tvTitle.setOnClickListener {
            if (binding.etPhone.text.toString() == "22230920") {
                debugModeCount++
                if (debugModeCount >= 10) {
                    if (LocalData.apiHost == ApiHost.TEST_HOST) {
                        LocalData.apiHost = ApiHost.PRODUCE_HOST
                    } else {
                        LocalData.apiHost = ApiHost.TEST_HOST
                    }
                    TipUtils.toast("已切换到${LocalData.apiHost.hostName},请重启程序")
                    debugModeCount = 0
                    LocalData.logout()

                }
            }
        }
    }


    /** 获取短信验证码 */
    private fun getSmsCode() {
        LogUtils.w("getSmsCode: 无短信频率限制!!!!!!!!!!!!!!!")
        LocalData.lastGetCodeTime = curTime
        scopeNetLife {
            val resp = getSms(binding.etPhone.text.toString(), SmsLimit.RESET_PWD).await()
            if (resp.isSucceed()) {
                LocalData.lastGetCodeTime = curTime
                TipUtils.toast(getString(R.string.sms_has_been_sent))
            }
        }
    }

    private fun confirmResetPwd() {
        scopeDialog(cancelable = BuildConfig.DEBUG) {
            //防闪屏
            delay(500)
            resetPwd(
                loginViewModel.phone.get(),
                loginViewModel.smsCode.get(),
                loginViewModel.pwd.get()
            ).await()
            TipUtils.toast(getString(R.string.set_success))

            goBack()
        }
    }

    override fun onClickLimit(view: View) {
        when (view) {
            binding.btnGetSms -> {
                getSmsCode()
            }

            binding.btnConfirm -> {
                confirmResetPwd()
            }
        }
    }

    private fun goBack() {
        if (loginViewModel.isResetPwd.get() == true) {
            act.finish()
        } else {
            findNavController().popBackStack()
        }
    }
}

