extends Control

onready var player_settings = find_node("settings_player")
onready var game_tab = find_node("game")
onready var controls_tab = find_node("controls")
onready var graphics_tab = find_node("graphics")
onready var system_tab = find_node("system")

var locale


func _ready():
	get_node("menu/back").connect("pressed",self,"_on_back_pressed")
	find_node("bots").set_pressed(local_storage.read_value("bots", true))
	game_tab.set_name(tr("GAME"))
	controls_tab.set_name(tr("CONTROLS"))
	graphics_tab.set_name(tr("GRAPHICS"))
	system_tab.set_name(tr("SYSTEM"))


func _on_back_pressed():
	queue_free()
	get_tree().get_root().get_node("main_menu").show()


func _on_locales_item_selected(index):
	match index:
		0:
			locale = "en"
		1:
			locale = "de"
		_:
			locale = "en"
	TranslationServer.set_locale(locale)
	game_tab.set_name(tr("GAME"))
	controls_tab.set_name(tr("CONTROLS"))
	graphics_tab.set_name(tr("GRAPHICS"))
	system_tab.set_name(tr("SYSTEM"))


func _init_locales():
	var locales = find_node("locales")
	locales.clear()
	locales.add_item(tr("LOCALE_EN"))
	locales.add_item(tr("LOCALE_DE"))
	locale = local_storage.read_value("locale","en")
	match locale:
		"en":
			locales.select(0)
			_on_locales_item_selected(0)
		"de":
			locales.select(1)
			_on_locales_item_selected(1)


func _on_locales_tree_exiting():
	TranslationServer.set_locale(local_storage.read_value("locale","en"))


func _on_save_pressed():
	var values = {}
	values['player_name'] = player_settings.get_name_node().text
	values['player_color'] = player_settings.get_color_node().color.to_html()
	values['bots'] = find_node("bots").is_pressed()
	values['locale'] = locale
	local_storage.write_values(values)
	_on_back_pressed()
