using System;
using System.Collections.Generic;
using Cute;
using UnityEngine;
using Wizard;

public class SwitchLanguage : MonoBehaviour
{
	private const int DEFAULT_INDEX = 0;

	[SerializeField]
	private UILabel _labelSelectTextLanguage;

	[SerializeField]
	private UILabel _labelSelectVoiceLanguage;

	[SerializeField]
	private GameObject _myPageLayoutRoot;

	[SerializeField]
	private GameObject _titleLayoutRoot;

	[SerializeField]
	private UIToggle _useSmallResource;

	private List<string> _languageTextList = new List<string>();

	private List<string> _languageKeyList = new List<string>();

	private List<string> _languageTextListVoice = new List<string>();

	private int _currentTextIndex;

	private int _currentVoiceIndex;

	private string _selectTextLanguage = "";

	private string _selectVoiceLanguage = "";

	private bool _isFirstCallUseSmallResourceToggleChange = true;

	private static readonly Dictionary<string, string[]> _dictTableVoiceLanguage = new Dictionary<string, string[]>
	{
		{
			Global.LANG_TYPE.Eng.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Eng.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		},
		{
			Global.LANG_TYPE.Kor.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Kor.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		},
		{
			Global.LANG_TYPE.Chs.ToString(),
			new string[1] { Global.LANG_TYPE.Jpn.ToString() }
		},
		{
			Global.LANG_TYPE.Cht.ToString(),
			new string[1] { Global.LANG_TYPE.Jpn.ToString() }
		},
		{
			Global.LANG_TYPE.Fre.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Eng.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		},
		{
			Global.LANG_TYPE.Ita.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Eng.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		},
		{
			Global.LANG_TYPE.Ger.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Eng.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		},
		{
			Global.LANG_TYPE.Spa.ToString(),
			new string[2]
			{
				Global.LANG_TYPE.Eng.ToString(),
				Global.LANG_TYPE.Jpn.ToString()
			}
		}
	};

