using FL.Data;
using FL.FGUI;
using XGame;
using XGame.Framework.Nodes;
using XGame.Framework.Time;


namespace FL.Nodes.DragonEgg
{
    // 属性比较枚举
    public enum eCompareType
    {
        None = 0,
        Low = 1,   // 红色箭头(属性比原来低)
        Higth = 2, // 绿色向上箭头(属性比原来的高)
        New = 3,   // 新的属性
    }

    public class UIComponent : NodeComponent
    {
        private ITimer _stepTimer; // 升阶倒计时定时器

        public override void OnEnable(object intent)
        {
            EventSingle.Instance.AddListener(EventDefine.UpgradeDragonEgg, UpgradeDragonEggEndTime);
            EventSingle.Instance.AddListener(EventDefine.ReduceDragonEggTime, UpgradeDragonEggEndTime);
            EventSingle.Instance.AddListener(EventDefine.UpgradeDragonEggSucess, UpgradeDragonEggSucess);
            EventSingle.Instance.AddListener(EventDefine.DropEquip, DropEquip);
        }
        public override void OnDisable()
        {
            EventSingle.Instance.RemoveListener(EventDefine.UpgradeDragonEgg, UpgradeDragonEggEndTime);
            EventSingle.Instance.RemoveListener(EventDefine.ReduceDragonEggTime, UpgradeDragonEggEndTime);
            EventSingle.Instance.RemoveListener(EventDefine.UpgradeDragonEggSucess, UpgradeDragonEggSucess);
            EventSingle.Instance.AddListener(EventDefine.DropEquip, DropEquip);
        }


        /// <summary>
        /// 龙蛋升阶时间变化,开启升级成功时间定时器
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        private void UpgradeDragonEggEndTime(int eventId, object args)
        {
            int delayTime = (int)(DragonEggData.Instance.UpgradeEndTime - Context.Time.GetNowTime());
            if (_stepTimer != null)
            {
                ClearStepTimer();
            }
            _stepTimer = Context.Time.AddDelayTimer(delayTime, () =>
            {
                Log.Debug("龙蛋升阶时间结束");
                ClearStepTimer();
            });
        }
        private void ClearStepTimer()
        {
            if (_stepTimer != null)
            {
                _stepTimer.Cancel();
                _stepTimer = null;
            }
        }
        /// <summary>
        /// 龙蛋升阶成功消息事件
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        private void UpgradeDragonEggSucess(int eventId, object args)
        {
            ClearStepTimer();
            Log.Debug("龙蛋升阶时间结束,升阶成功");
        }

        /// <summary>
        /// 龙裔宝卵掉落装备
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="args"></param>
        private void DropEquip(int eventId, object args)
        {
            var _uiParam = new EquipParam();
            _uiParam.id = (long)args;
            Context.UI.OpenAsync(UIKeys.DragonEggEquip, _uiParam);
        }

    }
}