using System;
using System.Collections;
using System.Text;
using Steamworks;
using UnityEngine;
using Wizard;
using Wizard.Title;

namespace Cute;

public class Certification : MonoBehaviour
{
	public static bool CheckUrlScheme;

	private const int ERROR_CODE_ACCOUNT_REMOVED = 5607;

	private static string udid;

	private static int short_udid;

	private static string sessionId;

	private const float DELAY_TIME = 0.02f;

	protected Callback<GetAuthSessionTicketResponse_t> m_GetAuthSessionTicketResponse;

	public static string Udid
	{
		get
		{
			if (string.IsNullOrEmpty(udid))
			{
				udid = Cryptographer.decode(Toolbox.SavedataManager.GetString("UDID"));
			}
			return udid;
		}
	}

	public static int ViewerId
	{
		// Post-Task-8: strictly ambient. The historical SavedataManager-backed lazy decode was the
		// client process's single-viewer-id source; in the headless multi-instance world the viewer
		// id MUST come from the per-session ambient context. Setter is a no-op (BattleAmbientContext
		// .ViewerId is `init`-only — fixed at scope entry per design — and the historical caller
		// (SavedataManager.SetInt + Save) is dead in the server world).
		get => SVSim.BattleEngine.Ambient.BattleAmbient.Require().ViewerId;
		set { /* ambient ViewerId is init-only; SavedataManager path is dead headless */ }
	}

	public static int ShortUdid
	{
		get
		{
			if (short_udid == 0)
			{
				short_udid = Toolbox.SavedataManager.GetInt("SHORT_UDID");
			}
			return short_udid;
		}
		set
		{
			Toolbox.SavedataManager.SetInt("SHORT_UDID", value);
			short_udid = value;
		}
	}

	public static string SessionId
	{
		get
		{
			if (string.IsNullOrEmpty(sessionId))
			{
				sessionId = ViewerId + Udid;
			}
			return Cryptographer.MakeMd5(sessionId);
		}
		set
		{
			sessionId = value;
		}
	}

	public static string dmmViewerId { get; private set; }

	public static string dmmOnetimeToken { get; private set; }

	public static ulong SteamID { get; private set; }

	public static string SteamSessionTicket { get; private set; }

	public static bool IsExistsViewerId()
	{
		if (ViewerId != 0)
		{
			return true;
		}
		return false;
	}

