extends Node2D

onready var global: Node = get_node("/root/Global")
onready var can_quit: Node = $CanQuit
onready var timer: Timer = $Timer

onready var wave: Position2D = $Wave

onready var slider1: Slider = $Slider1
onready var slider2: Slider = $Slider2
onready var slider3: Slider = $Slider3

onready var indicator: Sprite = $Indicator
onready var cartridge: Sprite = $Cartridge

onready var record_state: int = 0
onready var answer: Array = \
	[[1, 0, 0], [0, 1, 0.25], [0.5, 0.75, 1]]

func _ready():
	if global.game_vars.has("cartridge_used") \
	&& !global.game_vars.has("cartridge_recorded"):
		cartridge.show()
	
	wave.a = slider1.value
	wave.b = slider2.value * 4
	wave.c = slider3.value * 2
#	wave.update()

func _on_Slider1_value_changed(value):
	wave.a = value

func _on_Slider2_value_changed(value):
	wave.b = value * 4

func _on_Slider3_value_changed(value):
	wave.c = value * 2

func _on_RecordButton_pressed():
	global.play_sound("button_click")
	if !global.game_vars.has("cartridge_used") \
	&& !global.game_vars.has("cartridge_recorded"):
		return
	
	var values: Array = [slider1.value, slider2.value, slider3.value]
	var right: bool = true
	for i in answer[record_state].size():
		if values[i] != answer[record_state][i]:
			right = false
			break
	if !right:
		record_state = 0
		indicator.frame = record_state
		print("beep!")
		return
	
	record_state += 1
	indicator.frame = record_state
	if record_state >= answer.size():
		var inv = global.inventory
		if inv.add_item(inv.Items.rec_cartridge):
			global.game_vars["cartridge_recorded"] = true
			can_quit.queue_free()
			timer.start()
			return
	
func _on_Timer_timeout():
	global.player.animation_player.play("Victory")
	global.return_to_room()
	global.change_room("res://Rooms/SmallHouse/SmallHouse.tscn")
	global.inventory_input = true
