using ChillConnect;

namespace Assets.Scripts
{
    public class WindowSystem : BaseSystem
    {
        public WindowSystem(bool isAutoInit = true)
        {
            if (isAutoInit)
            {
                Init();
            }
        }

        TaskSequence startGameSequence = new TaskSequence();
        TaskSequence backMainSequence = new TaskSequence();

        public sealed override void Init()
        {
            base.Init();

            AddListener();
        }

        private void AddListener()
        {
            CtrlDispatcher.Instance.AddOnceListener(CtrlMsg.Game_Start, StartGame);
            GameDispatcher.Instance.AddListener(GameMsg.BackMainScene, BackMain);
        }

        private void StartGame(object obj)
        {
            startGameSequence.Clear();



            if (startGameSequence.Count == 0)
            {
                End();
            }
            else
            {
                startGameSequence.Run();
            }
        }


        private void End()
        {
        }

        private bool haveBackMain = false;

        private void BackMain(object obj)
        {
            if (!true)
            {
                return;
            }

            backMainSequence.Clear();
            haveBackMain = true;
            backMainSequence.Run();
        }

        private void AddRateUs(TaskSequence backMainSequence)
        {
            bool isAdd = 5 == DataMgr.GameOfCount.Value;

            backMainSequence.Add(isAdd, (procedure) =>
            {
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.RateUsDialogUI_Open);
                UICtrlDispatcher.Instance.AddOnceListener(UICtrlMsg.RateUsDialogUI_Close,
                    (e) => { procedure.InvokeComplete(); });
            });
        }
    }
}