// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖

using System;
using FairyGUI;
using UnityEngine;
using NBC;
using UIPanel = NBC.UIPanel;

namespace NBF
{
    public partial class MessageBox : UIPanel
    {
        private static string _confirmText;
        private static string _cancelText;
        private static string _title;
        private static string _content;
        private static Action<bool> _callback;
        private static int _style;

        public static void Show(string msg, Action<bool> callback, string title = "", int style = 0,
            string confirm = "", string cancel = "")
        {
            if (string.IsNullOrEmpty(title))
            {
                title = "TEXT_NOTE";
            }

            if (string.IsNullOrEmpty(confirm))
            {
                confirm = "TEXT_CONFIRM";
            }

            if (string.IsNullOrEmpty(cancel))
            {
                cancel = "TEXT_CANCEL";
            }

            _confirmText = confirm;
            _cancelText = cancel;
            _callback = callback;
            _content = msg;
            _title = title;
            _style = style;
            Show();
        }

        protected override void OnInit()
        {
            this.AutoAddClick(OnClick);
            IsModal = true;
            HideAnim = UIPanelAnimation.GetCenterPopScaleFade(this, true);
            InputManager.OnUICanceled += OnUICanceled;
        }

        protected override void OnShow()
        {
            // TextTitle.SetLanguage(_title);
            BtnConfirm.SetLanguage(_confirmText);
            // BtnCancel.SetLanguage(_cancelText);
            // MessageStyle.selectedIndex = _style;
            TextTitle.SetLanguage(_content);
        }

        private void OnUICanceled(string action)
        {
            if (!IsShowing) return;
            if (!IsTop) return;
            if (action == InputDef.UI.Enter)
            {
                ClosePanel(true);
            }
            else if (action == InputDef.UI.Back)
            {
                ClosePanel(false);
            }
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            InputManager.OnUICanceled -= OnUICanceled;
        }


        private void OnClick(GComponent btn)
        {
            ClosePanel(btn == BtnConfirm);
        }

        private void ClosePanel(bool ret)
        {
            _callback?.Invoke(ret);
            Hide();
        }
    }
}