##part 1 - tile scenes and meshes
const TILE_FLAT = "tile_flat"
const TILE_OCT = "tile_oct"
const TILE_PLUS = "tile_plus"
const TILE_RAMP = "tile_ramp"
const TILE_RAMP_BOTTOM = "tile_ramp_bottom"
const TILE_RAMP_DOUBLE = "tile_ramp_double"
const TILE_STRANGE = "tile_strange"
const ELEMENT_MISSING = "element_missing"

const OBJ_PLAYER = "obj_player"

const OBJ_SCREW_RED = "obj_screw_red"
const OBJ_SCREW_BLUE = "obj_screw_blue"
const OBJ_SCREW_YELLOW = "obj_screw_yellow"

const OBJ_TOOL_RED = "obj_tool_red"
const OBJ_TOOL_BLUE = "obj_tool_blue"
const OBJ_TOOL_YELLOW = "obj_tool_yellow"





var template_scenes = {
	self.ELEMENT_MISSING : preload("res://scenes/tiles/tile_missing.tscn"),
	
	self.TILE_FLAT : preload("res://scenes/tiles/tile_flat.tscn"),
	self.TILE_OCT : preload("res://scenes/tiles/tile_oct.tscn"),
	self.TILE_PLUS : preload("res://scenes/tiles/tile_plus.tscn"),
	self.TILE_RAMP : preload("res://scenes/tiles/tile_ramp.tscn"),
	self.TILE_RAMP_BOTTOM : preload("res://scenes/tiles/tile_ramp_bottom.tscn"),
	self.TILE_RAMP_DOUBLE : preload("res://scenes/tiles/tile_ramp_double.tscn"),
	self.TILE_STRANGE : preload("res://scenes/tiles/tile_strange.tscn"),
	
	self.OBJ_SCREW_RED : preload("res://scenes/entities/screws_and_tools/ScrewRed.tscn"),
	self.OBJ_SCREW_BLUE : preload("res://scenes/entities/screws_and_tools/ScrewBlue.tscn"),
	self.OBJ_SCREW_YELLOW : preload("res://scenes/entities/screws_and_tools/ScrewYellow.tscn"),
	
	self.OBJ_TOOL_RED : preload("res://scenes/entities/screws_and_tools/PlayerToolRed.tscn"),
	self.OBJ_TOOL_BLUE : preload("res://scenes/entities/screws_and_tools/PlayerToolBlue.tscn"),
	self.OBJ_TOOL_YELLOW : preload("res://scenes/entities/screws_and_tools/PlayerToolYellow.tscn"),
	
	self.OBJ_PLAYER : preload("res://scenes/entities/player/Player.tscn"),

}

var template_meshes = {
	self.ELEMENT_MISSING : preload("res://assets/tiles/meshes/tile_flat.obj"),
	
	self.TILE_FLAT : preload("res://assets/tiles/meshes/tile_flat.obj"),
	self.TILE_OCT : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.TILE_PLUS : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.TILE_RAMP : preload("res://assets/tiles/meshes/tile_ramp.obj"),
	self.TILE_RAMP_BOTTOM : preload("res://assets/tiles/meshes/tile_ramp_bottom.obj"),
	self.TILE_RAMP_DOUBLE : preload("res://assets/tiles/meshes/tile_ramp_double.obj"),
	self.TILE_STRANGE : preload("res://assets/tiles/meshes/tile_strange.obj"),
	
	self.OBJ_SCREW_RED : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.OBJ_SCREW_BLUE : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.OBJ_SCREW_YELLOW : preload("res://assets/tiles/meshes/tile_oct.obj"),
	
	self.OBJ_TOOL_RED : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.OBJ_TOOL_BLUE : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.OBJ_TOOL_YELLOW : preload("res://assets/tiles/meshes/tile_oct.obj"),
	
	self.OBJ_PLAYER : preload("res://assets/tiles/meshes/tile_oct.obj"),
}

