package com.rdiot.yx485.ui.login

import android.os.Bundle
import com.drake.net.utils.TipUtils
import com.drake.net.utils.scopeDialog
import com.gyf.immersionbar.ktx.immersionBar
import com.rdiot.yx485.BuildConfig
import com.rdiot.yx485.R
import com.rdiot.yx485.base.BaseActivity
import com.rdiot.yx485.base.LocalData
import com.rdiot.yx485.databinding.ActSetPwdBinding
import com.rdiot.yx485.net.setPwd
import com.rdiot.yx485.util.setClickLimitListener

/**
 * 设置密码
 * @author mR2hao
 * @date 2022/12/13
 */
class SetPwdActivity : BaseActivity<ActSetPwdBinding>() {
    override fun getLayoutId() = R.layout.act_set_pwd

    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            statusBarView(binding.v)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.main_bg_color)
            statusBarDarkFont(true)
        }

        binding.nb.setLeftClickListener {
            finish()
        }

        binding.btnConfirm.setClickLimitListener {
            val pwdLength = binding.etPwd.text?.length ?: 0
            if (pwdLength < 6) {
                TipUtils.toast(getString(R.string.set_6_pwd))
                return@setClickLimitListener
            } else {
                if (binding.etPwd.text.toString() != binding.etConfirmPwd.text.toString()) {
                    TipUtils.toast(getString(R.string.pwd_dont_match))
                    return@setClickLimitListener
                }
            }

            scopeDialog(cancelable = BuildConfig.DEBUG) {
                setPwd(binding.etPwd.text.toString()).await()
                TipUtils.toast(getString(R.string.set_success))
                LocalData.userData.value?.apply {
                    setPwd = true
                    LocalData.userData.postValue(this)
                }

                finish()
            }
        }
    }
}