extends Control

var rows : Array = [
	"Animatronics/CyberChuck",
	"Animatronics/CyberHelen",
	"Animatronics/CyberMunch",
	"",
	"Lighting/Spotlights",
	"Lighting/Floodlights",
	"Lighting/Backdrop Lights",
	"Lighting/Misc Lights",
	"Lighting/Organ Lights",
	"",
	"Add",
	"Audio",
	"Bool",
	"Color",
	"Direct Output",
	"Float",
	"Invert",
	"Keystrokes",
	"Keystroke Ramp",
	"Lerp",
	"Mix Colors",
	"Mix Floats",
	"Mouse Wheel",
	"Multiply",
	"Random",
	"Record",
	"Sine",
	"Subtract",
	"Switch Audio",
	"Timeline",
]

var searching : bool
var lastMousePos : Vector2

func _ready():
	_set_State(false)
	_set_rows()

func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_RIGHT && event.pressed:
				_set_State(!searching)
		#if event.button_index == MOUSE_BUTTON_LEFT && event.pressed && searching:
				#_set_State(false) #fix when not hovered

func _set_State(state:bool):
	searching = state
	visible = searching
	lastMousePos = get_viewport().get_mouse_position()
	position = lastMousePos

func _set_rows():
	var container = get_node("Panel").get_node("ScrollContainer").get_node("Container")
	for child in container.get_children():
		child.queue_free()
	for key in rows:
		if str(key) == "": 
			container.call_deferred("add_child",load("res://Scenes/UI/Search Row.tscn").instantiate())
			continue
		var row = load("res://Scenes/UI/Search Row.tscn").instantiate()
		var button = (row.get_node("Button") as Button)
		var visualName = load("res://Scenes/Node Types/" + str(key) + ".tscn").instantiate().get_node("Node").visual_name
		button.text = visualName if visualName != "" else str(key)
		button.pressed.connect(func():
			_create_node(str(key))
		)
		button.pressed.connect(func():
			_set_State(false)
		)
		container.call_deferred("add_child",row)

func _create_node(name:String):
	var path = "res://Scenes/Node Types/" + name + ".tscn"
	var node = load(path).instantiate()
	get_parent().get_node("Holder").add_child(node)
	node = (node as Control).get_child(0) as GL_Node
	node.nodePath = path
	node.position = lastMousePos
	node._create_uuid()