extends Control

@onready
var settings_panel: Panel = %SettingsPanel

@onready
var stop_after_current_button: CheckButton = %StopAfterCurrentCheckButton

@onready
var hide_next_track_button: CheckButton = %HideNextTrackCheckButton

@onready
var add_to_database_button: CheckButton = %AddToDatabaseCheckButton

@onready
var low_played_button: CheckButton = %LowPlayedCheckButton

@onready
var score_label: Label = %ScoreLabel

@onready
var lower_winning_score_button: Button = %LowerButton

@onready
var increase_winning_score_button: Button = %IncreaseButton

@onready
var cache_label: Label = %CacheLabel

@onready
var lower_cache_button: Button = %LowerCacheButton

@onready
var increase_cache_button: Button = %IncreaseCacheButton

@onready
var select_server_button: OptionButton = %SelectServerButton

@onready
var fullscreen_button: CheckButton = %FullscreenButton

#@onready
#var quick_sync_button := $QuickSyncButton 

@onready
var local_button: CheckButton = %LocalButton

signal server_changed

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	fullscreen_button.button_pressed = Settings.fullscreen
	select_server_button.selected = Settings.selected_server
	
	stop_after_current_button.pressed.connect(stop_after_current)
	hide_next_track_button.pressed.connect(hide_next_track)
	add_to_database_button.pressed.connect(add_to_database)
	low_played_button.pressed.connect(low_played)
	lower_winning_score_button.pressed.connect(lower_winning_score)
	increase_winning_score_button.pressed.connect(increase_winning_score)
	lower_cache_button.pressed.connect(lower_cache)
	increase_cache_button.pressed.connect(increase_cache)
	fullscreen_button.pressed.connect(fullscreen)
	local_button.pressed.connect(local_play)
	
	stop_after_current_button.button_pressed = Settings.stop_after_current
	hide_next_track_button.button_pressed = Settings.hide_next_track
	add_to_database_button.button_pressed = Settings.add_to_stats
	low_played_button.button_pressed = Settings.use_low_played_mode
	score_label.text = str(Settings.winning_score)
	cache_label.text = str(Playlist.number_of_tracks_to_preload)
	fullscreen_button.button_pressed = Settings.fullscreen
	select_server_button.select(Settings.selected_server)
	select_server_button.item_selected.connect(select_server)

func _input(event: InputEvent) -> void:
	if self.visible == true:
		if event is InputEventMouseButton && event.is_pressed():
			var evLocal: InputEvent = make_input_local(event)
			if !Rect2(Vector2(0, 0), settings_panel.size).has_point(evLocal.position):
				self.visible = false

func fullscreen() -> void:
	Settings.fullscreen = !Settings.fullscreen
	if Settings.fullscreen == true:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	if Settings.fullscreen == false:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)

func local_play() -> void:
	Settings.play_local = !Settings.play_local
	
func stop_after_current() -> void:
	Settings.stop_after_current = !Settings.stop_after_current

func hide_next_track() -> void:
	Settings.hide_next_track = !Settings.hide_next_track
	
func add_to_database() -> void:
	Settings.add_to_stats = !Settings.add_to_stats
	
func low_played() -> void:
	Settings.use_low_played_mode = !Settings.use_low_played_mode

func lower_winning_score() -> void:
	Settings.winning_score -= 1
	score_label.text = str(Settings.winning_score)

func increase_winning_score() -> void:
	Settings.winning_score += 1
	score_label.text = str(Settings.winning_score)

func lower_cache() -> void:
	Playlist.number_of_tracks_to_preload -= 1
	if Playlist.number_of_tracks_to_preload < 1:
		Playlist.number_of_tracks_to_preload = 1
	cache_label.text = str(Playlist.number_of_tracks_to_preload)

func increase_cache() -> void:
	Playlist.number_of_tracks_to_preload += 1
	cache_label.text = str(Playlist.number_of_tracks_to_preload)

func select_server(new_server: int) -> void:
	Settings.default_path = select_server_button.get_item_text(new_server)
	Settings.selected_server = new_server
	print("Settings.default_path: " + Settings.default_path)
	print("Settings.selected_server: " + str(Settings.selected_server))
	print("new_server: " + str(new_server))
	print("select_server_button.get_item_text(new_server): " + select_server_button.get_item_text(new_server))
	server_changed.emit()
