extends Node

var DISPLAY_SETTINGS_FILE = "user://display_settings.txt"
var WIDGET_SETTINGS_FILE = "user://widget_settings.txt"

var settings_menu_scene: Node
var settings_menu_button: Node


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	
	SettingSignals.display_settings_saved.connect(_on_display_settings_saved)
	SettingSignals.settings_closed.connect(_on_settings_closed)
	
	DisplayData.WINDOW_SIZE = Vector2(DisplayServer.window_get_size())
	DisplayData.WINDOW_CENTER = DisplayData.WINDOW_SIZE / 2
	
	settings_menu_button = get_node("OpenSettings")
	settings_menu_scene = get_node("SettingsMenu")
	remove_child(settings_menu_scene)
	_try_load_settings()
	_set_settings_button_position()
	
func _try_load_settings():
	_try_load_display_settings()
	_try_load_widget_settings()

func _try_load_display_settings():
	if not FileAccess.file_exists(DISPLAY_SETTINGS_FILE):
		_use_default_settings()
		_on_settings_opened()
		_set_settings_button_position()
		return
	var settings_file = FileAccess.open(DISPLAY_SETTINGS_FILE, FileAccess.READ)
	DisplayData.DISPLAY_MODE_IDX = int(settings_file.get_line())
	DisplayData.DISPLAY_PARAMETERS = []
	var str_bound_parameters = settings_file.get_csv_line()
	for val in str_bound_parameters:
		DisplayData.DISPLAY_PARAMETERS.append(float(val))
	_set_settings_button_position()

func _use_default_settings():
	DisplayData.DISPLAY_MODE_IDX = 0
	var radius = min(DisplayData.WINDOW_SIZE.x, DisplayData.WINDOW_SIZE.y) / 2
	DisplayData.DISPLAY_PARAMETERS = [radius, radius*2]

func _try_load_widget_settings():
	pass

func _on_display_settings_saved(new_display_mode: int, new_display_bounds: Array):
	DisplayData.DISPLAY_MODE_IDX = new_display_mode
	DisplayData.DISPLAY_PARAMETERS = new_display_bounds
	var settings_file = FileAccess.open(DISPLAY_SETTINGS_FILE, FileAccess.WRITE)
	settings_file.store_line(str(DisplayData.DISPLAY_MODE_IDX))
	settings_file.store_line(str(DisplayData.DISPLAY_PARAMETERS[0])+','+str(DisplayData.DISPLAY_PARAMETERS[1]))
	settings_file.close()

func _on_settings_closed():
	remove_child(settings_menu_scene)
	add_child(settings_menu_button)
	_set_settings_button_position()
	
func _on_settings_opened():
	SettingSignals.settings_opened.emit()
	remove_child(settings_menu_button)
	add_child(settings_menu_scene)
	_set_settings_menu_position()

func _set_settings_button_position():
	var corner = Vector2(0, 0)
	var button_position = Vector2(0, 0)
	if DisplayData.DISPLAY_MODE_IDX == 0:
		corner = DisplayData.WINDOW_CENTER + (DisplayData.BR_CORNER * DisplayData.DISPLAY_PARAMETERS[0])
		button_position = corner - (DisplayData.BR_CORNER * max(settings_menu_button.size.x, settings_menu_button.size.y))
	else:
		corner = DisplayData.WINDOW_CENTER + Vector2(DisplayData.DISPLAY_PARAMETERS[0]/2, DisplayData.DISPLAY_PARAMETERS[1]/2)
		button_position = corner - settings_menu_button.size
	settings_menu_button.position = button_position

func _set_settings_menu_position():
	var scene_size = settings_menu_scene.get_node("Panel").size
	settings_menu_scene.get_node("Panel").position = DisplayData.WINDOW_CENTER - scene_size/2

func check_in_bounds(center: Vector2, width: float, height: float):
	if DisplayData.DISPLAY_MODE_IDX == 0:
		return _check_in_circle(center, width, height)
	else:
		return _check_in_rect(center, width, height)

func _check_in_circle(center: Vector2, width: float, height: float):
	return ((center - DisplayData.WINDOW_CENTER).length() + max(width, height)/2) < DisplayData.DISPLAY_PARAMETERS[0]

func _check_in_rect(center: Vector2, width: float, height: float):
	var rel_center = center - DisplayData.WINDOW_CENTER
	if abs(rel_center.x) + width / 2 >= DisplayData.DISPLAY_PARAMETERS[0] / 2:
		return false
	if abs(rel_center.y) + height / 2 >= DisplayData.DISPLAY_PARAMETERS[1] / 2:
		return false
	return true
