extends Node

# these two signals come from the MainMenu and start / continue the game
# gets sent to the main.gd scene
signal on_new_game_started
signal on_game_continued

@onready var main_menu: Control = $MainMenu
@onready var ingame_controls: Control = $IngameControls
@onready var ingame_menu: Control = $IngameMenu
@onready var camera_control_area : Control = $CameraControlArea
@onready var save_ui : Control = $SaveUI
@onready var battle_ui : Control = $BattleUI

func _ready() -> void:
	main_menu.connect("on_game_continued", _on_main_menu_on_game_continued)
	main_menu.connect("on_new_game_started", _on_main_menu_on_new_game_started)
	
	ingame_controls.connect("menu_button_clicked", _on_ingame_controls_menu_button_clicked)
	ingame_controls.connect("save_button_clicked", _on_ingame_controls_save_button_clicked)
	ingame_controls.connect("save_button_released", _on_ingame_controls_save_button_released)
	ingame_controls.connect("save_timer_incremented", _on_ingame_controls_save_timer_incremented)
	ingame_controls.connect("save_timer_reached", _on_ingame_controls_save_timer_reached)
	
	pass

func show_main_menu(p_visible = true) -> void:
	main_menu.visible = p_visible
	
func show_ingame_controls(p_visible = true) -> void:
	ingame_controls.visible = p_visible

func show_battle_ui(p_visible = true) -> void:
	battle_ui.visible = p_visible
	battle_ui.update()

func _on_ingame_controls_menu_button_clicked() -> void:
	ingame_menu.visible = true

# Updates the whole UI
func update() -> void:
	# Updating the player info
	ingame_menu._update_player_info()
	
	# Updating the monster list
	ingame_menu._update_monster_list()
	

func _on_ingame_controls_save_button_clicked() -> void:
	save_ui.visible = true

func _on_ingame_controls_save_button_released() -> void:
	save_ui.visible = false

func _on_ingame_controls_save_timer_incremented(value) -> void:
	save_ui.get_node("SavePercentage").text = str(value)


func _on_ingame_controls_save_timer_reached() -> void:
	save_ui.get_node("SavePercentage").text = "Saved!"
	ingame_controls.disable_save_button()
	await get_tree().create_timer(1.0).timeout
	ingame_controls.enable_save_button()
	save_ui.visible = false
	save_ui.get_node("SavePercentage").text = "0%"


# sends the signal from the MainMenu node up to the main.gd scene
func _on_main_menu_on_new_game_started() -> void:
	on_new_game_started.emit()

# sends the signal from the MainMenu node up to the main.gd scene
func _on_main_menu_on_game_continued() -> void:
	on_game_continued.emit()
