extends Control
var level
var editor
var active_button


var all_element_buttons
var element_select_button = preload("res://scenes/level_editor/select_button.tscn")
@onready var tile_container = $TabContainer/tiles/HBoxContainer/tile_container
@onready var object_container = $TabContainer/objects/HBoxContainer/object_container
@onready var material_select_dropdown = $TabContainer/tiles/HBoxContainer/tile_container/material_select_dropdown


# Called when the node enters the scene tree for the first time.
func _ready():
	level = get_tree().get_nodes_in_group("level")[0]
	editor = get_tree().get_nodes_in_group("editor")[0]
	

	if level != null and editor != null:
			
		for tile_mesh in level.templates.tile_template_meshes:
			if tile_mesh != level.templates.ELEMENT_MISSING:
				var button = add_button_to_ui(tile_mesh, "tile")
				button.add_to_group("element_select_button")
				button.connect("element_selected", Callable(editor, "_on_element_select_button_pressed"))
				if tile_mesh == level.templates.TILE_FLAT:
					active_button = button.set_as_selected(true)
		all_element_buttons = get_tree().get_nodes_in_group("element_select_button")
		update_active_materials("stone01")
		fill_material_select_dropdown()
		
		for obj_mesh in level.templates.floor_object_template_meshes:
			if obj_mesh != level.templates.ELEMENT_MISSING:
				var button = add_button_to_ui(obj_mesh, "floor_object")
				button.add_to_group("floor_object_select_button")
				button.connect("element_selected", Callable(editor, "_on_element_select_button_pressed"))
				
		for obj_mesh in level.templates.object_template_meshes:
			if obj_mesh != level.templates.ELEMENT_MISSING:
				var button = add_button_to_ui(obj_mesh, "object")
				button.add_to_group("object_select_button")
				button.connect("element_selected", Callable(editor, "_on_element_select_button_pressed"))


func _on_material_select_dropdown_item_selected(index):
	var _mat_id = level.templates.tiles_material_dropdown_entries.keys()[index]
	editor.active_tile_material_set = _mat_id
	update_button_materials()
	editor.update_select_box_materials()


func update_button_materials():
	update_active_materials(editor.active_tile_material_set)


func update_active_materials(material_set):
	editor.active_tile_material_top = level.templates.get_tile_material(material_set, "top")
	editor.active_tile_material_wall = level.templates.get_tile_material(material_set, "wall")
	editor.active_tile_material_top_prev = level.templates.get_tile_preview_material(material_set, "top")
	editor.active_tile_material_wall_prev = level.templates.get_tile_preview_material(material_set, "wall")
	for button in all_element_buttons:
		button.set_element_material(editor.active_tile_material_top, "top")
		button.set_element_material(editor.active_tile_material_wall, "wall")


func add_button_to_ui(element, element_class):
	var element_button = element_select_button.instantiate()
	
	
	if element_class == "tile":
		tile_container.add_child(element_button)	
	else:
		object_container.add_child(element_button)
		
	element_button.set_element(level.templates.get_template_ui_scene(element))
	element_button.element_type = element
	element_button.element_class = element_class
	return element_button


func fill_material_select_dropdown():
	for entry in level.templates.tiles_material_dropdown_entries:
		material_select_dropdown.add_item(level.templates.tiles_material_dropdown_entries[entry])

