using UnityEngine;
using UnityEngine.UI;
using TMPro;

namespace GameSDK
{
    /// <summary>
    /// 修改密码面板
    /// </summary>
    public class ChangePasswordPanel : BasePanel
    {
        [Header("UI References")]
        public TMP_InputField usernameInput;
        public TMP_InputField oldPasswordInput;
        public TMP_InputField newPasswordInput;
        public TMP_InputField confirmPasswordInput;
        public Button confirmBtn;
        public Button backBtn;
        public TMP_Text tipText;

        private bool initialized;

        private void OnEnable()
        {
            if (initialized) return;
            initialized = true;
            confirmBtn.onClick.AddListener(OnConfirmClick);
            backBtn.onClick.AddListener(OnBackClick);
        }

        private void OnDisable()
        {
            if (!initialized) return;
            initialized = false;
            confirmBtn.onClick.RemoveListener(OnConfirmClick);
            backBtn.onClick.RemoveListener(OnBackClick);
        }

        private void OnDestroy()
        {
            initialized = false;
            if (confirmBtn != null) confirmBtn.onClick.RemoveListener(OnConfirmClick);
            if (backBtn != null) backBtn.onClick.RemoveListener(OnBackClick);
        }

        private void OnConfirmClick()
        {
            string username = usernameInput.text.Trim();
            string oldPassword = oldPasswordInput.text.Trim();
            string newPassword = newPasswordInput.text.Trim();
            string confirmPassword = confirmPasswordInput.text.Trim();

            if (string.IsNullOrEmpty(username))
            {
                ShowTip("请输入账号");
                return;
            }
            if (string.IsNullOrEmpty(oldPassword))
            {
                ShowTip("请输入原密码");
                return;
            }
            if (string.IsNullOrEmpty(newPassword))
            {
                ShowTip("请输入新密码");
                return;
            }
            if (newPassword.Length < 6)
            {
                ShowTip("新密码长度不能少于6位");
                return;
            }
            if (newPassword != confirmPassword)
            {
                ShowTip("两次输入的新密码不一致");
                return;
            }
            if (oldPassword == newPassword)
            {
                ShowTip("新密码不能与旧密码相同");
                return;
            }

            // TODO: 接入实际修改密码接口
            Debug.Log($"[SDK] 修改密码请求: 账号={username}");
            ShowTip("提交中...");

            // 模拟成功
            Debug.Log("[SDK] 密码修改成功");
            SDKUIManager.Instance.PopPanel();
        }

        private void OnBackClick()
        {
            SDKUIManager.Instance.PopPanel();
        }

        private void ShowTip(string msg)
        {
            tipText.text = msg;
        }

        public override void OnEnter()
        {
            base.OnEnter();
            usernameInput.text = "";
            oldPasswordInput.text = "";
            newPasswordInput.text = "";
            confirmPasswordInput.text = "";
            tipText.text = "";
        }
    }
}
