using System;
using SGModule.Net;
using SGModule.NetKit;

namespace ChillConnect
{
    public class LoginSystem : BaseSystem
    {

        private int loginCount = 0;
        public LoginSystem(bool isAutoInit = true)
        {
            if (isAutoInit)
            {
                Init();
            }
        }

        public static string attribution = "";

        public sealed override void Init()
        {
            base.Init();
            InitData();
            AddListener();
        }

        private void InitData()
        {
        }

        private void AddListener()
        {
            NetworkDispatcher.Instance.AddListener(NetworkMsg.Login, RequestLogin);
        }

        private void RemoveListener()
        {
            NetworkDispatcher.Instance.RemoveListener(NetworkMsg.Login, RequestLogin);
        }

        private TimerTask timerTask = null;
        private void RequestLogin(object obj = null)
        {
            TrackKit.TrackLoginFunnel(LoginFunnelEventType.LoginSend);
            LoginKit.Instance.LoginRequest(SuperApplication.Instance.attribution, NetworkManager.haveSimCard, (isSuccess, loginData) =>
            {
                TrackKit.TrackLoginFunnel(LoginFunnelEventType.LoginRecv, isSuccess ? "success" : "fail");
                if (isSuccess)
                {

                    GameDispatcher.Instance.Dispatch(GameMsg.UpdateHotFixProgress, 97);

                    MaxADKit.SetUserID(loginData.Uid.ToString());

                    DateTimeManager.Instance.SetServerCurrTimestamp(loginData.LoginTime);

                    NetworkDispatcher.Instance.Dispatch(NetworkMsg.GetPlayData);
                }
                else
                {
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NetLoadingUI_Close);

                    void OnFail()
                    {
                        NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
                    }

                //    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.TipsViewUI_Open, (Action)OnFail);zhushi

                    // UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NetLoadingUI_Close);
                    //
                    // float times = loginCount == 0 ? 0 : 5f;
                    //
                    // DOVirtual.DelayedCall(times,()=>{
                    //     if (loginCount < 5) {
                    //         loginCount++;
                    //         RequestLogin();
                    //
                    //     } else {
                    //         loginCount = 0;
                    //         Action _OnFail = () =>
                    //         {
                    //             NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
                    //         };
                    //         UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.TipsViewUI_Open, _OnFail);
                    //     }
                    // });
                }
            });
        }

        public override void Dispose()
        {
            base.Dispose();
            RemoveListener();
        }
    }
}
