package com.example.defenseapplication.personal

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.defenseapplication.R
import com.example.defenseapplication.bean.VerifyOptionPasswordRequest
import com.example.defenseapplication.databinding.PasswordManagementActivityBinding
import com.example.defenseapplication.utils.RetrofitNetwork
import com.gyf.immersionbar.ImmersionBar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
//密码管理
class PasswordManagementActivity : AppCompatActivity() {

    private lateinit var binding: PasswordManagementActivityBinding
    private lateinit var passwordDialog: PasswordInputDialog

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

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

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.loginPassword.setOnClickListener {
            startActivity(Intent(this, LoginPasswordActivity::class.java))
        }

        binding.retrievePassword.setOnClickListener {
            startActivity(Intent(this, RetrievePasswordActivity::class.java))
        }

        binding.operationPassword.setOnClickListener {
            showPasswordDialog()
        }
    }

    private fun showPasswordDialog() {
        passwordDialog = PasswordInputDialog(this).apply {
            setOnConfirmListener { inputPassword ->
                CoroutineScope(Dispatchers.IO).launch {
                    try {
                        val response = RetrofitNetwork.getNetworkService().verifyOptionPassword(VerifyOptionPasswordRequest(inputPassword))
                        withContext(Dispatchers.Main) {
                            if (response.code == 200) {
                                startActivity(Intent(this@PasswordManagementActivity, OperationPasswordActivity::class.java))
                                dismiss()
                            } else {
                                Toast.makeText(this@PasswordManagementActivity, response.msg ?: "校验失败", Toast.LENGTH_SHORT).show()
                                showError(getString(R.string.password_error))
                                clearPassword()
                            }
                        }
                    } catch (e: Exception) {
                        withContext(Dispatchers.Main) {
                            Toast.makeText(this@PasswordManagementActivity, "网络请求失败", Toast.LENGTH_SHORT).show()
                            showError(getString(R.string.password_error))
                            clearPassword()
                        }
                    }
                }
            }
            setOnDismissListener {
                // Dialog dismissed
            }
        }
        passwordDialog.show()
    }

    override fun onDestroy() {
        if (::passwordDialog.isInitialized) {
            passwordDialog.dismiss()
        }
        super.onDestroy()
    }
}