tool

extends Control

var socketTemplate = preload("res://Actors/Objects/ElectricSocket/ElectricSocket.tscn")

var api : EditorPlugin

onready var socket_types = $Commands/MakeSocket/Type

func _ready():
	# Add make_socket options
	socket_types.add_item("SNK", 0)
	socket_types.add_item("SRC", 1)
	socket_types.add_item("BID", 2)

func _make_socket(direction: String) -> void:
	var socket_dir = null
	var socket_type = null
	var offset = Vector2.ZERO
	match direction:
		"up":
			socket_dir = ElectricSocket.Direction.DOWN
			offset.y = -32
		"left":
			socket_dir = ElectricSocket.Direction.RIGHT
			offset.x = -32
		"down":
			socket_dir = ElectricSocket.Direction.UP
			offset.y = 32
		"right":
			socket_dir = ElectricSocket.Direction.LEFT
			offset.x = 32
	match socket_types.get_selected_id():
		0:
			socket_type = ElectricSocket.Flow.SINK
		1:
			socket_type = ElectricSocket.Flow.SOURCE
		2:
			socket_type = ElectricSocket.Flow.BIDIRECTIONAL
	var map = api.get_editor_interface().get_edited_scene_root()
	var sockets = map.get_node("sockets")
	var nodes = api.get_editor_interface().get_selection().get_selected_nodes()
	for node in nodes:
		var socket = socketTemplate.instance()
		sockets.add_child(socket)
		socket.direction = socket_dir
		socket.flow = socket_type
		socket.owner = map
		socket.global_position = node.global_position + offset
		socket.connectionPaths = [socket.get_path_to(node)]

func _area_debug_modified(button_pressed):
	var map = api.get_editor_interface().get_edited_scene_root() as GameMap
	map.debug_areas = button_pressed
	for area in map.get_node("areas").get_children():
		area.update()
