using System;
using System.Linq;
using DG.Tweening;
using FGUI.Common_01;
using FGUI.Game_04;
using IgnoreOPS;
using Spine.Unity;

namespace ChillConnect
{
    public class OpenGameUI : BaseUI
    {
        private OpenGameUICtrl ctrl;
        private OpenGameModel model;
        private com_open_tips ui;
        private Action closeCallback;

        public OpenGameUI(OpenGameUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.OpenGameUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Common_01";
            uiInfo.assetName = "com_open_tips";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期
        protected override void OnInit()
        {
            // model = ModuleManager.Instance.GetModel(ModelConst.OpenGameModel) as OpenGameModel;
        }

        protected override void OnClose()
        {

        }

        protected override void OnBind()
        {
            ui = baseUI as com_open_tips;
        }

        protected override void OnOpenBefore(object args)
        {
            try
            {
                // 添加空值检查，防止 NullReferenceException
                var makeupTaskHistory = DataMgr.MakeupTaskHistory?.Value;
                if (makeupTaskHistory != null && makeupTaskHistory.Any())
                {
                    var makeupTaskData = makeupTaskHistory.Last();
                    var config = ConfigSystem.GetConfig<MakeupModel>();
                    if (config != null)
                    {
                        var vo = config.GetData(makeupTaskData.tableId);
                        if (vo != null && ui?.text_level_limit != null)
                        {
                            var stage= vo.levels_need - GameHelper.GetLevel();
                            if(stage <= 0)
                            {
                                stage = 1;
                            }
                            ui.text_level_limit.SetVar("x", stage.ToString()).FlushVars();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
           

            InitView();
            
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion

        //初始化页面逻辑`
        private void InitView()
        {

            
            ui.t0.Play(() =>
            {
                DOVirtual.DelayedCall(0.2f, () =>
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);
                });
            });
            
        }
    }
}