-------------------------------------------------------------------------------------
-- Компонент редактора отвечающий за создание и удаление спрайтов
-- AA 2013
-------------------------------------------------------------------------------------

class 'SpriteManagement' (Node)

function SpriteManagement:__init(game_scene)
	Node.__init(self, game_scene:getMainEntity(), 'SpriteManagement', 1)
	self.game_scene = game_scene
	self.created_sprites = {}
	self.removed_sprites = {}
	self.sprites_counter = 0
	self.object_description = "\t\t{ name = '%s',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  editingLocked = false,  treeItemIsExpanded = false,  width = 100,  height = 100, color = {r = 1, g = 1, b = 1, a = 0.00392157}, },"

	if self.CreateLoaderTable then
		local table_resource = self:CreateLoaderTable()
		LoadScene(self.main_entity, table_resource)
	end

	self:init_buttons()
	self:set_key_binds()
end

-------------------------------------------------------------------------------------
function SpriteManagement:set_key_binds()
	self.game_scene:getWindow():getHotkeySignal(ERSK_DELETE):removeAll()
	self.game_scene:getWindow():getHotkeySignal(ERSK_DELETE):add(function() 
		self:remove_sprite()
	end)
end

-------------------------------------------------------------------------------------
function SpriteManagement:on_scene_start()
	self.created_sprites = {}
	self.removed_sprites = {}
end

-------------------------------------------------------------------------------------
function SpriteManagement:init_buttons()
	self:getEntityNamed('create_sprite_button').onPress:add(function()
		self:create_sprite()
	end)

	self:getEntityNamed('remove_sprite_button').onPress:add(function()
		self:remove_sprite()
	end)	
end

-------------------------------------------------------------------------------------
function SpriteManagement:create_sprite()
	local playfield = self.game_scene:getPlayField()
	self.sprites_counter = self.sprites_counter + 1
	local sprite_name = 'area_click_'..self.sprites_counter
	while playfield:hasEntityNamed(sprite_name) do
		self.sprites_counter = self.sprites_counter + 1
		sprite_name = 'area_click_'..self.sprites_counter
	end

	local top_zoom_zone = playfield.active_zones:GetTop()
	local parent = top_zoom_zone and top_zoom_zone:getEntityAnchor() or playfield:getMainEntity()
	local entity = CreateSpriteEntity('')
	entity:setName(sprite_name)
	entity:attachTo(parent)
	entity:setSize(100, 100)
	entity:setXY(512, 384)
	entity:setColor(1, 1, 1, 0.003)

	if self.game_scene.is_objects_highlighted then
		playfield:highlightObject(entity)
	end

	table.insert(self.created_sprites, sprite_name)
	self.game_scene:getIngameEditor():on_sprite_created(entity)
end

-------------------------------------------------------------------------------------
function SpriteManagement:remove_sprite()
	local current_selected_object = self.game_scene:getIngameEditor():get_selection():get_current_selected_object()
	if current_selected_object then
		if table.has_value(self.created_sprites, current_selected_object:getName()) then
			table.remove_by_value(self.created_sprites, current_selected_object:getName())
		else
			table.insert(self.removed_sprites, current_selected_object:getName())
		end
		self.game_scene:getIngameEditor():on_sprite_removed()
		current_selected_object:hide()
	end
end

-------------------------------------------------------------------------------------
function SpriteManagement:save()
	local script_lines = {}
	local loader_table_lines = {}
	local playfield = self.game_scene:getPlayField()
	local top_zoom_zone = playfield.active_zones:GetTop()
	local script_path = top_zoom_zone and playfield:getZoomZonePath(top_zoom_zone:getName()) or playfield:getPathLua()

	local parsing_loader_table = false
	for line in io.lines(script_path) do
		if string.find(line, 'CreateLoaderTable') or parsing_loader_table then
			parsing_loader_table = true
			table.insert(loader_table_lines, line)
		else
			table.insert(script_lines, line)
		end
	end

	-- delete removed sprites
	for key, line in pairs(loader_table_lines) do
		for _, sprite_name in pairs(self.removed_sprites) do
			if string.find(line, 'name = \''..sprite_name..'\'') then
				loader_table_lines[key] = nil
			end
		end
	end

	-- insert created sprites
	for _, object_name in pairs(self.created_sprites) do
		table.insert(loader_table_lines, #loader_table_lines - 2, string.format(self.object_description, object_name))
	end

	local script_file = io.open(script_path, 'w+')
	for key, line in pairs(script_lines) do
		script_file:write(line..'\n')
	end
	for key, line in pairs(loader_table_lines) do
		script_file:write(line..'\n')
	end
	script_file:close()

	self.created_sprites = {}
	self.removed_sprites = {}
end

-------------------------------------------------------------------------------------
function SpriteManagement:enable()
	self:getEntityNamed('sprite_buttons_parent'):show()
end

-------------------------------------------------------------------------------------
function SpriteManagement:disable()
	self:getEntityNamed('sprite_buttons_parent'):hide()
end


--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function SpriteManagement:CreateLoaderTable()
	local objects =
	{
		{ name = 'sprite_buttons_parent',  entityType = 'SpriteEntity',  x = 38,  y = 30,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 100,  height = 100,  },
		{ name = 'create_sprite_button',  entityType = 'ButtonEntity',  parentName = 'sprite_buttons_parent',  x = 215,  y = -15,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/IngameEditor/create_sprite.png',  text = '',  activeRectX = 80,  activeRectY = 30,  },
		{ name = 'remove_sprite_button',  entityType = 'ButtonEntity',  parentName = 'sprite_buttons_parent',  x = 300,  y = -15,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/IngameEditor/remove_sprite.png',  text = '',  activeRectX = 120,  activeRectY = 50,  },
	}
	return objects
end
