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

namespace ChillConnect
{
    public class OpenGameUI : BaseUI
    {
        private OpenGameUICtrl ctrl;
        private OpenGameModel model;
        private com_open ui;
        private Action closeCallback;
        private bool need_show = true;

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

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Game_04";
            uiInfo.assetName = "com_open";
            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 com_open;
        }

        protected override void OnOpenBefore(object args)
        {
            if (args != null) need_show = (bool)args;
            string stage = GameHelper.gameType == 0 ? "Lv." + GameHelper.GetLevel() : "";
            ui.text_level.text = stage;

            InitView();

            DOVirtual.DelayedCall(0.7f, () => { GameDispatcher.Instance.Dispatch(GameMsg.reset_game, args); });

            if (AudioManager.Instance.IsOpenEffect)
            {
                AudioManager.Instance.PlayDynamicEffect(AudioConst.game_open);
            }
        }

        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()
        {

            
            // var sk = FXManager.Instance.SetFx<SkeletonAnimation>(ui.anim, Fx_Type.fx_open, ref closeCallback);
            // sk.state.SetAnimation(0, "animation", false);


            // DOVirtual.DelayedCall(2.8f, () =>
            // {
            //     ui.bg.visible  = false;
            // });
            ui.t0.Play();
            DOVirtual.DelayedCall(1.5f, () =>
            {
               // ui.anim.visible = false;
                if (need_show&&GameHelper.IsGiftSwitch()) {
                    if (GameHelper.IsShowLevelTips()) 
                    {
                      //  ui.tips_node.visible = false;
                        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);
                            };
                        });
                    } 
                    else 
                    {
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);    
                    }
                } else {
                    DOVirtual.DelayedCall(0.4f, () =>
                    {
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.OpenGameUI_Close);    
                    });
                }
            });
        }
    }
}