const CLASS_TILE = "tile"
const CLASS_OBJECT = "object"
const CLASS_FLOOR_OBJECT = "floor_object"

var level = null
var editor = null

func _init(level_scene):
	self.level = level_scene


func place_tile(position, name, rotation, material_set = ""):
	var element_holder = self.level.get_element(position)

	if element_holder.tile.is_present():
		#self._notify_removal(tile_holder, position)
		element_holder.tile.clear()

	var new_element = self.place_element(position, name, rotation, 0, self.level.tiles_layer, element_holder, element_holder.tile, material_set)


func place_object(position, name, rotation, material_set = ""):
	var element_holder = self.level.get_element(position)

	if element_holder.object.is_present():
		#self._notify_removal(tile_holder, position)
		element_holder.object.clear()

	var new_element = self.place_element(position, name, rotation, 0, self.level.objects_layer, element_holder, element_holder.object, material_set)


func place_floor_object(position, name, rotation, material_set = ""):
	var element_holder = self.level.get_element(position)

	if element_holder.floor_object.is_present():
		#self._notify_removal(tile_holder, position)
		element_holder.floor_object.clear()

	var new_element = self.place_element(position, name, rotation, 0, self.level.floor_objects_layer, element_holder, element_holder.floor_object, material_set)


func place_element(position, name, rotation, vertical_offset, layer, element_holder, sub_element_holder, material_set = ""):
	var new_element = self.level.templates.get_template_scene(name)
	var world_position = self.level.level_to_local(position)
	
	layer.add_child(new_element)
	
	new_element.set_position(world_position)
	new_element.set_rotation(Vector3(0, deg_to_rad(rotation), 0))
	if material_set != null and material_set != "":
		var tile_material_top = self.level.templates.get_tile_material(material_set, "top", false)
		var tile_material_wall = self.level.templates.get_tile_material(material_set, "wall", false)
		new_element.material_set = material_set
		new_element.set_material(0, tile_material_top)
		new_element.set_material(1, tile_material_wall)
	new_element.current_rotation = rotation
	

	sub_element_holder.set_element(new_element)

	return new_element	


func clear_element_layer(position, element_class = ""):
	var elem = self.level.get_element(position, true)

	if elem == null:
		return

	if elem.tile.is_present() and (element_class == self.CLASS_TILE or element_class == ""):
		#self._notify_removal(tile.unit, position, self.map.builder.CLASS_UNIT, tile.unit.tile.side, {}, false)
		elem.tile.clear()
	if elem.object.is_present() and  (element_class == self.CLASS_OBJECT or element_class == ""):
		#self._notify_removal(tile.unit, position, self.map.builder.CLASS_UNIT, tile.unit.tile.side, {}, false)
		elem.object.clear()
	if elem.floor_object.is_present() and  (element_class == self.CLASS_FLOOR_OBJECT or element_class == ""):
		#self._notify_removal(tile.unit, position, self.map.builder.CLASS_UNIT, tile.unit.tile.side, {}, false)
		elem.floor_object.clear()


func wipe_level():
	for elem_id in self.level.level_chunks.keys():
		self.level.level_chunks[elem_id].wipe()


func fill_level_from_data(data):
	var elem_data = data["tiles"]
	
	if data.has("metadata"):
		self.level.metadata = data["metadata"]
	
	#elem_id: position of element, like '59_23_2'
	for elem_id in self.level.elem_ids.keys():
		if elem_data.has(elem_id):
			self.place_element0(elem_id, elem_data[elem_id])


func place_element0(elem_id, elem_data):
	var element_holder = self.level.get_element_from_string(elem_id)
	if element_holder == null:
		return
	

	if elem_data["tile"]["name"] != null:
		self.place_tile(element_holder.position, elem_data["tile"]["name"], elem_data["tile"]["rotation"], elem_data["tile"]["material_set"])
	if elem_data["object"]["name"] != null:
		self.place_object(element_holder.position, elem_data["object"]["name"], elem_data["object"]["rotation"])
	if elem_data["floor_object"]["name"] != null:
		self.place_floor_object(element_holder.position, elem_data["floor_object"]["name"], elem_data["floor_object"]["rotation"])


#func _notify_removal position, tile_class, side=null, modifiers={}, double=true):
#    if self.editor != null:
#        self.editor.notify_about_removal({
#            "type" : "remove",
#            "class" : tile_class,
#            "position" : position,
#            "tile" : tile_fragment.tile.template_name,
#            "rotation" : tile_fragment.tile.current_rotation,
#            "side" : side,
#            "modifiers" : modifiers,
#            "double" : double
#        })




