using System;
using DG.Tweening;
using Spine.Unity;

namespace ScrewsMaster
{
    public class OpenGameUI : BaseUI
    {
        private OpenGameUICtrl ctrl;
        private OpenGameModel model;
        private FGUI.G003_play.com_opengame ui;

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

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

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

        protected override void OnClose()
        {

        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G003_play.com_opengame;
        }

        protected override void OnOpenBefore(object args)
        {
            // DOVirtual.DelayedCall(0.7f, () => { GameDispatcher.Instance.Dispatch(GameMsg.reset_game, args); });
            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 Action closeCallback;
        //初始化页面逻辑
        private void InitView()
        {
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.MaskUI_Open);
            ui.GoWordart.visible = ShowScrews.Instance.gameMode == 1;
            ui.LevelWordart.visible = ShowScrews.Instance.gameMode != 1;
            ui.lv_text.visible = ShowScrews.Instance.gameMode != 1;
            ui.lv_text.text = GameHelper.GetLevel().ToString();
            if (GameHelper.IsShowLevelTips())
            {
                DOVirtual.DelayedCall(0.2f, () =>
                {
                    ui.tips_node.visible = true;
                    var meteor1 = FXManager.Instance.SetFx<SkeletonAnimation>(ui.tips_node, Fx_Type.fx_tips, ref closeCallback);
                    meteor1.state.SetAnimation(0, "animation", false);
                    // meteor1.state.Complete += (a) =>
                    // {
                    //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);
                    // };
                });
            }
            // ui.t0.Play(() =>
            // {
            //     UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);
            // });
        }

        public void PlayAnim()
        {
            ui.t0.Play(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.MaskUI_Close);
            });
        }
    }
}