package com.example.defenseapplication.personal

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.SPUtils
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.OperationPasswordActivityBinding
import com.gyf.immersionbar.ImmersionBar

//操作密码
class OperationPasswordActivity : AppCompatActivity() {

    private lateinit var binding: OperationPasswordActivityBinding

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

        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()

        initListener()
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.swOperationPassword.setOnCheckedChangeListener { _, isChecked ->
            when (isChecked) {
                true -> {
                    binding.etPassword.isEnabled = true
                    binding.etConfirmPassword.isEnabled = true
                    binding.btnConfirmReset.isEnabled = true
                    Toast.makeText(this, getString(R.string.operation_password_enabled), Toast.LENGTH_SHORT).show()
                }
                false -> {
                    binding.etPassword.isEnabled = false
                    binding.etConfirmPassword.isEnabled = false
                    binding.btnConfirmReset.isEnabled = false
                    SPUtils.getInstance().remove("operation_password")
                    Toast.makeText(this, getString(R.string.operation_password_disabled), Toast.LENGTH_SHORT).show()
                }
            }
        }

        binding.btnConfirmReset.setOnClickListener {
            val password = binding.etPassword.text.toString()
            val confirmPassword = binding.etConfirmPassword.text.toString()

            if (password.isEmpty()) {
                Toast.makeText(this, getString(R.string.password_cannot_empty), Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (password.length != 6) {
                Toast.makeText(this, getString(R.string.password_must_be_6_digits), Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (confirmPassword.isEmpty()) {
                Toast.makeText(this, getString(R.string.please_confirm_password), Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (password != confirmPassword) {
                Toast.makeText(this, getString(R.string.password_mismatch), Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            SPUtils.getInstance().put("operation_password", password)
            Toast.makeText(this, getString(R.string.operation_success), Toast.LENGTH_SHORT).show()
        }
    }
}