var tile_template_meshes = {
	self.TILE_FLAT : preload("res://assets/tiles/meshes/tile_flat.obj"),
	self.TILE_OCT : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.TILE_PLUS : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.TILE_RAMP : preload("res://assets/tiles/meshes/tile_ramp.obj"),
	self.TILE_RAMP_BOTTOM : preload("res://assets/tiles/meshes/tile_ramp_bottom.obj"),
	self.TILE_RAMP_DOUBLE : preload("res://assets/tiles/meshes/tile_ramp_double.obj"),
	self.TILE_STRANGE : preload("res://assets/tiles/meshes/tile_strange.obj"),
}

var object_template_meshes = {
	self.OBJ_TOOL_RED : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.OBJ_TOOL_BLUE : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.OBJ_TOOL_YELLOW : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.OBJ_PLAYER : preload("res://assets/tiles/meshes/tile_plus.obj"),
}

var floor_object_template_meshes = {
	self.OBJ_SCREW_RED : preload("res://assets/tiles/meshes/tile_oct.obj"),
	self.OBJ_SCREW_BLUE : preload("res://assets/tiles/meshes/tile_plus.obj"),
	self.OBJ_SCREW_YELLOW : preload("res://assets/tiles/meshes/tile_oct.obj"),
}

var template_ui_scenes = {
	self.ELEMENT_MISSING : preload("res://scenes/level_editor/ui_tiles/ui_tile_flat.tscn"),
	
	self.TILE_FLAT : preload("res://scenes/level_editor/ui_tiles/ui_tile_flat.tscn"),
	self.TILE_OCT : preload("res://scenes/level_editor/ui_tiles/ui_tile_oct.tscn"),
	self.TILE_PLUS : preload("res://scenes/level_editor/ui_tiles/ui_tile_plus.tscn"),
	self.TILE_RAMP : preload("res://scenes/level_editor/ui_tiles/ui_tile_ramp.tscn"),
	self.TILE_RAMP_BOTTOM : preload("res://scenes/level_editor/ui_tiles/ui_tile_ramp_bottom.tscn"),
	self.TILE_RAMP_DOUBLE : preload("res://scenes/level_editor/ui_tiles/ui_tile_ramp_double.tscn"),
	self.TILE_STRANGE : preload("res://scenes/level_editor/ui_tiles/ui_tile_strange.tscn"),
	
	self.OBJ_SCREW_RED  : preload("res://scenes/level_editor/ui_tiles/ui_obj_screw_red.tscn"),
	self.OBJ_SCREW_BLUE  : preload("res://scenes/level_editor/ui_tiles/ui_obj_screw_blue.tscn"),
	self.OBJ_SCREW_YELLOW  : preload("res://scenes/level_editor/ui_tiles/ui_obj_screw_yellow.tscn"),
	
	self.OBJ_TOOL_RED  : preload("res://scenes/level_editor/ui_tiles/ui_obj_tool_red.tscn"),
	self.OBJ_TOOL_BLUE  : preload("res://scenes/level_editor/ui_tiles/ui_obj_tool_blue.tscn"),
	self.OBJ_TOOL_YELLOW  : preload("res://scenes/level_editor/ui_tiles/ui_obj_tool_yellow.tscn"),
	
	self.OBJ_PLAYER  : preload("res://scenes/level_editor/ui_tiles/ui_obj_player.tscn"),
}



func get_template_scene(template_name, material_set=""):
	if template_name == null:
		return null
	if template_name not in self.template_scenes:
		template_name = "element_missing"

	var new_template_scene = self.template_scenes[template_name].instantiate()
	new_template_scene.template_name = template_name
	if "material_set" in new_template_scene:
		new_template_scene.material_set = material_set

	return new_template_scene


func get_template_mesh(template_name):
	if template_name == null:
		return null
	if template_name not in self.template_meshes:
		template_name = "element_missing"

	var new_template_mesh = self.template_meshes[template_name]
	return new_template_mesh


