
using UnityEngine;

using Newtonsoft.Json;
using BingoBrain.Core;
using BingoBrain;
using DG.Tweening;
using BingoBrain.HotFix;
using DontConfuse;
using System;


public class LoginSystem : BaseSystem
{

    //is debug test (LoginCountLimit 应该为 5， 测试改为2)
    private const int LoginCountLimit = 5;
    private int loginCount = 0;
    public LoginSystem(bool isAutoInit = true)
    {
        if (isAutoInit)
        {
            Init();
        }
    }

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

        AddListener();
    }

    private void InitData()
    {
    }

    private void AddListener()
    {
     //   NetworkDispatcher.Instance.AddListener(NetworkMsg.Login, OnRequestLogin);
      //  NetworkDispatcher.Instance.AddListener(NetworkMsg.Reconnect, OnResponseReconnect);
    }

    private void RemoveListener()
    {
      //  NetworkDispatcher.Instance.RemoveListener(NetworkMsg.Login, OnRequestLogin);

      //  NetworkDispatcher.Instance.RemoveListener(NetworkMsg.Reconnect, OnResponseReconnect);
    }


    private void RequestLogin()
    {
        var requestLoginData = new RequestLoginData
        {
            device_id = SystemInfo.deviceUniqueIdentifier,
          //  device_id ="E742FE8B-C32E-56A7-8F8A-6B3BC0F3169",
            pack_name = NetworkMsg.Identifier,
            app_version = Application.version,
            channel = BingoBea.Instance.attribution,
            sim = WebviewManager.haveSimCard
        };

        Debug.Log(SystemInfo.deviceUniqueIdentifier);
        GameHelper.PostFunnelLogin("loginSend");
        Debug.Log(JsonConvert.SerializeObject(requestLoginData));
       //zhushi // NetworkKit.Post<LoginModel>("login", requestLoginData, (isSuccess, loginData) =>
        // {


        //     if (isSuccess)
        //     {
        //         var loginModel = GameHelper.GetLoginModel();
        //         loginModel.cdn_url = loginData.cdn_url;
        //         loginModel.setting = loginData.setting;
        //         loginModel.play_data = loginData.play_data;
        //         loginModel.token = loginData.token;
        //         loginModel.uid = loginData.uid;
        //         loginModel.country = loginData.country;
        //         loginModel.expires_at = loginData.expires_at;
        //         loginModel.is_magic = loginData.is_magic;
        //         loginModel.invite_code = loginData.invite_code;
        //         loginModel.invite_url = loginData.invite_url;
        //         loginModel.last_login_time = loginData.last_login_time;
        //         loginModel.login_time = loginData.login_time;
        //         loginModel.reg_time = loginData.reg_time;
        //         loginModel.new_player = loginData.new_player;
        //         loginModel.debug_log = loginData.debug_log;
        //         loginModel.enwp = loginData.enwp;


        //         loginModel.preferences = new Preferences();
        //         NetworkKit.CDNUrl = $"{loginData.cdn_url}/";
        //         NetworkKit.SetCacheToken(loginData.token);
        //         //RequestHeart();
        //         DateTimeBoardk.Instance.SetServerCurrTimestamp(loginData.login_time);
        //         Sequence mLoopSequence = DOTween.Sequence();
        //         mLoopSequence.AppendCallback(() =>
        //         {
        //             RequestHeart();

        //         }).AppendInterval(60).SetLoops(-1);
        //         //TimerHelper.UnscaleGeneral.AddLoopTimer(60, (timer) => { RequestHeart(); });
        //         //NetworkDispatcher.Instance.Dispatch(NetworkMsg.GetPlayData);
        //         //UICtrlDispatcher.Instance.Dispatch(SkinInfo.EnterBingoUI_Open);
        //         PreferencesMgr.Instance.PlayerName = loginData.invite_code;
        //         NetworkDispatcher.Instance.Dispatch(ExternalInfo.GetConfig);
        //         MaxADKit.Init();
        //         GameHelper.PostFunnelLogin("loginRecv", true); 
        //         if (GameHelper.IsGiftSwitch())
        //         {
        //             GameObject.Find("MainCameraRoot").SetActive(false);
        //         }
        //         else{
        //              GameObject.Find("sheep_parent").SetActive(false);
        //         }
        //     }
        //     else
        //     {
        //         //UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NetLoadingUI_Close);
        //         Debug.Log("登陆请求失败");
        //         GameHelper.PostFunnelLogin("loginRecv", false);

        //         float times = loginCount == 0 ? 0 : 5f;

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


    private void RequestHeart()
    {
        NetworkKit.PostWithHeader("user/health");
    }


    private void OnRequestLogin(object obj = null)
    {
        RequestLogin();
    }


    private void OnResponseReconnect(object obj)
    {
    }

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

public partial class NetworkMsg
{
    private static uint MsgRootLogic = 100000;

    public static uint GetConfig = ++MsgRootLogic;

    public static uint Reconnect = ++MsgRootLogic;

    public static uint Login = ++MsgRootLogic;

    public static uint SavePlayData = ++MsgRootLogic;

    public static uint GetPlayData = ++MsgRootLogic;

    public static uint SendInviteCode = ++MsgRootLogic;

    public static uint GetInviteData = ++MsgRootLogic;

    public static uint UpdateCheckInviteData = ++MsgRootLogic;

    public static string Identifier = "com.interactivegames123.bingograssland";
    public static uint Start = ++MsgRootLogic;
    public static uint NotNetwork = ++MsgRootLogic;
}
public class RequestLoginData
{
    public string device_id;
    public string pack_name;
    public string app_version;
    public string channel;
    public bool sim;
}