# MainMenu.gd
# Hauptmenü mit Spielstart und Einstellungen
extends CanvasLayer

signal start_game

@onready var background      = $Background
@onready var main_panel      = $Background/MainPanel
@onready var settings_panel  = $Background/SettingsPanel
@onready var play_btn        = $Background/MainPanel/ButtonPanel/VBoxContainer/PlayBtn
@onready var settings_btn    = $Background/MainPanel/ButtonPanel/VBoxContainer/SettingsBtn
@onready var quit_btn        = $Background/MainPanel/ButtonPanel/VBoxContainer/QuitBtn

# Settings Controls
@onready var resolution_option  = $Background/SettingsPanel/VBoxContainer/ResolutionRow/ResolutionOption
@onready var window_mode_option = $Background/SettingsPanel/VBoxContainer/WindowModeRow/WindowModeOption
@onready var vsync_check        = $Background/SettingsPanel/VBoxContainer/VsyncRow/VsyncCheck
@onready var msaa_option        = $Background/SettingsPanel/VBoxContainer/MSAARow/MSAAOption
@onready var back_btn           = $Background/SettingsPanel/VBoxContainer/BackBtn

# Bevorzugte Reihenfolge: Hintergrund mit Titel, dann ohne, dann Banner
const BG_WITH_TITLE = "res://assets/Icons/Background+GameName.png"
const BG_PLAIN      = "res://assets/Icons/Background.png"
const BANNER_PATH   = "res://assets/Icons/Banner.png"

# Auflösungen
const RESOLUTIONS = [
	Vector2i(1280, 720),
	Vector2i(1366, 768),
	Vector2i(1600, 900),
	Vector2i(1920, 1080),
	Vector2i(2560, 1440),
	Vector2i(3840, 2160),
]

func _ready():
	get_tree().paused = true
	process_mode = Node.PROCESS_MODE_ALWAYS

	main_panel.visible = true
	settings_panel.visible = false

	# Hintergrundbild laden: zuerst mit Titel, dann ohne, dann Banner
	if ResourceLoader.exists(BG_WITH_TITLE):
		background.texture = load(BG_WITH_TITLE)
	elif ResourceLoader.exists(BG_PLAIN):
		background.texture = load(BG_PLAIN)
	elif ResourceLoader.exists(BANNER_PATH):
		background.texture = load(BANNER_PATH)

	play_btn.pressed.connect(_on_play)
	settings_btn.pressed.connect(_on_settings)
	quit_btn.pressed.connect(_on_quit)
	back_btn.pressed.connect(_on_back)

	_setup_resolution_options()
	_setup_window_mode_options()
	_setup_vsync()
	_setup_msaa()

func _setup_resolution_options():
	resolution_option.clear()
	var current_size = DisplayServer.window_get_size()
	var selected = 0
	for i in range(RESOLUTIONS.size()):
		var res = RESOLUTIONS[i]
		resolution_option.add_item(str(res.x) + " x " + str(res.y), i)
		if res == current_size:
			selected = i
	resolution_option.selected = selected
	resolution_option.item_selected.connect(_on_resolution_changed)

func _setup_window_mode_options():
	window_mode_option.clear()
	window_mode_option.add_item("Fenster", 0)
	window_mode_option.add_item("Randloses Fenster", 1)
	window_mode_option.add_item("Vollbild", 2)

	var current_mode = DisplayServer.window_get_mode()
	match current_mode:
		DisplayServer.WINDOW_MODE_WINDOWED:
			window_mode_option.selected = 0
		DisplayServer.WINDOW_MODE_FULLSCREEN:
			window_mode_option.selected = 2
		_:
			window_mode_option.selected = 0

	window_mode_option.item_selected.connect(_on_window_mode_changed)

func _setup_vsync():
	var vsync_mode = DisplayServer.window_get_vsync_mode()
	vsync_check.button_pressed = (vsync_mode != DisplayServer.VSYNC_DISABLED)
	vsync_check.toggled.connect(_on_vsync_toggled)

func _setup_msaa():
	msaa_option.clear()
	msaa_option.add_item("Aus", 0)
	msaa_option.add_item("2x", 1)
	msaa_option.add_item("4x", 2)
	msaa_option.add_item("8x", 3)

	var current_msaa = get_viewport().msaa_3d
	match current_msaa:
		Viewport.MSAA_DISABLED:
			msaa_option.selected = 0
		Viewport.MSAA_2X:
			msaa_option.selected = 1
		Viewport.MSAA_4X:
			msaa_option.selected = 2
		Viewport.MSAA_8X:
			msaa_option.selected = 3

	msaa_option.item_selected.connect(_on_msaa_changed)

func _on_resolution_changed(index: int):
	var res = RESOLUTIONS[index]
	DisplayServer.window_set_size(res)
	# Fenster zentrieren
	var screen_size = DisplayServer.screen_get_size()
	var window_pos = (screen_size - res) / 2
	DisplayServer.window_set_position(window_pos)

func _on_window_mode_changed(index: int):
	match index:
		0:  # Fenster
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
			DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
		1:  # Randloses Fenster
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
			DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, true)
			var screen_size = DisplayServer.screen_get_size()
			DisplayServer.window_set_size(screen_size)
			DisplayServer.window_set_position(Vector2i.ZERO)
		2:  # Vollbild
			DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
			DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)

func _on_vsync_toggled(enabled: bool):
	if enabled:
		DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_ENABLED)
	else:
		DisplayServer.window_set_vsync_mode(DisplayServer.VSYNC_DISABLED)

func _on_msaa_changed(index: int):
	match index:
		0:
			get_viewport().msaa_3d = Viewport.MSAA_DISABLED
		1:
			get_viewport().msaa_3d = Viewport.MSAA_2X
		2:
			get_viewport().msaa_3d = Viewport.MSAA_4X
		3:
			get_viewport().msaa_3d = Viewport.MSAA_8X

func _on_play():
	start_game.emit()
	get_tree().paused = false
	queue_free()

func _on_settings():
	main_panel.visible = false
	settings_panel.visible = true

func _on_back():
	settings_panel.visible = false
	main_panel.visible = true

func _on_quit():
	get_tree().quit()
