package com.miya.fastcashier.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import com.blankj.utilcode.util.ToastUtils
import com.miya.fastcashier.R
import com.miya.fastcashier.databinding.ActivityResetAuthorizePasswordBinding
import com.miya.fastcashier.databinding.ActivitySettingBinding
import com.miya.fastcashier.service.AccountService
import com.miya.fastcashier.ui.dialog.CommonDialog
import com.miya.fastcashier.utils.CenterToasty
import com.miya.fastcashier.utils.clickWithTrigger
import com.miya.fastcashier.utils.manage.AccountPasswordManageKit
import com.tencent.mmkv.MMKV

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(newConfirmPassword)) {
            CenterToasty.error(this, "新密码两次输入不一致！").show()
            return
        }

        val accountAuthorizePassword = AccountPasswordManageKit.getInstance().accountPassword
        if (TextUtils.isEmpty(accountAuthorizePassword)) {
            AccountPasswordManageKit.getInstance()
                .loadAccountPassword(this, AccountPasswordManageKit.AccountPasswordManageCallback {
                    if (!it.equals(originPassword)) {
                        CenterToasty.error(this, "原始密码输入错误！").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.getInstance().updateAccountPassword(newPassword)
        finish()
    }
}