class_name DisarmTrapModal extends Control


const action_value : Dictionary[String, int] = {
	"detonate" : 0,
	"lay trap" : 1,
	"detect" : 2,
	"attack" : 3
}

const icon_textures : Array = [
	{
		"normal" : preload("res://visuals/images/icons/button-detonate.png"),
		"pressed" : preload("res://visuals/images/icons/button-detonate-hit.png")
	},
	{
		"normal" : preload("res://visuals/images/icons/button-trap.png"),
		"pressed" : preload("res://visuals/images/icons/button-trap-hit.png")
	},
	{
		"normal" : preload("res://visuals/images/icons/button-detect.png"),
		"pressed" : preload("res://visuals/images/icons/button-detect-hit.png")
	},
	{
		"normal" : preload("res://visuals/images/icons/button-attack.png"),
		"pressed" : preload("res://visuals/images/icons/button-attack-hit.png")
	},
]

const disarm_icon_template = preload("res://templates/disarm_icon.tscn")
const success_sound = preload("res://audio/sounds/TomWinandySFX_UI_ScifiTech_Notification_13.wav")
const fail_sound = preload("res://audio/sounds/TomWinandySFX_UI_ScifiTech_Cancel_05.wav")
const progress_sound = preload("res://audio/sounds/metal_tiny_hit_impact_01.wav")

@onready var confirmation_dialog : ConfirmationDialog = %ConfirmationDialog
@onready var disarm_window : PanelContainer = %DisarmWindow
@onready var disarm_button_container : HBoxContainer = %DisarmContainer
@onready var timer : Timer = %Timer
@onready var timer_label : Label = %TimerLabel


var square : Vector3i
var disarm_icons : Array[TextureButton]
var disarm_buttons : Array[int]
var progress : int
var disarming : bool
var failed : bool
var difficulty : int = 1

func _process(delta: float) -> void:
	timer_label.text = "%02f.3" % timer.time_left

func start_disarming() -> void:
	disarming = true
	progress = 0
	disarm_icons = []
	disarm_buttons = []
	for i in range(Game.level.difficulty):
		var btn = randi_range(0,3)
		var icon : TextureButton = disarm_icon_template.instantiate()
		icon.texture_normal = icon_textures[btn].normal
		icon.texture_pressed = icon_textures[btn].pressed
		disarm_buttons.append(btn)
		disarm_icons.append(icon)
		disarm_button_container.add_child(icon)
	confirmation_dialog.visible = false
	disarm_window.visible = true
	timer.start(2 + (len(disarm_buttons) * randf_range(.75, 1.25) ))

func try_advance(btn : int) -> void:
	
	if btn == disarm_buttons[progress]:
		disarm_icons[progress].button_pressed = true
		progress += 1
		if progress >= len(disarm_buttons):
			succeed()
		else:
			Game.oneshot(progress_sound)
	else:
		fail()
		
	
func button_pressed(event : InputEventAction) -> void:
	if disarming:
		try_advance(action_value[event.action])
	else:
		match(event.action):
			"detonate": 
				start_disarming()
			"attack":
				var trap = Game.level.get_square_trap(square)
				trap.disarming = false
				Game.player.close_modal()
			_: return

func fail() -> void:
	if failed:
		return
	failed = true
	Game.player.close_modal()
	Game.oneshot(fail_sound)
	Game.level.activate_trap(square)

func succeed() -> void:
	Game.player.close_modal()
	Game.oneshot(success_sound)
	Game.level.disarm_trap(square)

func _on_trap_failed() -> void:
	fail()

func _on_trap_removed(type : Trap.Type) -> void:
	Game.player.close_modal()