	public static string GetEncodedViewerId()
	{
		string s = CryptAES.encrypt(ViewerId.ToString());
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(s));
	}

	public static string GetEncodedSessionId()
	{
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(SessionId));
	}

	public static string GetEncodedShortUdid()
	{
		return Convert.ToBase64String(Encoding.UTF8.GetBytes(ShortUdid.ToString()));
	}

	public static string GetKeyChainViewerId()
	{
		return "";
	}

	public static string GetIDFA()
	{
		return "";
	}

	public static void SetKeyChainViewerId(string viewerId)
	{
	}

	public static void DeleteKeyChainViewerId()
	{
	}

	public static void InitializeFileds()
	{
		sessionId = null;
		udid = null;
		short_udid = 0;
		Toolbox.SavedataManager.SetInt("VIEWER_ID", 0);
		Toolbox.SavedataManager.SetInt("SHORT_UDID", 0);
		Toolbox.SavedataManager.SetString("UDID", "");
	}

	public IEnumerator Login()
	{
		if (ViewerId == 0)
		{
			GenerateUdid();
			SignUpTask signUpTask = new SignUpTask();
			signUpTask.SetParameter();
			yield return StartCoroutine(Toolbox.NetworkManager.Connect(signUpTask, delegate
			{
				StartCoroutine(GameStartCheckTaskExec());
			}, delegate
			{
				if (Toolbox.BootNetwork != null)
				{
					Toolbox.BootNetwork.IsDoneGameStartCheck = false;
				}
				OutOfService.ShowServiceEndedDialogIfNeeded();
			}, delegate
			{
				if (Toolbox.BootNetwork != null)
				{
					Toolbox.BootNetwork.IsDoneGameStartCheck = false;
				}
			}));
		}
		else
		{
			yield return StartCoroutine(GameStartCheckTaskExec());
		}
	}

	public static bool IsiCloudAvailable()
	{
		return false;
	}

	public static void SetiCloudUser()
	{
	}

	public static void EraseiCloudUser()
	{
	}

	public static string GetiCloudUser()
	{
		return "";
	}

	public void CheckiCloudUserData(Action<NetworkTask.ResultCode> callback)
	{
		GetiCloudUserDataTask.VerifiediCloudUserData.Reset();
		string text = GetiCloudUser();
		if (string.IsNullOrEmpty(text))
		{
			callback(NetworkTask.ResultCode.Success);
			return;
		}
		GenerateUdid();
		GetiCloudUserDataTask getiCloudUserDataTask = new GetiCloudUserDataTask();
		getiCloudUserDataTask.SetParameter(text);
		StartCoroutine(Toolbox.NetworkManager.Connect(getiCloudUserDataTask, callback));
	}

	public void MigrateiCloudUserData(Action<NetworkTask.ResultCode> callback)
	{
		string parameter = GetiCloudUser();
		UpdateiCloudUserDataTask updateiCloudUserDataTask = new UpdateiCloudUserDataTask();
		updateiCloudUserDataTask.SetParameter(parameter);
		StartCoroutine(Toolbox.NetworkManager.Connect(updateiCloudUserDataTask, callback));
	}

	public void FirstTimeSaveiCloudUserData()
	{
		if (IsiCloudAvailable() && string.IsNullOrEmpty(GetiCloudUser()))
		{
			SetiCloudUser();
		}
	}

	public IEnumerator GameStartCheckTaskExec()
	{
		GameStartCheckTask gameStartCheckTask = new GameStartCheckTask();
		gameStartCheckTask.AddSkipCuteCheckResultCode(5607);
		gameStartCheckTask.SetParameter();
		bool isRemoveAccount = false;
		yield return StartCoroutine(Toolbox.NetworkManager.Connect(gameStartCheckTask, delegate
		{
			if (Toolbox.BootNetwork != null)
			{
				Toolbox.BootNetwork.IsDoneGameStartCheck = true;
			}
			URLScheme.ClearCampaignData();
		}, delegate
		{
			if (Toolbox.BootNetwork != null)
			{
				Toolbox.BootNetwork.IsDoneGameStartCheck = false;
			}
			OutOfService.ShowServiceEndedDialogIfNeeded();
		}, delegate(int resultCode)
		{
			if (Toolbox.BootNetwork != null)
			{
				Toolbox.BootNetwork.IsDoneGameStartCheck = false;
			}
			URLScheme.ClearCampaignData();
			if (resultCode == 5607)
			{
				isRemoveAccount = true;
				OnRemoveAccount();
			}
		}));
		if (isRemoveAccount)
		{
			while (true)
			{
				yield return null;
			}
		}
	}

	private void OnRemoveAccount()
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateConfirmationDialog(Data.SystemText.Get("MyPage_0097"));
		dialogBase.SetTitleLabel(Data.SystemText.Get("ErrorHeader_0001"));
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.OnClose = delegate
		{
			UserInfoRequest.DeleteUserData();
		};
	}

	public void GenerateUdid()
	{
		udid = Cryptographer.decode(Toolbox.SavedataManager.GetString("UDID"));
		if (string.IsNullOrEmpty(udid))
		{
			Toolbox.SavedataManager.SetString("UDID", Cryptographer.encode(Guid.NewGuid().ToString()));
			Toolbox.SavedataManager.Save();
		}
	}

	public static bool IsLogined()
	{
		return !string.IsNullOrEmpty(sessionId);
	}

	private IEnumerator Start()
	{
		while (Toolbox.BootSystem == null)
		{
			yield return 0;
		}
		SessionId = "";
		setDmmPlatformData();
		setSTEAMPlatformData();
		URLSchemeStart();
	}

	private void OnApplicationFocus(bool focus)
	{
		if (focus)
		{
			URLSchemeStart();
		}
	}

	private void URLSchemeStart()
	{
		if (CheckUrlScheme)
		{
			StartCoroutine(Delay(0.02f, delegate
			{
			}));
		}
	}

	private IEnumerator Delay(float waitTime, Action action)
	{
		yield return new WaitForSeconds(waitTime);
		action();
	}

	private void setSTEAMPlatformData()
	{
		try
		{
			SteamID = SteamUser.GetSteamID().m_SteamID;
			m_GetAuthSessionTicketResponse = Callback<GetAuthSessionTicketResponse_t>.Create(OnGetAuthSessionTicketResponse);
			byte[] array = new byte[1024];
			SteamNetworkingIdentity pSteamNetworkingIdentity = default(SteamNetworkingIdentity);
			SteamUser.GetAuthSessionTicket(array, array.Length, out var pcbTicket, ref pSteamNetworkingIdentity);
			Array.Resize(ref array, (int)pcbTicket);
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < pcbTicket; i++)
			{
				stringBuilder.AppendFormat("{0:x2}", array[i]);
			}
			SteamSessionTicket = stringBuilder.ToString();
		}
		catch (Exception ex)
		{
			Debug.LogError("<color=aqua>steam client が起動していない。steamの機能を使えません。</color>");
			Debug.LogError(ex.Message);
			Debug.LogError(ex.StackTrace);
		}
	}

	private void OnGetAuthSessionTicketResponse(GetAuthSessionTicketResponse_t pCallback)
	{
	}

	private void setDmmPlatformData()
	{
	}

	public void URLSchemeStartiOS(string message)
	{
		URLScheme.URLSchemeStartiOS(message);
	}
}
