tool
extends Node


const languages_dict := {
	"en_US" : ["English", "English"],
	"cs_CZ" : ["Czech", "Czech"],
	"de_DE" : ["Deutsch", "German"],
	"el_GR" : ["Ελληνικά", "Greek"],
	"eo" : ["Esperanto", "Esperanto"],
	"es_ES" : ["Español", "Spanish"],
	"fr_FR" : ["Français", "French"],
	"id_ID" : ["Indonesian", "Indonesian"],
	"it_IT" : ["Italiano", "Italian"],
	"lv_LV" : ["Latvian", "Latvian"],
	"pl_PL" : ["Polski", "Polish"],
	"pt_BR" : ["Português Brasileiro", "Brazilian Portuguese"],
	"ru_RU" : ["Русский", "Russian"],
	"zh_CN" : ["简体中文", "Chinese Simplified"],
	"zh_TW" : ["繁體中文", "Chinese Traditional"],
	"no_NO" : ["Norsk", "Norwegian"],
	"hu_HU" : ["Magyar", "Hungarian"],
	"ro_RO" : ["Română", "Romanian"],
	"ko_KR" : ["한국어", "Korean"],
}

var loaded_locales : Array
onready var cjk_font = preload("res://addons/draw_gd/assets/fonts/CJK/NotoSansCJKtc-Regular.tres")

var DrawGD : Node = null

func _enter_tree() -> void:
	var n : Node = get_parent()
	while n:
		if n.name == "DrawGDSingleton":
			DrawGD = n
			break
		n = n.get_parent()
	
	loaded_locales = TranslationServer.get_loaded_locales()

	# Make sure locales are always sorted, in the same order
	loaded_locales.sort()
	var button_group = get_child(0).group

	# Create radiobuttons for each language
	for locale in loaded_locales:
		if !locale in languages_dict:
			continue
		var button = CheckBox.new()
		button.text = languages_dict[locale][0] + " [%s]" % [locale]
		button.name = languages_dict[locale][1]
		button.hint_tooltip = languages_dict[locale][1]
		button.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
		button.group = button_group
		if is_cjk(locale):
			button.add_font_override("font", cjk_font)
		add_child(button)

	# Load language
	if DrawGD.config_cache.has_section_key("preferences", "locale"):
		var saved_locale : String = DrawGD.config_cache.get_value("preferences", "locale")
		TranslationServer.set_locale(saved_locale)

		# Set the language option menu's default selected option to the loaded locale
		var locale_index: int = loaded_locales.find(saved_locale)
		get_child(0).pressed = false # Unset System Language option in preferences
		get_child(locale_index + 1).pressed = true
	else: # If the user doesn't have a language preference, set it to their OS' locale
		TranslationServer.set_locale(OS.get_locale())

	for child in get_children():
		if child is Button:
			child.connect("pressed", self, "_on_Language_pressed", [child.get_index()])
			child.hint_tooltip = child.name


func _on_Language_pressed(index : int) -> void:
	get_child(index).pressed = true
	if index == 0:
		TranslationServer.set_locale(OS.get_locale())
	else:
		TranslationServer.set_locale(loaded_locales[index - 1])

	if is_cjk(TranslationServer.get_locale()):
		DrawGD.control.theme.default_font = preload("res://addons/draw_gd/assets/fonts/CJK/NotoSansCJKtc-Regular.tres")
	else:
		DrawGD.control.theme.default_font = preload("res://addons/draw_gd/assets/fonts/Roboto-Regular.tres")

	DrawGD.config_cache.set_value("preferences", "locale", TranslationServer.get_locale())
	DrawGD.config_cache.save("user://cache.ini")

	# Update Translations
	DrawGD.update_hint_tooltips()
	DrawGD.preferences_dialog._on_PreferencesDialog_popup_hide()
	DrawGD.preferences_dialog._on_PreferencesDialog_about_to_show(true)


func is_cjk(locale : String) -> bool:
	return "zh" in locale or "ko" in locale