func get_template_ui_scene(template_name):
	if template_name == null:
		return null
	if template_name not in self.template_ui_scenes:
		template_name = "element_missing"

	var new_template_ui_scene = self.template_ui_scenes[template_name].instantiate()
	#new_tile.template_name = template

	return new_template_ui_scene		
	

##part 2 - tile materials / material sets

const MAT_STONE_01 = "stone01"
const MAT_STONE_02 = "stone02"
const MAT_JUNGLE_01 = "jungle01"
const MAT_MUSHROOM_01 = "mushroom01"
const MAT_MUSHROOM_02 = "mushroom02"
const MAT_LAVA_01 = "lava01"

var tiles_material_dropdown_entries = {
	self.MAT_STONE_01 : "stone 01",
	self.MAT_STONE_02 : "stone 02",
	self.MAT_JUNGLE_01 : "jungle 01",
	self.MAT_MUSHROOM_01 : "mushroom 01",
	self.MAT_MUSHROOM_02 : "mushroom 02",
	self.MAT_LAVA_01 : "lava 01",
}


var tiles_materials_top = {
	self.MAT_STONE_01 : ResourceLoader.load("res://assets/tiles/materials/M_Stone01_Top.material"),
	self.MAT_STONE_02 : ResourceLoader.load("res://assets/tiles/materials/M_Stone02_Top.material"),
	self.MAT_JUNGLE_01 : ResourceLoader.load("res://assets/tiles/materials/M_Jungle01_Top.material"),
	self.MAT_MUSHROOM_01 : ResourceLoader.load("res://assets/tiles/materials/M_Mushroom01_Top.material"),
	self.MAT_MUSHROOM_02 : ResourceLoader.load("res://assets/tiles/materials/M_Mushroom02_Top.material"),
	self.MAT_LAVA_01 : ResourceLoader.load("res://assets/tiles/materials/M_Lava01_Top.material"),
}

var tiles_materials_wall = {
	self.MAT_STONE_01 : ResourceLoader.load("res://assets/tiles/materials/M_Stone01_Wall.material"),
	self.MAT_STONE_02 : ResourceLoader.load("res://assets/tiles/materials/M_Stone02_Wall.material"),
	self.MAT_JUNGLE_01 : ResourceLoader.load("res://assets/tiles/materials/M_Jungle01_Wall.material"),
	self.MAT_MUSHROOM_01 : ResourceLoader.load("res://assets/tiles/materials/M_Mushroom01_Wall.material"),
	self.MAT_MUSHROOM_02 : ResourceLoader.load("res://assets/tiles/materials/M_Mushroom02_Wall.material"),
	self.MAT_LAVA_01 : ResourceLoader.load("res://assets/tiles/materials/M_Lava01_Wall.material"),
}



func get_tile_material(tile_variant, type, editor_variant = false):
	if type == "top":
		return self.tiles_materials_top[tile_variant]
	elif type == "wall":
		return self.tiles_materials_wall[tile_variant]
	elif type == "invis":
		if editor_variant == false:
			return ResourceLoader.load("res://assets/tiles/materials/M_Invis.material")
		else:
			return ResourceLoader.load("res://assets/tiles/materials/M_InvisED.material")
	else:
		return null
		
func get_tile_preview_material(tile_variant, type):
	if type == "top":
		return _create_preview_material_variant(self.tiles_materials_top[tile_variant])
	elif type == "wall":
		return _create_preview_material_variant(self.tiles_materials_wall[tile_variant])
	elif type == "invis":

		return ResourceLoader.load("res://assets/tiles/materials/M_InvisED.material")
	else:
		return null

func _create_preview_material_variant(material, alpha = 0.6):
	var new_material = material.duplicate()
	var col = new_material.albedo_color
	new_material.albedo_color = Color(col.r, col.g, col.b, alpha)
	new_material.transparency = 1
	return new_material
