extends Control

const PROTAG_IMAGE = preload("res://ui/protag_image.tres")
const GRAYSCALE = preload("res://shaders/grayscale.material")

var slot: int
var save1: Save
var save2: Save
var save3: Save


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	save1 = Save.load(1)
	save2 = Save.load(2)
	save3 = Save.load(3)
	
	set_slot(1, save1)
	set_slot(2, save2)
	set_slot(3, save3)
	
	$HBoxContainer/Btn_Slot1.grab_focus()
	await global.fade_in()


func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("ui_cancel"):
		if $DifficultyDialog.visible:
			$DifficultyDialog/Panel/VBoxContainer/Btn_DifficultyBack.emit_signal("pressed")
		elif $OverwriteDialog.visible:
			$OverwriteDialog.get_cancel_button().emit_signal("pressed")
		else:
			_on_btn_back_pressed()


func _on_btn_back_pressed() -> void:
	var scene = load("res://menus/main_menu.tscn")
	global.goto_scene(scene)


func _on_btn_slot_1_pressed() -> void:
	global.save = Save.new(1)
	$Overlay.show()
	if save1:
		$OverwriteDialog.show()
	else:
		$DifficultyDialog.show()
		$DifficultyDialog/Panel/VBoxContainer/Btn_DifficultyEasy.grab_focus()


func _on_btn_slot_2_pressed() -> void:
	global.save = Save.new(2)
	$Overlay.show()
	if save2:
		$OverwriteDialog.show()
	else:
		$DifficultyDialog.show()
		$DifficultyDialog/Panel/VBoxContainer/Btn_DifficultyEasy.grab_focus()


func _on_btn_slot_3_pressed() -> void:
	global.save = Save.new(3)
	$Overlay.show()
	if save3:
		$OverwriteDialog.show()
	else:
		$DifficultyDialog.show()
		$DifficultyDialog/Panel/VBoxContainer/Btn_DifficultyEasy.grab_focus()


func _on_overwrite_dialog_confirmed() -> void:
	$DifficultyDialog.show()
	$DifficultyDialog/Panel/VBoxContainer/Btn_DifficultyEasy.grab_focus()


func _on_overwrite_dialog_canceled() -> void:
	global.save = null
	$Overlay.hide()


func _on_btn_difficulty_easy_pressed() -> void:
	global.save.difficulty = Save.Difficulty.EASY
	global.save.save_to_disk()
	var scene = load("res://rooms/%s.tscn" % global.save.room)
	$DifficultyDialog.hide()
	global.goto_scene(scene)


func _on_btn_difficulty_normal_pressed() -> void:
	global.save.difficulty = Save.Difficulty.NORMAL
	global.save.save_to_disk()
	var scene = load("res://rooms/%s.tscn" % global.save.room)
	$DifficultyDialog.hide()
	global.goto_scene(scene)


func _on_btn_difficulty_hard_pressed() -> void:
	global.save.difficulty = Save.Difficulty.HARD
	global.save.save_to_disk()
	var scene = load("res://rooms/%s.tscn" % global.save.room)
	$DifficultyDialog.hide()
	global.goto_scene(scene)


func _on_btn_difficulty_back_pressed() -> void:
	global.save = null
	$DifficultyDialog.hide()
	$Overlay.hide()


func set_slot(save_slot: int, save: Save) -> void:
	var basePath = "HBoxContainer/Btn_Slot%s" % save_slot
	if save:
		var ts = Timespan.new(save.playtime)
		get_node(basePath + "/VBoxContainer/Lab_Playtime").text = "%dH %dM %dS" % [floori(ts.TotalHours()), ts.Minutes(), ts.Seconds()]
		get_node(basePath + "/VBoxContainer/SlotImage").texture = PROTAG_IMAGE
		
		get_node(basePath + "/VBoxContainer/Box_Abilities").show()
		if !save.fire: get_node(basePath + "/VBoxContainer/Box_Abilities/Tex_Fire").material = GRAYSCALE
		if !save.ice: get_node(basePath + "/VBoxContainer/Box_Abilities/Tex_Ice").material = GRAYSCALE
		if !save.laser: get_node(basePath + "/VBoxContainer/Box_Abilities/Tex_Laser").material = GRAYSCALE
		
		get_node(basePath + "/VBoxContainer/Grid_Stats").show()
		get_node(basePath + "/VBoxContainer/Grid_Stats/Lab_AtkValue").text = "%s" % save.atk
		get_node(basePath + "/VBoxContainer/Grid_Stats/Lab_DefValue").text = "%s" % save.def
		get_node(basePath + "/VBoxContainer/Grid_Stats/Lab_ConValue").text = "%s" % save.con
		get_node(basePath + "/VBoxContainer/Grid_Stats/Lab_ChgValue").text = "%s" % save.chg
