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

using System.Collections.Generic;
using UnityEngine;
using NBC;

namespace NBF
{
    public partial class CommonTopPanel : UIPanel
    {
        protected override void OnInit()
        {
            ContentPane.sortingOrder = UIDef.UIOrder.CommonTopPanel;
            IsShowCursor = false;
            Lan.Inst.ChangeAction += ChangeLanguageConfirm;
        }

        protected override void OnShow()
        {
            InputManager.OnUICanceled += OnUICanceled;
        }

        private void OnUICanceled(string action)
        {
            if (action == InputDef.UI.Back)
            {
                var panel = UI.Inst.GetUI<HomePanel>();
                if (panel != null && panel.IsShowing) return;
                HomePanel.Show();
            }
        }

        protected override void OnUpdate()
        {
            // base.OnUpdate();
            if (Input.GetKeyDown(KeyCode.F9))
            {
                PreviewPanel.Show();
            }
        }


        private void ChangeLanguageConfirm()
        {
            var uis = UI.Inst.GetAllUI();
            List<IUIPanel> panels = new List<IUIPanel>();
            foreach (var ui in uis)
            {
                if (!ui.IsShowing && !ui.IsDotDel)
                {
                    UI.Inst.DestroyUI(ui.GetType());
                }
                else
                {
                    panels.Add(ui);
                }
            }

            //打开的页面重刷多语言
            foreach (var panel in panels)
            {
                panel.SetLanguage();
                _ui.TrySetPanelLanguage(panel.ContentPane);
            }

            // Net.Inst.Send(EnmCmdValue.EcvCsAcSetLanguageReq, new CS_AC_SetLanguageReq()
            // {
            //     AppLanguage = Lan.Inst.GetCurrentCustomLanguageName()
            // });
        }

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

        protected override void OnDestroy()
        {
            Lan.Inst.ChangeAction -= ChangeLanguageConfirm;
        }
    }
}