/// #pkgName FGUI包名
/// #panelName UIPanel名字
/// #UIName = $"{#pkgName}{#panelName}" UIKey名字
/// 该脚本由模板创建
/// created by cb 2024

using FairyGUI;
using FL.Data;
using System.Collections.Generic;
using System.Linq;
using XGame.Database;
using XGame.Framework.UI;

namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class PartnerMainPanelCtrl : UIController<PartnerMainPanelVM>
    {
        private List<PartnerListItemData> _goneUpPartners = new();
        private List<PartnerListItemData> _allPartners = new();
        protected override void OnEnable(object intent)
        {
            AddUIListenres();
            AddListenres();
            InitLists();
            RefreshGoneUpPartners();
            RefreshAllPartners();
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            RemoveListenres();
            ClearLists();
        }
        #region UI事件
        private void AddUIListenres()
        {
            VM.OneKeyUpBtn.onClick.Add(OnClickOneKeyUpBtn);
            VM.OneKeyStrongBtn.onClick.Add(OnClickOneKeyStrongBtn);

        }
        private void RemoveUIListenres()
        {
            VM.OneKeyUpBtn.onClick.Remove(OnClickOneKeyUpBtn);
            VM.OneKeyStrongBtn.onClick.Remove(OnClickOneKeyStrongBtn);

        }
        private void OnClickOneKeyUpBtn(EventContext context)
        {
            XGame.Log.Info("OnClickOneKeyUpBtn.");
        }
        private void OnClickOneKeyStrongBtn(EventContext context)
        {
            XGame.Log.Info("OnClickOneKeyStrongBtn.");
        }

        #endregion
        #region 事件监听
        private void AddListenres()
        {
            EventSingle.Instance.AddListener(EventDefine.PartnerDown, OnPartnerDown);
            EventSingle.Instance.AddListener(EventDefine.PartnerUp, OnPartnerUp);
        }
        private void RemoveListenres()
        {
            EventSingle.Instance.RemoveListener(EventDefine.PartnerDown, OnPartnerDown);
            EventSingle.Instance.RemoveListener(EventDefine.PartnerUp, OnPartnerUp);
        }

        private void OnPartnerUp(int eventId, object args)
        {
            RefreshGoneUpPartners();
            RefreshAllPartners();
        }

        private void OnPartnerDown(int eventId, object args)
        {
            RefreshGoneUpPartners();
            RefreshAllPartners();
        }
        #endregion
        #region 随从列表
        private void InitLists()
        {
            VM.GoneUpList.ListType = EGListType.None;
            VM.ParentAllList.ListType = EGListType.Virtual;
        }
        private void ClearLists()
        {
            _goneUpPartners.Clear();
            _allPartners.Clear();
        }
        private void RefreshGoneUpPartners()
        {
            var goneUpUIDs = PartnersData.Instance.GoneUpMap.Keys.ToList();
            var partnersMap = PartnersData.Instance.PartnersMap;
            var count = goneUpUIDs.Count;
            if (_goneUpPartners.Count > count)
            {
                for (var i = 0; i < _goneUpPartners.Count - count; i++)
                {
                    _goneUpPartners.RemoveAt(0);
                }
            }
            for (var i = 0; i < count; i++)
            {
                var data = GetOrAddListItemData(_goneUpPartners, i);
                data.uid = goneUpUIDs[i];
                data.tableId = partnersMap[data.uid].TableId;
                data.state = 2;
                data.isShowUpMask = false;
            }
            VM.GoneUpList.BindDatas(_goneUpPartners);
        }
        private void RefreshAllPartners()
        {
            var tables = PartnerTableRepo.GetAll();
            var partnersMap = PartnersData.Instance.PartnersMap;
            var goneupMap = PartnersData.Instance.GoneUpMap;
            var index = 0;
            var owneds = new HashSet<int>();
            foreach (var partner in partnersMap.Values)
            {
                var uid = partner.UID;
                var isGoneUp = goneupMap.ContainsKey(uid);
                var data = GetOrAddListItemData(_allPartners, index++);
                data.tableId = partner.TableId;
                data.uid = uid;
                data.state = isGoneUp ? 2 : 1;
                data.isShowUpMask = isGoneUp;
                owneds.Add(partner.TableId);
            }
            foreach (var table in tables)
            {
                if (owneds.Contains(table.Id))
                    continue;
                var data = GetOrAddListItemData(_allPartners, index++);
                data.tableId = table.Id;
                data.uid = 0;
                data.state = 0;
                data.isShowUpMask = false;
            }
            _allPartners.Sort((a, b) =>
            {
                // 已上阵的排前面
                var result = b.state - a.state;
                if (result != 0)
                    return result;
                // id小的排前面
                return a.tableId - b.tableId;
            });
            VM.ParentAllList.BindDatas(_allPartners);
        }
        private PartnerListItemData GetOrAddListItemData(List<PartnerListItemData> datas, int index)
        {
            if (datas.Count > index)
            {
                return datas[index];
            }
            var data = new PartnerListItemData();
            datas.Add(data);
            return data;
        }
        #endregion
    }
}