extends Control

var sphere_mesh = SphereMesh.new()
var quad_mesh = QuadMesh.new()

@export var circle_size_control: PackedScene
@export var rect_size_control: PackedScene
var size_control_instance: Node

var screen_bound_mesh: MeshInstance2D

var current_mode: int

var mode_container: HBoxContainer
var save_container: HBoxContainer

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	DisplayData.WINDOW_SIZE = Vector2(DisplayServer.window_get_size())
	DisplayData.WINDOW_CENTER = DisplayData.WINDOW_SIZE / 2
	
	screen_bound_mesh = get_node("ScreenBounds")
	
	mode_container = get_node("ModeSelect")
	var mode_container_size = mode_container.size
	var mode_container_position = Vector2(DisplayData.WINDOW_CENTER.x - mode_container_size.x/2, DisplayData.WINDOW_CENTER.y - mode_container_size.y)
	mode_container.position = mode_container_position
	
	save_container = get_node("SaveSelect")
	var save_container_size = save_container.size
	var save_container_position = Vector2(DisplayData.WINDOW_CENTER.x - save_container_size.x/2, DisplayData.WINDOW_CENTER.y + save_container_size.y * 4)
	save_container.position = save_container_position

func update():
	current_mode = DisplayData.DISPLAY_MODE_IDX
	if current_mode == 0:
		_on_circle_selected()
		screen_bound_mesh.mesh.radius = DisplayData.DISPLAY_PARAMETERS[0]
		screen_bound_mesh.mesh.height = DisplayData.DISPLAY_PARAMETERS[1]
	else:
		_on_rectangle_selected()
		screen_bound_mesh.mesh.size = Vector2(DisplayData.DISPLAY_PARAMETERS[0], DisplayData.DISPLAY_PARAMETERS[1])

func _on_circle_selected() -> void:
	if size_control_instance != null:
		remove_child(size_control_instance)
		size_control_instance.queue_free()
	screen_bound_mesh.mesh = sphere_mesh
	screen_bound_mesh.mesh.radius = 0.5 * min(DisplayData.WINDOW_SIZE.x, DisplayData.WINDOW_SIZE.y)
	screen_bound_mesh.mesh.height = 2 * screen_bound_mesh.mesh.radius
	screen_bound_mesh.position = DisplayData.WINDOW_CENTER
	size_control_instance = circle_size_control.instantiate()
	size_control_instance.set_mesh(screen_bound_mesh.mesh)
	var container_size = size_control_instance.get_node("HBoxContainer").size
	var container_position = Vector2(DisplayData.WINDOW_CENTER.x - container_size.x/2, DisplayData.WINDOW_CENTER.y + container_size.y/2)
	add_child(size_control_instance)
	size_control_instance.position = container_position
	current_mode = 0

func _on_rectangle_selected() -> void:
	if size_control_instance != null:
		remove_child(size_control_instance)
		size_control_instance.queue_free()
	screen_bound_mesh.mesh = quad_mesh
	screen_bound_mesh.mesh.size = DisplayData.WINDOW_SIZE
	screen_bound_mesh.position = DisplayData.WINDOW_CENTER
	size_control_instance = rect_size_control.instantiate()
	size_control_instance.set_mesh(screen_bound_mesh.mesh)
	var container_size = size_control_instance.size
	var container_position = Vector2(DisplayData.WINDOW_CENTER.x - 90, DisplayData.WINDOW_CENTER.y + 36)
	add_child(size_control_instance)
	size_control_instance.position = container_position
	current_mode = 1

func _on_save_pressed() -> void:
	var current_bounds = []
	if current_mode == 0:
		current_bounds = [screen_bound_mesh.mesh.radius, screen_bound_mesh.mesh.height]
	else:
		current_bounds = [screen_bound_mesh.mesh.size.x, screen_bound_mesh.mesh.size.y]
	SettingSignals.display_settings_saved.emit(current_mode, current_bounds)
	SettingSignals.display_settings_closed.emit()

func _on_cancel_pressed() -> void:
	SettingSignals.display_settings_closed.emit()
