package com.example.defenseapplication.personal

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import android.widget.Toast

import com.bumptech.glide.Glide
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.PersonalProfileActivityBinding
import com.example.defenseapplication.view.CustomDialogFragmentText
import com.example.defenseapplication.view.PhoneModifyDialogFragment
import com.gyf.immersionbar.ImmersionBar
//个人信息
class PersonalProfileActivity : AppCompatActivity() {
    private lateinit var binding: PersonalProfileActivityBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = PersonalProfileActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()
        initView()
    }

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

        Glide.with(this)
            .load(R.drawable.img)
            .circleCrop()
            .into(binding.ivAvatar)

        binding.tvNickname.text = getString(R.string.nickname_default)
        binding.tvPhone.text = "13589898989"

        binding.itemNickname.setOnClickListener {
            showNicknameDialog()
        }

        binding.itemPhone.setOnClickListener {
            showPhoneDialog()
        }
    }

    private fun showNicknameDialog() {
        val currentNickname = binding.tvNickname.text.toString()
        val dialog = CustomDialogFragmentText()
            .setTitle(getString(R.string.set_name))
            .setMessage("")
            .setInputHint(getString(R.string.nickname_hint))
            .setInputText(currentNickname)
            .setShowInput(true)
            .setPositiveText(getString(R.string.confirm))
            .setNegativeText("")
            .setOnConfirmListener { newNickname ->
                if (newNickname.isNotBlank()) {
                    binding.tvNickname.text = newNickname
                    Toast.makeText(this, getString(R.string.operation_success), Toast.LENGTH_SHORT).show()
                }
            }
        dialog.show(supportFragmentManager, "nickname_dialog")
    }

    private fun showPhoneDialog() {
        val dialog = PhoneModifyDialogFragment()
            .setTitle(getString(R.string.modify_phone))
            .setNegativeText(getString(R.string.cancel))
            .setPositiveText(getString(R.string.verify_and_bind))
            .setOnConfirmListener { phone, code ->
                binding.tvPhone.text = phone
                Toast.makeText(this, getString(R.string.operation_success), Toast.LENGTH_SHORT).show()
            }
        dialog.show(supportFragmentManager, "phone_dialog")
    }
}
