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

using FairyGUI;
using FL.Data;
using System;
using XGame.Framework.UI;

namespace FL.FGUI
{
    public class UseItemParam
    {
        public int id;
        public int showNum;
        public int maxNum;   // -1表示无上限
        public long endTime; // 结束时间戳
        public Action<int> onPromiseCallback; // 确定按钮回调函数
        public Action onCancelCallback;  // 取消按钮回调函数
    }

    /// <summary>
    /// UI逻辑处理类
    /// </summary>
    /// <typeparam name=""></typeparam>
    public partial class CommonUseItemCtrl : UIController<CommonUseItemVM>
    {
        private bool _bPromiseCallback;
        private int _showCount;
        private long _openUIServerTime; // 打开此界面时所处的当前的时间戳(用于加速类道具打开此界面停留时间比较久时)
        private UseItemParam _useItemParam;
        private const int stayTime = 3000; // 停留时间超过30秒

        protected override void OnEnable(object intent)
        {
            _bPromiseCallback = false;
            AddUIListenres();
            AddEventListeners();
            if (intent != null)
            {
                _useItemParam = (UseItemParam)intent;
            }
            _openUIServerTime = Context.Time.GetNowTime();
            ShowItemUI();
            ShowUI();
        }
        protected override void OnDisable()
        {
            RemoveUIListenres();
            RemoveEventListeners();
            if (_bPromiseCallback)
            {
                if (_useItemParam?.onPromiseCallback != null) _useItemParam.onPromiseCallback(_showCount);
            }
            else
            {
                if (_useItemParam?.onCancelCallback != null) _useItemParam.onCancelCallback();
            }
            _bPromiseCallback = false;
            _showCount = 0;
            _openUIServerTime = 0;
            _useItemParam = null;
        }
        #region UI事件
        private void AddUIListenres()
        {
            VM.UseBtn.onClick.Add(OnClickUseBtn);
            VM.AddBtn.onClick.Add(OnClickAddBtn);
            VM.SubBtn.onClick.Add(OnClickSubBtn);
        }
        private void RemoveUIListenres()
        {
            VM.UseBtn.onClick.Remove(OnClickUseBtn);
            VM.AddBtn.onClick.Remove(OnClickAddBtn);
            VM.SubBtn.onClick.Remove(OnClickSubBtn);
        }
        private void OnClickUseBtn(EventContext context)
        {
            if (_useItemParam?.endTime > 0)
            {
                XGame.Log.Warn($"当前服务器时间:{Context.Time.GetNowTime()},_openUIServerTime:{_openUIServerTime}");
                if (Context.Time.GetNowTime() - _openUIServerTime > stayTime)
                {
                    var needNum = GetRealNeed();
                    if (needNum != _useItemParam.maxNum)
                    {
                        RefreshNeedTimeCard(needNum);
                        return;
                    }
                }
            }
            _bPromiseCallback = true;
            Context.ClosePanel();
        }
        private void OnClickAddBtn(EventContext context)
        {
            long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
            var count = _showCount + 1;
            if (count <= itemCount)
            {
                if (_useItemParam.maxNum > 0 && count <= _useItemParam.maxNum)
                {
                    _showCount = count;
                }
                ShowCount();
            }
        }
        private void OnClickSubBtn(EventContext context)
        {
            var count = _showCount - 1;
            if (count >= 0)
            {

                _showCount = count;
                ShowCount();
            }
        }

        #endregion

        #region 事件监听
        private void AddEventListeners()
        {

        }
        private void RemoveEventListeners()
        {

        }
        #endregion

        private void ShowItemUI()
        {
            UiParam _uiParam = new UiParam();
            _uiParam.bShowBtn = true;
            _uiParam.bShowName = true;
            _uiParam.bShowCount = true;
            VM.ItemBtn.Ctrl.ShowAnalogData(_useItemParam.id, _uiParam);
        }

        private void ShowUI()
        {
            long itemCount = ItemData.Instance.GetItemNum(_useItemParam.id); // 当前拥有的道具数量
            _showCount = itemCount < _useItemParam.showNum ? (int)itemCount : _useItemParam.showNum;
            ShowCount();
        }

        /// <summary>
        /// 显示数量按钮UI状态
        /// </summary>
        private void ShowBtnUI()
        {

        }

        private void ShowCount()
        {
            VM.NumLabel.text = _showCount.ToString();
        }

        // 获取实际需要的加速道具数量
        private int GetRealNeed()
        {
            var needTime = DragonEggData.Instance.UpgradeEndTime - Context.Time.GetNowTime();
            return DragonEggService.Instance.GetAccelerationCardNum(DragonEggData.Instance.AccelerateId, needTime);
        }

        //时间变化，刷新卡所需数量重置
        private void RefreshNeedTimeCard(int maxNum)
        {
            _useItemParam.maxNum = maxNum;
            _useItemParam.showNum = _useItemParam.maxNum > 1 ? _useItemParam.maxNum - 1 : 0;
            _openUIServerTime = Context.Time.GetNowTime();
            ShowUI();
        }
    }
}