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

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

namespace NBF
{
    public partial class CommonSubMenu : GComponent
    {
        public event Action<int> OnTabChange;

        private string _leftActionName;
        private string _rightActionName;

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

            // CommonSubMenu
        }

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

        public override void Dispose()
        {
            base.Dispose();
        }

        public void SetTabs(List<TabItemData> subItems, int selectIndex = 0)
        {
            List.RemoveChildrenToPool();
            var width = 0f;
            for (int i = 0; i < subItems.Count; i++)
            {
                var tabData = subItems[i];
                var tabItem = List.AddItemFromPool(BtnSubMenu.URL).asButton;//i == 0 ? BtnSubMenuLeft.URL : 
                tabItem.SetLanguage(tabData.Key);

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

            Log.Info($"Set tab index={List.selectedIndex}");
            List.selectedIndex = selectIndex;
            List.width = width;
            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();
        }
    }
}