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

using System.Collections.Generic;
using XGame.Framework.FGUI;
using XGame.Framework.UI;

namespace FL.FGUI
{
    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class CommonToastPanelCtrl : UIController<CommonToastPanelVM>
    {
        private const int TIPS_LIMIT = 5;
        private HashSet<CommonTipsItemView> _showingTips;
        private Stack<CommonTipsItemView> _unusedTips;
        protected override void OnEnable(object intent)
        {
            AddListenres();
        }
        protected override void OnDisable()
        {
            VM.KillTweens();
            RemoveListenres();
            ClearTips();
        }
        private void AddListenres()
        {
            EventSingle.Instance.AddListener(EventDefine.ShowTips, OnShowTips);
        }



        private void RemoveListenres()
        {
            EventSingle.Instance.RemoveListener(EventDefine.ShowTips, OnShowTips);
        }
        #region Tips
        private void OnShowTips(int eventId, object args)
        {
            var tips = (string)args;
            if (_unusedTips != null && _unusedTips.Count > 0)
            {
                var tipsItemView = _unusedTips.Pop();
                tipsItemView.Enable(null);
                ShowTips(tipsItemView, tips);
                return;
            }
            var async = Context.Asset.LoadFguiNested(UINestedKeys.CommonTipsItem, VM.ItemRoot);
            async.On(_ =>
            {
                var result = async.Result as CommonTipsItemView;
                if (result == null)
                {
                    return;
                }
                ShowTips(result, tips);
            });
        }
        private void ShowTips(CommonTipsItemView tipsItemView, string tips)
        {
            if (_showingTips == null)
            {
                _showingTips = new HashSet<CommonTipsItemView>();
            }
            _showingTips.Add(tipsItemView);
            tipsItemView.Ctrl.ShowTips(tips, () =>
            {
                tipsItemView.Disable();
                _showingTips.Remove(tipsItemView);
                if (_unusedTips == null)
                    _unusedTips = new Stack<CommonTipsItemView>();
                _unusedTips.Push(tipsItemView);
            });
        }
        private void ClearTips()
        {
            if (_showingTips != null)
            {
                foreach (var item in _showingTips)
                {
                    Context.Asset.Recycle(item);
                }
                _showingTips.Clear();
            }
            if (_unusedTips != null)
            {
                while (_unusedTips.Count > 0)
                {
                    var item = _unusedTips.Pop();
                    Context.Asset.Recycle(item);
                }
            }
        }
        #endregion
    }
}