extends VBoxContainer


onready var settings_api_key = $"%settings_api_key"
onready var settings_location = $"%settings_location"
onready var settings_celsius = $"%settings_celsius"
onready var settings_fahrenheit = $"%settings_fahrenheit"
onready var settings_autodark = $"%settings_autodark"
onready var settings_12_hour_clock = $"%settings_12_hour_clock"

var changed := false setget set_changed

func set_changed(new:bool):
	changed = true

func _ready():
	settings_api_key.text = Config.flags.api_key
	settings_location.text = Config.flags.location
	settings_celsius.set_pressed_no_signal(Config.flags.show_celsius)
	settings_fahrenheit.set_pressed_no_signal( Config.flags.show_fahrenheit )
	settings_autodark.set_pressed_no_signal(Config.flags.automatic_dark_mode)
	settings_12_hour_clock.set_pressed_no_signal(Config.flags.use_12_hour_clock)
	$"%prevent_sleep_mode".set_pressed_no_signal(Config.flags.prevent_sleep_mode)

func _on_settings_api_key_text_changed(new_text):
	$api_debounce.start(3.5)


func _on_settings_location_text_changed(new_text):
	$api_debounce.start(3.5)


func _on_settings_celsius_toggled(button_pressed):
	Config.flags.show_celsius = button_pressed
	Config.flags.emit_signal("changed")


func _on_settings_fahrenheit_toggled(button_pressed):
	Config.flags.show_fahrenheit = button_pressed
	Config.flags.emit_signal("changed")


func _on_settings_autodark_toggled(button_pressed):
	Config.flags.automatic_dark_mode = button_pressed
	Config.flags.emit_signal("changed")


func _on_settings_12_hour_clock_toggled(button_pressed):
	Config.flags.use_12_hour_clock = button_pressed
	Config.flags.emit_signal("changed")


func _on_location_debounce_timeout():
	Config.flags.location = $"%settings_location".text
	Config.flags.api_key = $"%settings_api_key".text
	Config.flags.emit_signal("changed")


func _on_show_api_key_toggled(button_pressed):
	$"%settings_api_key".secret = !button_pressed


func _on_prevent_sleep_mode_toggled(button_pressed):
	Config.flags.prevent_sleep_mode = button_pressed
	Config.flags.emit_signal("changed")


func _on_paste_key_pressed():
	if not OS.clipboard in ['', null]:
		$"%settings_api_key".text = OS.clipboard
		_on_location_debounce_timeout()
