-------------------------------------------------------------------------------
-- Tools helpers
-------------------------------------------------------------------------------

class 'ToolHelpers'

-------------------------------------------------------------------------------
-- SETTERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function ToolHelpers.setToolFound(toolName)
	assert(toolName)
	AddGameState(TOOL_FOUND, toolName)
end
-------------------------------------------------------------------------------

function ToolHelpers.setToolInfoShown(toolName)
	assert(toolName)
	AddGameState(TOOL_INFO_SHOWN, toolName)
end
-------------------------------------------------------------------------------

function ToolHelpers.setToolApplied(toolName)
	assert(toolName)
	AddGameState(TOOL_APPLIED, toolName)
end
-------------------------------------------------------------------------------


-------------------------------------------------------------------------------
-- GETTERS --------------------------------------------------------------------
-------------------------------------------------------------------------------

function ToolHelpers.isToolFound(toolName)
	assert(toolName)
	return HasGameState(TOOL_FOUND, toolName)
end
-------------------------------------------------------------------------------

function ToolHelpers.isToolInfoShown(toolName)
	assert(toolName)
	return HasGameState(TOOL_INFO_SHOWN, toolName)
end
-------------------------------------------------------------------------------

function ToolHelpers.isToolApplied(toolName)
	assert(toolName)
	return HasGameState(TOOL_APPLIED, toolName)
end
-------------------------------------------------------------------------------

function ToolHelpers.isToolCollected(toolName) 
	assert(toolName)
	local collection = getCollectionTools(toolName)

	if not collection then
		return ToolHelpers.isToolFound(toolName)
	end

	return collection:is_collect_all_tools()
end
-------------------------------------------------------------------------------




-------------------------------------------------------------------------------
-- OLD ------------------------------------------------------------------------
-------------------------------------------------------------------------------

function IsToolFound(tool_name)
	return ToolHelpers.isToolFound(tool_name)
end
-------------------------------------------------------------------------------

function IsToolInfoShown(tool_name)
	return ToolHelpers.isToolInfoShown(tool_name)
end
-------------------------------------------------------------------------------

function IsToolApplied(tool_name)
	return ToolHelpers.isToolApplied(tool_name)
end
-------------------------------------------------------------------------------

-- work for collection (tool_name should be one of the tools of the collection) as well as for single tools
function IsToolCollected(tool_name) 
	return ToolHelpers.isToolCollected(tool_name)
end
-------------------------------------------------------------------------------

function GetToolsApply(level_name)
	local state_name = 'RegisterToolApply_'..level_name
	local result = {}
	
	if HasGameState(state_name) then
		for i=0, GetGameState(state_name):getSize()-1 do
			local toolName = GetGameState(state_name):get(i)
			table.insert(result, toolName)
		end
	end
	
	return result
end
-------------------------------------------------------------------------------

function IsToolsFound(toolsNames)
	for i, toolName in pairs(toolsNames) do
		if not ToolHelpers.isToolFound(toolName) then
			return false
		end
	end
	
	return true
end
-------------------------------------------------------------------------------

function IsToolsHave(tools_names, level_name)
	local level  = GetCurrentLevel()
	level_name = level_name or level.name
	local result = false
	
	for i, tool_name in pairs(tools_names) do
		local tool = level.tools[tool_name]
		if tool.global_targets then
			for i, target in pairs(tool.global_targets) do
				if IsToolFound(tool_name) and (target.level_name == level_name and not target:IsUsed()) then
					result = true
					break
				end
			end
			
			if result then
				break
			end
		else
			if IsToolFound(tool_name) and not IsToolApplied(tool_name) then
				result = true
				break
			end
		end
	end
	
	return result
end
-------------------------------------------------------------------------------

function IsToolsApplied(tools_names, level_name)
	local level  = GetCurrentLevel()
	level_name = level_name or level.name
	local result = true
	
	for i, tool_name in pairs(tools_names) do
		local tool = level.tools[tool_name]
		if tool.global_targets then
			for i, target in pairs(tool.global_targets) do
				if target.level_name == level_name and not target:IsUsed() then
					result = false
					break
				end
			end
			
			if not result then
				break
			end
		else
			if not IsToolApplied(tool_name) then
				result = false
				break
			end
		end
	end
	
	return result
end
-------------------------------------------------------------------------------