using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Cute;
using LitJson;
using UnityEngine;
using Wizard.ErrorDialog;

namespace Wizard;

public class SetUp : MonoBehaviour
{
	private JsonReader defaultReader;

	private NetworkManager networkManager;

	private IEnumerator Start()
	{
		Object.DontDestroyOnLoad(this);
		ToolboxGame.SetUp = this;
		if (string.IsNullOrEmpty(Toolbox.SavedataManager.GetString("LANG_SETTING")))
		{
			CustomPreference.SetTextLanguage("Eng");
			CustomPreference.SetSoundLanguage("Eng");
		}
		Data.SystemText = new SystemText();
		Data.SystemText.Initialize();
		InitFrameWorkSettings();
		Data.Initialize();
		Data.SystemText.Initialize();
		Wizard.ErrorDialog.Dialog.Initialize();
		while (ToolboxGame.UIManager == null)
		{
			yield return 0;
		}
		InitGameManger();
		Toolbox.SceneManager.SceneChangeParameter.KeepAssets = true;
	}

	public void InitFrameWorkSettings()
	{
		CustomPreference.SetScemeMode(CustomPreference.eSchemeType.Https);
		CustomPreference.SetScemeModeCDN(CustomPreference.eSchemeType.Https);
		CustomPreference.SetOptionalNodeSceme();
		CustomPreference.SetSignature("Shadowverse");
		CustomPreference.SetApplicationServerURL("utoongaize.shadowverse.jp/shadowverse/");
		CustomPreference.SetResourceServerURL("shadowverse.akamaized.net/");
		CustomPreference.SetNodeServerURL("");
		CustomPreference.SetDeckBuilderServerURL("shadowverse-portal.com/api/v1/game_api/");
		CustomPreference.SetLocale("Eng");
		int num = (PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.FRAMERATE) ? 60 : 30);
		Toolbox.QualityManager.SetFrameRate(num);
		Application.targetFrameRate = num;
		Toolbox.DeviceManager.SetVersionName();
		Toolbox.AssetManager.AddNoUnloadAssetGroupName("card_shader_common");
		Toolbox.AssetManager.AddNoUnloadAssetGroupName("card_foil");
		Toolbox.AssetManager.AddNoUnloadAssetGroupName("effect_md_eff");
		string text = Toolbox.SavedataManager.GetString("LANG_SETTING", "Eng");
		string soundLanguage = Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING", SwitchLanguage.GetDefaultVoiceLanguage(text));
		string text2 = "";
		for (int i = 0; i < AssetBundleEditorTag.categoryNameList.Length; i++)
		{
			if (text2 != "")
			{
				text2 += ",";
			}
			text2 += AssetBundleEditorTag.categoryNameList[i].name;
		}
		string[] categoryList = text2.Split(',');
		Toolbox.AssetManager.SetCategoryList(categoryList);
		Toolbox.AssetManager.createSavePath();
		Toolbox.AssetManager.createPackagePath();
		Toolbox.NetworkManager.NetworkUI = NetworkUI.GetInstance();
		SoftwareReset.setAction();
		CustomPreference.isPreferenceComplete = true;
		CustomPreference.SetTextLanguage(text);
		CustomPreference.SetSoundLanguage(soundLanguage);
		SetLanguageData();
		Toolbox.NetworkManager.Certification();
		StartTitleCheckTask();
	}

	private void StartTitleCheckTask()
	{
		CheckSpecialTitleTask checkSpecialTitleTask = new CheckSpecialTitleTask();
		checkSpecialTitleTask.SkipAllNetworkChecks();
		StartCoroutine(Toolbox.NetworkManager.Connect(checkSpecialTitleTask, null, null, null, encrypt: true, useJson: false, showLoadingIcon: false));
	}

	public void InitGameSetting()
	{
		SetNetworkManager();
		WaitSetUp();
	}

	private void InitGameManger()
	{
		GameMgr.CreateIns();
		GameMgr.GetIns().CreateMgrIns(base.gameObject);
		GameMgr.GetIns().GetSoundMgr().SetCueInfo();
		GameMgr.GetIns().GetSoundMgr().AssignSe();
		GameMgr.GetIns().GetSoundMgr().CreateBGMList();
	}

	private void WaitSetUp()
	{
		StartDownLoad();
	}

	private void StartDownLoad()
	{
		GameMgr.GetIns().GetSoundMgr().LoadBGM(Bgm.BGM_TYPE.HOME, delegate
		{
			StartCoroutine(LoadAssetBundles());
		});
	}

	private void errorCallBack()
	{
		UIManager.GetInstance().isErrorProc = false;
		string titleLabel = Data.SystemText.Get("System_0020");
		if (UIManager.GetInstance().NowOpenDialog != null)
		{
			UIManager.GetInstance().NowOpenDialog.CloseWithoutSelect();
		}
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose(isSystem: true);
		dialogBase.SetFadeButtonEnabled(flag: false);
		dialogBase.SetTitleLabel(titleLabel);
		dialogBase.SetText(Data.SystemText.Get("System_0021"));
		dialogBase.SetReturnMsg(UIManager.GetInstance().gameObject, "CommonResetGame");
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.GrayBtn);
		dialogBase.SetButtonText(Data.SystemText.Get("System_0006"));
		UIManager.GetInstance().isRetryProc = false;
		if (UIManager.GetInstance().isErrorProc && !UIManager.GetInstance().isRetryProc)
		{
			SoftwareReset.exec();
		}
	}

	[DllImport("CyCrypt")]
	public static extern string EncodePost(string input, string opt);

	[DllImport("CyCrypt")]
	public static extern string DecodePost(string input, string opt);

	[DllImport("CyCrypt")]
	public static extern void DestoryEncodeText();

	[DllImport("CyCrypt")]
	public static extern void DestoryDecodeText();

	private void SetNetworkManager()
	{
		if (networkManager == null)
		{
			networkManager = Toolbox.NetworkManager;
		}
	}

	public void BuildFirstScene()
	{
		PlayerStaticData.LoadUserEmblemTexture();
		PlayerStaticData.LoadUserCountryTexture();
		IDictionary<int, int> dictionary = new Dictionary<int, int>();
		foreach (UserCard userCard in Data.Load.data.UserCardList)
		{
			if (!dictionary.ContainsKey(userCard.card_id))
			{
				dictionary.Add(userCard.card_id, userCard.number);
			}
		}
		GameMgr.GetIns().GetDataMgr().SetUserOwnCardData(dictionary);
		LoadComplete();
	}

	private void LoadComplete()
	{
		DataMgr dataMgr = GameMgr.GetIns().GetDataMgr();
		if (Data.Load.data._userTutorial.tutorial_step != 100)
		{
			dataMgr.SetPlayerCharaId(500008);
			Global.IS_LOAD_ALLDONE = true;
			return;
		}
		DeckInfoTask task = new DeckInfoTask();
		task.SetParameter(Format.All);
		UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
		{
			bool flag = false;
			DeckGroupListData deckGroupListData = task.DeckGroupListData;
			foreach (DeckData item in deckGroupListData.GetDeckListByAttribute(DeckAttributeType.CustomDeck))
			{
				if (!item.IsNoCard())
				{
					dataMgr.SetPlayerCharaIdBySkinId(item.GetSkinId());
					dataMgr.SetCurrentDeckData(item.GetCardIdList());
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				DeckData deckData = deckGroupListData.GetDeckListByAttribute(DeckAttributeType.DefaultDeck).First();
				dataMgr.SetPlayerCharaIdByClassId(deckData.GetDeckClassID());
				dataMgr.SetCurrentDeckData(deckData.GetCardIdList());
			}
			GameMgr.GetIns().BuildDeckData();
			Global.IS_LOAD_ALLDONE = true;
		}));
	}

	private IEnumerator LoadAssetBundles()
	{
		GameMgr.GetIns().Init();
		yield return StartCoroutine(UIManager.GetInstance().CardLoadResident());
		BuildFirstScene();
	}

	private void OnUpdateDeckRequestFinished(NetworkTask.ResultCode error)
	{
		LoadComplete();
	}

	private void Update()
	{
		if (GameMgr.GetIns() != null)
		{
			GameMgr.GetIns().Update();
		}
	}

	private void SetLanguageData()
	{
		string text = Toolbox.SavedataManager.GetString("LANG_SETTING");
		if (string.IsNullOrEmpty(text))
		{
			string systemLanguage = Global.GetSystemLanguage();
			if (Global.IsSupportedSystemLanguage(systemLanguage))
			{
				text = Global.GetLanguageType(systemLanguage);
				Toolbox.SavedataManager.SetString("LANG_SETTING", text);
				Toolbox.SavedataManager.SetString("LANG_FONT", Global.GetFontLangType(text));
				Toolbox.SavedataManager.SetString("LANG_SOUND_SETTING", SwitchLanguage.GetDefaultVoiceLanguage(text));
				CustomPreference.SetTextLanguage(Toolbox.SavedataManager.GetString("LANG_SETTING"));
				CustomPreference.SetSoundLanguage(Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING"));
				Data.SystemText = new SystemText();
				Data.SystemText.Initialize();
				CustomPreference.createResourcePath();
			}
			if (string.IsNullOrEmpty(text))
			{
				Toolbox.SavedataManager.SetString("LANG_SETTING", "Eng");
				Toolbox.SavedataManager.SetString("LANG_FONT", Global.GetFontLangType(Global.LANG_TYPE.Eng.ToString()));
				Toolbox.SavedataManager.SetString("LANG_SOUND_SETTING", SwitchLanguage.GetDefaultVoiceLanguage("Eng"));
				CustomPreference.SetTextLanguage(Toolbox.SavedataManager.GetString("LANG_SETTING"));
				CustomPreference.SetSoundLanguage(Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING"));
			}
		}
	}
}
