package com.miya.fastcashier.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.miya.fastcashier.databinding.ActivityResetAuthorizePasswordBinding
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.utils.manage.AccountPasswordManageKit

class ResetAuthorizePasswordActivity : BaseActivity() {

    private lateinit var viewBinding: ActivityResetAuthorizePasswordBinding

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val intent = Intent(context, ResetAuthorizePasswordActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityResetAuthorizePasswordBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        init()
    }

    private fun init() {
        viewBinding.llBack.clickWithTrigger { finish() }
        viewBinding.tvConfirm.clickWithTrigger { updateAuthorizePassword() }
    }

    private fun updateAuthorizePassword() {
        val originPassword = viewBinding.etOriginPassword.text.toString()
        val newPassword = viewBinding.etNewPassword.text.toString()
        val newConfirmPassword = viewBinding.etNewPasswordConfirm.text.toString()

        if (TextUtils.isEmpty(originPassword) or
            TextUtils.isEmpty(newPassword) or
            TextUtils.isEmpty(newConfirmPassword)
        ) {
            when {
                TextUtils.isEmpty(originPassword) -> CenterToasty.error(this, "请输入原始密码！").show()
                TextUtils.isEmpty(newPassword) -> CenterToasty.error(this, "请输入新密码！").show()
                TextUtils.isEmpty(newConfirmPassword) -> CenterToasty.error(this, "请输入确认密码！").show()
            }
            return
        }

        if (originPassword.length != 6) {
            CenterToasty.error(this, "请输入6位原始密码！").show()
            return
        }

        if (newPassword.length != 6) {
            CenterToasty.error(this, "请输入6位新密码！").show()
            return
        }

        if (newConfirmPassword.length != 6) {
            CenterToasty.error(this, "请输入6位确认密码！").show()
            return
        }

        if (newPassword.equals(originPassword)) {
            CenterToasty.error(this, "新密码与原密码不能相同！").show()
            return
        }

        if (!newPassword.equals(newConfirmPassword)) {
            CenterToasty.error(this, "新密码两次输入不一致！").show()
            return
        }

        val accountAuthorizePassword = AccountPasswordManageKit.get().accountPassword
        if (TextUtils.isEmpty(accountAuthorizePassword)) {
            AccountPasswordManageKit.get()
                .loadAccountPassword(
                    this,
                    object : AccountPasswordManageKit.AccountPasswordManageCallback {
                        override fun onPasswordGet(password: String?) {
                            if (!password.equals(originPassword)) {
                                CenterToasty.error(this@ResetAuthorizePasswordActivity, "原始密码输入错误！")
                                    .show()
                            } else {
                                updateAuthorizePasswordSucced(newPassword)
                            }
                        }

                    })
        } else {
            if (!accountAuthorizePassword.equals(originPassword)) {
                CenterToasty.error(this, "原始密码输入错误！").show()
            } else {
                updateAuthorizePasswordSucced(newPassword)
            }
        }
    }

    private fun updateAuthorizePasswordSucced(newPassword: String) {
        CenterToasty.success(this, "修改成功！", 1).show()
        AccountPasswordManageKit.get().updateAccountPassword(newPassword)
        finish()
    }
}