extends Control

signal open_settings

@onready var menu_buttons: Array[Button] = []
var current_menu_index: int = 0
var original_button_scales: Array[Vector2] = []


func _ready() -> void:
	DebugManager.log_info("MainMenu ready", "MainMenu")
	_setup_menu_navigation()
	_update_new_game_button()


func _on_new_game_button_pressed() -> void:
	AudioManager.play_ui_click()
	var button_text: String = $MenuContainer/NewGameButton.text
	if button_text == "Continue":
		DebugManager.log_info("Continue pressed", "MainMenu")
		GameManager.continue_game()
	else:
		DebugManager.log_info("New Game pressed", "MainMenu")
		GameManager.start_new_game()


func _on_settings_button_pressed() -> void:
	AudioManager.play_ui_click()
	DebugManager.log_info("Settings pressed", "MainMenu")
	open_settings.emit()


func _on_exit_button_pressed() -> void:
	AudioManager.play_ui_click()
	DebugManager.log_info("Exit pressed", "MainMenu")
	get_tree().quit()


func _setup_menu_navigation() -> void:
	menu_buttons.clear()
	original_button_scales.clear()

	menu_buttons.append($MenuContainer/NewGameButton)
	menu_buttons.append($MenuContainer/SettingsButton)
	menu_buttons.append($MenuContainer/ExitButton)

	for button in menu_buttons:
		original_button_scales.append(button.scale)

	_update_visual_selection()


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("move_up"):
		_navigate_menu(-1)
	elif event.is_action_pressed("move_down"):
		_navigate_menu(1)
	elif event.is_action_pressed("action_south"):
		_activate_current_button()
	elif event.is_action_pressed("options_menu"):
		AudioManager.play_ui_click()
		DebugManager.log_info("Options menu shortcut pressed", "MainMenu")
		open_settings.emit()
	elif event.is_action_pressed("quit_game"):
		AudioManager.play_ui_click()
		DebugManager.log_info("Quit game shortcut pressed", "MainMenu")
		get_tree().quit()


func _navigate_menu(direction: int) -> void:
	AudioManager.play_ui_click()
	current_menu_index = (current_menu_index + direction) % menu_buttons.size()
	if current_menu_index < 0:
		current_menu_index = menu_buttons.size() - 1
	_update_visual_selection()
	DebugManager.log_info("Menu navigation: index " + str(current_menu_index), "MainMenu")


func _activate_current_button() -> void:
	if current_menu_index >= 0 and current_menu_index < menu_buttons.size():
		var button: Button = menu_buttons[current_menu_index]
		DebugManager.log_info("Activating button via keyboard/gamepad: " + button.text, "MainMenu")
		button.pressed.emit()


func _update_visual_selection() -> void:
	for i in range(menu_buttons.size()):
		var button: Button = menu_buttons[i]
		if i == current_menu_index:
			button.scale = original_button_scales[i] * UIConstants.BUTTON_HOVER_SCALE
			button.modulate = Color(1.2, 1.2, 1.0)
		else:
			button.scale = original_button_scales[i]
			button.modulate = Color.WHITE


func _update_new_game_button() -> void:
	# Check if there's an existing save with progress
	var current_score: int = SaveManager.get_current_score()
	var games_played: int = SaveManager.get_games_played()
	var has_saved_grid: bool = SaveManager.has_saved_grid()

	var new_game_button: Button = $MenuContainer/NewGameButton
	if current_score > 0 or games_played > 0 or has_saved_grid:
		new_game_button.text = "Continue"
		DebugManager.log_info(
			(
				"Updated button to Continue (score: %d, games: %d, grid: %s)"
				% [current_score, games_played, has_saved_grid]
			),
			"MainMenu"
		)
	else:
		new_game_button.text = "New Game"
		DebugManager.log_info("Updated button to New Game", "MainMenu")