	private Dictionary<string, Dictionary<string, string>> _dictTableVoiceLanguageViewText = new Dictionary<string, Dictionary<string, string>>
	{
		{
			Global.LANG_TYPE.Eng.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Eng.ToString(),
					"System_Eng_Eng"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Eng_Jpn"
				}
			}
		},
		{
			Global.LANG_TYPE.Kor.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Kor.ToString(),
					"System_Kor_Kor"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Kor_Jpn"
				}
			}
		},
		{
			Global.LANG_TYPE.Chs.ToString(),
			new Dictionary<string, string> { 
			{
				Global.LANG_TYPE.Jpn.ToString(),
				"System_Chs_Jpn"
			} }
		},
		{
			Global.LANG_TYPE.Cht.ToString(),
			new Dictionary<string, string> { 
			{
				Global.LANG_TYPE.Jpn.ToString(),
				"System_Cht_Jpn"
			} }
		},
		{
			Global.LANG_TYPE.Fre.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Eng.ToString(),
					"System_Fre_Eng"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Fre_Jpn"
				}
			}
		},
		{
			Global.LANG_TYPE.Ita.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Eng.ToString(),
					"System_Ita_Eng"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Ita_Jpn"
				}
			}
		},
		{
			Global.LANG_TYPE.Ger.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Eng.ToString(),
					"System_Ger_Eng"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Ger_Jpn"
				}
			}
		},
		{
			Global.LANG_TYPE.Spa.ToString(),
			new Dictionary<string, string>
			{
				{
					Global.LANG_TYPE.Eng.ToString(),
					"System_Spa_Eng"
				},
				{
					Global.LANG_TYPE.Jpn.ToString(),
					"System_Spa_Jpn"
				}
			}
		}
	};

	private DialogBase _switchLanguageDialog;

	public static void Create(bool isForceConfirmDialog, bool isTitle, Action<bool> decideCallback, Action decideCallbackWhenSameChoice = null)
	{
		SwitchLanguage switchLanguage = UnityEngine.Object.Instantiate(UIManager.GetInstance().SwitchLanguagePrefab);
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetObj(switchLanguage.gameObject);
		switchLanguage.CreateSwitchLanguageDialog(dialogBase, isForceConfirmDialog, isTitle, decideCallback, decideCallbackWhenSameChoice);
	}

	public static string GetDefaultVoiceLanguage(string languageKey)
	{
		if (_dictTableVoiceLanguage.ContainsKey(languageKey))
		{
			return _dictTableVoiceLanguage[languageKey][0];
		}
		return "Eng";
	}

	public void CreateSwitchLanguageDialog(DialogBase dialog, bool isForceConfirmDialog, bool isTitle, Action<bool> decideCallback, Action decideCallbackWhenSameChoice = null)
	{
		SystemText text = Data.SystemText;
		_switchLanguageDialog = dialog;
		_switchLanguageDialog.SetSize(DialogBase.Size.M);
		_switchLanguageDialog.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		_switchLanguageDialog.SetButtonText(text.Get("Common_0004"));
		_switchLanguageDialog.SetTitleLabel(text.Get("Dia_Language_003_Title"));
		_languageKeyList.Clear();
		_languageTextList.Clear();
		Global.LanguageProps[] languagePropList = Global.LanguagePropList;
		for (int i = 0; i < languagePropList.Length; i++)
		{
			Global.LanguageProps languageProps = languagePropList[i];
			_languageKeyList.Add(languageProps.LangType);
			_languageTextList.Add(languageProps.DisplayName);
		}
		isTitle = true;
		_titleLayoutRoot.SetActive(isTitle);
		_myPageLayoutRoot.SetActive(!isTitle);
		_useSmallResource.value = PlayerPrefsCache.Instance.GetValue(PlayerPrefsWrapper.SMALL_RESOURCE_STATUS) == 2;
		_useSmallResource.onChange.Add(new EventDelegate(delegate
		{
			OnClickUseSmallResource();
		}));
		_selectTextLanguage = Toolbox.SavedataManager.GetString("LANG_SETTING", "Eng");
		_selectVoiceLanguage = Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING", GetDefaultVoiceLanguage(_selectTextLanguage));
		UpdateSelectLanguageLabel();
		_switchLanguageDialog.SetButtonDelegate(delegate
		{
			bool flag = PlayerPrefsCache.Instance.GetValue(PlayerPrefsWrapper.SMALL_RESOURCE_STATUS) == 2;
			bool afterUseSmallResource = _useSmallResource.value;
			if (isTitle)
			{
				afterUseSmallResource = flag;
			}
			if (IsSameCurrentLanguage() && flag == afterUseSmallResource && !isForceConfirmDialog)
			{
				Toolbox.SavedataManager.SetString("LANG_FIRST_SET", "1");
				decideCallbackWhenSameChoice.Call();
			}
			else
			{
				DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
				dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
				dialogBase.SetTitleLabel(text.Get("System_0040"));
				string textLanguageViewText = GetTextLanguageViewText(_selectTextLanguage);
				string voiceLanguageViewText = GetVoiceLanguageViewText(_selectTextLanguage, _selectVoiceLanguage);
				string arg = Data.SystemText.Get(afterUseSmallResource ? "System_0062" : "System_0061");
				string key = ((PlayerPrefsCache.Instance.GetValue(PlayerPrefsWrapper.SMALL_RESOURCE_STATUS) == 0) ? "System_0063" : "System_0041");
				dialogBase.SetText(string.Format(text.Get(key), textLanguageViewText, voiceLanguageViewText, arg));
				string selectText = _selectTextLanguage;
				string selectVoice = _selectVoiceLanguage;
				bool isSameCurrentLanguage = IsSameCurrentLanguage();
				dialogBase.SetButtonText(text.Get("Common_0004"));
				dialogBase.SetButtonDelegate(delegate
				{
					if (UIManager.GetInstance().GetCurrentScene() != UIManager.ViewScene.Title)
					{
						CheckTimeSlipRotationPeriodTask task = new CheckTimeSlipRotationPeriodTask();
						UIManager.GetInstance().StartCoroutine(Toolbox.NetworkManager.Connect(task, delegate
						{
							ChangeLanguage(isTitle, afterUseSmallResource, decideCallback, selectText, selectVoice, isSameCurrentLanguage);
						}));
					}
					else
					{
						ChangeLanguage(isTitle, afterUseSmallResource, decideCallback, selectText, selectVoice, isSameCurrentLanguage);
					}
				});
			}
		});
	}

	private static void ChangeLanguage(bool isTitle, bool afterUseSmallResource, Action<bool> decideCallback, string selectTextLanguage, string selectVoiceLanguage, bool isSameCurrentLanguage)
	{
		Toolbox.SavedataManager.SetString("LANG_SETTING", selectTextLanguage);
		string fontLangType = Global.GetFontLangType(selectTextLanguage);
		Toolbox.SavedataManager.SetString("LANG_FONT", fontLangType);
		CustomPreference.SetTextLanguage(Toolbox.SavedataManager.GetString("LANG_SETTING", "Eng"));
		Toolbox.SavedataManager.SetString("LANG_SOUND_SETTING", selectVoiceLanguage);
		CustomPreference.SetSoundLanguage(Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING", "Eng"));
		if (!isTitle)
		{
			PlayerPrefsCache.Instance.SetValue(PlayerPrefsWrapper.SMALL_RESOURCE_STATUS, (!afterUseSmallResource) ? 1 : 2);
		}
		Toolbox.SavedataManager.SetString("LANG_FIRST_SET", "1");
		ToolboxGame.SetUp.InitFrameWorkSettings();
		CustomPreference.createResourcePath();
		Data.Initialize();
		Data.SystemText.Initialize();
		if (selectTextLanguage != selectVoiceLanguage)
		{
			PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.MOVIE_SUBTITLES, flag: true);
		}
		else
		{
			PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.MOVIE_SUBTITLES, flag: false);
		}
		decideCallback.Call(!isSameCurrentLanguage);
	}

	public void CreateSwitchTextLanguageDialog()
	{
		_switchLanguageDialog.SetDisp(inDisp: false);
		_currentTextIndex = _languageKeyList.FindIndex((string k) => k == _selectTextLanguage);
		if (_currentTextIndex == -1)
		{
			_currentTextIndex = 0;
		}
		DialogBase dia = DrumrollDialog.Create(_languageTextList, _currentTextIndex, onSelectTextLanguage);
		SystemText systemText = Data.SystemText;
		dia.SetTitleLabel(systemText.Get("Dia_Language_001_Text_Title"));
		dia.SetButtonText(systemText.Get("Common_0004"));
		dia.ResetBackViewAlpha();
		dia.onPushButton1 = delegate
		{
			_selectTextLanguage = _languageKeyList[_currentTextIndex];
			_selectVoiceLanguage = GetDefaultVoiceLanguage(_selectTextLanguage);
			UpdateSelectLanguageLabel();
			_switchLanguageDialog.ReOpen(isResetBackViewAlpha: true);
			dia.InactiveBackView();
		};
		dia.onCloseWithoutSelect = delegate
		{
			_switchLanguageDialog.ReOpen(isResetBackViewAlpha: true);
			dia.InactiveBackView();
		};
	}

	private void onSelectTextLanguage(int index)
	{
		_currentTextIndex = index;
	}

	public void CreateSwitchVoiceLanguageDialog()
	{
		_switchLanguageDialog.SetDisp(inDisp: false);
		string[] array = _dictTableVoiceLanguage[_selectTextLanguage];
		_currentVoiceIndex = 0;
		_languageTextListVoice.Clear();
		for (int i = 0; i < array.Length; i++)
		{
			string text = array[i];
			_languageTextListVoice.Add(GetVoiceLanguageViewText(_selectTextLanguage, text));
			if (_selectVoiceLanguage == text)
			{
				_currentVoiceIndex = i;
			}
		}
		DialogBase dia = DrumrollDialog.Create(_languageTextListVoice, _currentVoiceIndex, onSelectVoiceLanguage);
		SystemText systemText = Data.SystemText;
		dia.SetTitleLabel(systemText.Get("Dia_Language_002_Voice_Title"));
		dia.SetButtonText(systemText.Get("Common_0004"));
		dia.ResetBackViewAlpha();
		dia.onPushButton1 = delegate
		{
			_selectVoiceLanguage = _dictTableVoiceLanguage[_selectTextLanguage][_currentVoiceIndex];
			UpdateSelectLanguageLabel();
			_switchLanguageDialog.ReOpen(isResetBackViewAlpha: true);
			dia.InactiveBackView();
		};
		dia.onCloseWithoutSelect = delegate
		{
			_switchLanguageDialog.ReOpen(isResetBackViewAlpha: true);
			dia.InactiveBackView();
		};
	}

	private void onSelectVoiceLanguage(int index)
	{
		_currentVoiceIndex = index;
	}

	private void UpdateSelectLanguageLabel()
	{
		_labelSelectTextLanguage.text = GetTextLanguageViewText(_selectTextLanguage);
		_labelSelectVoiceLanguage.text = GetVoiceLanguageViewText(_selectTextLanguage, _selectVoiceLanguage);
	}

	private string GetTextLanguageViewText(string key)
	{
		string result = string.Empty;
		if (Global.IsSupportedLanguageType(key))
		{
			result = Global.GetDisplayLanguage(key);
		}
		return result;
	}

	private string GetVoiceLanguageViewText(string textKey, string voiceKey)
	{
		string text = string.Empty;
		if (_dictTableVoiceLanguageViewText.ContainsKey(textKey) && _dictTableVoiceLanguageViewText[textKey].ContainsKey(voiceKey))
		{
			text = Data.SystemText.Get(_dictTableVoiceLanguageViewText[textKey][voiceKey]);
		}
		_ = text == string.Empty;
		return text;
	}

	private bool IsSameCurrentLanguage()
	{
		string text = Toolbox.SavedataManager.GetString("LANG_SETTING", "Eng");
		string text2 = Toolbox.SavedataManager.GetString("LANG_SOUND_SETTING", "Eng");
		if (_selectTextLanguage == text && _selectVoiceLanguage == text2)
		{
			return true;
		}
		return false;
	}

	private void OnClickUseSmallResource()
	{
		if (!_isFirstCallUseSmallResourceToggleChange)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(_useSmallResource.value ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
		}
		_isFirstCallUseSmallResourceToggleChange = false;
	}
}
