extends Node2D

const melody = preload("res://Sounds/gramophone.ogg")

onready var global: Node = get_node("/root/Global")
onready var audio: AudioStreamPlayer = $AudioStreamPlayer

onready var sprite = $Sprite
onready var winder = $Winder
onready var winder_anim = $Winder/AnimationPlayer
onready var note = $Note

func _ready() -> void:
	if global.game_vars.has("vynil_used"):
		sprite.frame = 1

func _on_Winder_input_event(_viewport, event, _shape_idx) -> void:
	if !event is InputEventMouseButton:
		return
	if !event.pressed:
		return
	if event.button_index != BUTTON_LEFT:
		return
	if !winder_anim.is_playing():
		winder_anim.play("Wind")
		global.play_sound("winder_wind")
	
	if !global.game_vars.has("vynil_used"):
		return
	audio.stream = melody
	audio.play()
	if global.game_vars.has("note_grabbed"):
		return
	
	note.start()

func _on_Note_input_event(_viewport, event, _shape_idx):
	if !event is InputEventMouseButton:
		return
	if !event.pressed:
		return
	if event.button_index != BUTTON_LEFT:
		return
	
	if global.inventory.add_item(global.inventory.Items.note):
		global.game_vars["note_grabbed"] = true
	note.queue_free()
