using System.Collections;
using System.Globalization;
using System.Threading;
using UnityEngine;
using Wizard;

namespace Cute;

public class BootApp : MonoBehaviour
{
	public static string BootScene;

	private Coroutine _logCoroutine;

	private string _logMsg = "";

	private IEnumerator Start()
	{
		_logCoroutine = StartCoroutine(WaitToAccumulateTraceLog());
		_logMsg += "start";
		createMutex();
		restrainOSXProcess();
		startSteamClient();
		while (Toolbox.BootSystem == null)
		{
			yield return 0;
		}
		_logMsg += "start2";
		CultureInfo.DefaultThreadCurrentUICulture = (CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("ja-JP", useUserOverride: false));
		Toolbox.AssetManager.createSavePath();
		yield return StartCoroutine(FontChanger.FontTryChangePersistant(null));
		_logMsg += "start3";
		_logMsg += "start4";
		StopCoroutine(_logCoroutine);
		changeScene();
	}

	private IEnumerator WaitToAccumulateTraceLog()
	{
		yield return new WaitForSeconds(5f);
		LocalLog.AccumulateTraceInquiryLog("BootApp " + _logMsg);
	}

	private void createMutex()
	{
		Toolbox.mute = new Mutex(initiallyOwned: false, "Global\\ShadowversePcPlatformGlobalThread");
		if (Toolbox.mute != null && !Toolbox.mute.WaitOne(0, exitContext: false))
		{
			Toolbox.mute.Close();
			Toolbox.mute = null;
			Application.Quit();
		}
	}

	private void startSteamClient()
	{
		_ = SteamManager.Initialized;
	}

	private void restrainOSXProcess()
	{
	}

	private void changeScene()
	{
		if (BootScene != null)
		{
			Toolbox.SceneManager.ChangeScene(BootScene);
		}
		else
		{
			Toolbox.SceneManager.ChangeScene(SceneType._Splash);
		}
	}
}
