package com.example.defenseapplication.personal

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import bean.LoginRequest
import com.example.defenseapplication.R
import com.example.defenseapplication.bean.UpdatePassword
import com.example.defenseapplication.bean.UserInfoBean
import com.example.defenseapplication.databinding.LoginPasswordActivityBinding
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 LoginPasswordActivity : AppCompatActivity() {
    private lateinit var binding: LoginPasswordActivityBinding

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

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

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.btnConfirm.setOnClickListener {
            val oldPassword = binding.etOldPassword.text.toString().trim()
            val newPassword = binding.etNewPassword.text.toString().trim()
            val confirmPassword = binding.etConfirmPassword.text.toString().trim()

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

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

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

            CoroutineScope(Dispatchers.IO).launch {
                try {
                    val result = RetrofitNetwork.getNetworkService().updatePassword(UpdatePassword(oldPassword, newPassword))
                    withContext(Dispatchers.Main) {
                        if (result.code == 200) {
                            Toast.makeText(this@LoginPasswordActivity, getString(R.string.password_reset_success), Toast.LENGTH_SHORT).show()
                            finish()
                        } else {
                            Toast.makeText(this@LoginPasswordActivity, result.msg ?: getString(R.string.password_reset_failed), Toast.LENGTH_SHORT).show()
                        }
                    }
                } catch (e: Exception) {
                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@LoginPasswordActivity, getString(R.string.network_error), Toast.LENGTH_SHORT).show()
                    }
                }
            }
        }

    }
}
