extends Control

@export var TheTexture: GradientTexture2D
@export var Note: PackedScene

var Audio: AudioStreamOggVorbis = null

func SetLabel(named: String):
	get_node("Label").text = named

func AddSection(Streamed: AudioStreamOggVorbis):
	var children = get_child_count() - 1
	var thing = TextureRect.new()
	# change some variables so it works
	thing.name = "NoteArea" + str(children)
	thing.custom_minimum_size = Vector2(100, 50)
	thing.position = Vector2(100 * children, 0)
	thing.texture = TheTexture
	self.custom_minimum_size = self.custom_minimum_size + Vector2(100, 0)
	# add the audio
	Audio = Streamed
	# show the damn thing
	add_child(thing)

# add a note
func _on_gui_input(event):
	var AdjustPosition = floor(event.position.x / 25)
	if Input.get_action_raw_strength("Add Note") == 1:
		var thing = Note.instantiate()
		thing.get_node("Audio").stream = Audio
		thing.position = Vector2(floor(25 * AdjustPosition / 2), 0)
		# if there is a duplicate note, remove it. It will cause problems.
		for i in get_children():
			if thing.position == i.position && !i.name.contains("NoteArea") && !i.name.contains("Label"):
				i.queue_free()
		# there was a stupid bug where we couldn't add anything to the right of a note after it was placed.
		# solution: make a control node, parent it, and then parent the note onto the control node.
		# why does it work? I don't fucking know.
		var controlled = Control.new()
		controlled.position = Vector2(floor(25 * AdjustPosition / 2), 0)
		add_child(controlled)
		controlled.add_child(thing)
