// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖 

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

namespace NBF
{
    public partial class CommonMenu : GLabel
    {
        public event Action<int> OnTabChange;

        private void OnInited()
        {
            List.onClickItem.Add(OnClickItem);
            // BtnPrev.onClick.Add(OnClickBtnPrev);
            // BtnNext.onClick.Add(OnClickBtnNext);
        }

        private string _leftActionName;
        private string _rightActionName;

        public void SetBtnActionName(string leftActionName, string rightActionName)
        {
            _leftActionName = leftActionName;
            _rightActionName = rightActionName;
            // BtnPrev.SetData(leftActionName, string.Empty);
            // BtnNext.SetData(rightActionName, string.Empty);
        }

        public void SetTabs(List<TabItemData> tabList, int selectIndex = 0)
        {
            SetBtnActionName(InputDef.UI.Prev, InputDef.UI.Next);
            List.RemoveChildrenToPool();
            var listWidth = 0f;
            for (int i = 0; i < tabList.Count; i++)
            {
                var tabData = tabList[i];
                var tabItem = List.AddItemFromPool().asButton;
                tabItem.SetLanguage(tabData.Key);

                listWidth += tabItem.width;
                if (i > 0)
                {
                    listWidth += List.columnGap;
                }
            }

            Log.Info($"Set tab index={List.selectedIndex}");
            List.selectedIndex = selectIndex;
            List.width = listWidth;
            OnClickItem();
        }


        private void OnClickItem()
        {
            OnTabChange?.Invoke(List.selectedIndex);
        }

        public void OnClickBtnPrev()
        {
            if (List.selectedIndex > 0)
            {
                List.selectedIndex -= 1;
            }
            else
            {
                List.selectedIndex = List.numItems - 1;
            }

            OnClickItem();
        }

        public void OnClickBtnNext()
        {
            if (List.selectedIndex < List.numItems - 1)
            {
                List.selectedIndex += 1;
            }
            else
            {
                List.selectedIndex = 0;
            }

            OnClickItem();
        }
    }
}