using UnityEngine;
using UnityEngine.UI;
using TMPro;

namespace GameSDK
{
    /// <summary>
    /// 实名认证面板
    /// </summary>
    public class RealNameAuthPanel : BasePanel
    {
        [Header("UI References")]
        public TMP_InputField realNameInput;
        public TMP_InputField idCardInput;
        public Button submitBtn;
        public Button skipBtn;
        public TMP_Text tipText;

        private bool initialized;

        private void OnEnable()
        {
            if (initialized) return;
            initialized = true;
            submitBtn.onClick.AddListener(OnSubmitClick);
            skipBtn.onClick.AddListener(OnSkipClick);
        }

        private void OnDisable()
        {
            if (!initialized) return;
            initialized = false;
            submitBtn.onClick.RemoveListener(OnSubmitClick);
            skipBtn.onClick.RemoveListener(OnSkipClick);
        }

        private void OnDestroy()
        {
            initialized = false;
            if (submitBtn != null) submitBtn.onClick.RemoveListener(OnSubmitClick);
            if (skipBtn != null) skipBtn.onClick.RemoveListener(OnSkipClick);
        }

        private void OnSubmitClick()
        {
            string realName = realNameInput.text.Trim();
            string idCard = idCardInput.text.Trim();

            if (string.IsNullOrEmpty(realName))
            {
                ShowTip("请输入真实姓名");
                return;
            }
            if (realName.Length < 2)
            {
                ShowTip("姓名格式不正确");
                return;
            }
            if (string.IsNullOrEmpty(idCard))
            {
                ShowTip("请输入身份证号码");
                return;
            }
            if (!ValidateIdCard(idCard))
            {
                ShowTip("身份证号码格式不正确");
                return;
            }

            // TODO: 接入实际实名认证接口
            Debug.Log($"[SDK] 实名认证请求: 姓名={realName}, 身份证={MaskIdCard(idCard)}");
            ShowTip("认证中...");

            // 模拟认证成功
            Debug.Log("[SDK] 实名认证成功");
            CloseSDK();
        }

        private void OnSkipClick()
        {
            Debug.Log("[SDK] 跳过实名认证");
            CloseSDK();
        }

        private void CloseSDK()
        {
            // 关闭SDK界面
            Debug.Log($"[SDK] 用户: {SDKManager.Instance.CurrentUser} 已进入游戏");
            // 实际项目中可销毁SDK UI的Canvas
            var canvas = GetComponentInParent<Canvas>();
            if (canvas != null) canvas.gameObject.SetActive(false);
        }

        private bool ValidateIdCard(string idCard)
        {
            if (idCard.Length != 18) return false;
            for (int i = 0; i < 17; i++)
            {
                if (!char.IsDigit(idCard[i])) return false;
            }
            char last = idCard[17];
            return char.IsDigit(last) || last == 'X' || last == 'x';
        }

        private string MaskIdCard(string idCard)
        {
            if (idCard.Length != 18) return idCard;
            return idCard.Substring(0, 4) + "**********" + idCard.Substring(14);
        }

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

        public override void OnEnter()
        {
            base.OnEnter();
            realNameInput.text = "";
            idCardInput.text = "";
            tipText.text = "";
        }
    }
}
