ContainerPreset = {
	FILE_PRESETS = "manageContainers_containerPresets.ini",
	ILLEGAL_CHARS_PRESET_NAME = { ':', ';', ',', '=' }
}

-- Example of row of data
-- preset name=containerFilters:Ammo;Appear;Include:Exclude:containerName:testName;advanceMode:true;

function ContainerPreset:getPresets()
	local names = {};
	local file = getFileReader(ContainerPreset.FILE_PRESETS, true); -- TODO: Check if the last parameter is no longer needed

	if file == nil then
		return names
	end

	local line = file:readLine()
	-- Get each lines left hand side of the equal sign
	while line ~= nil do
		local sep = line:find("=",1,true)
		if sep ~= nil then
			local presetName = string.sub(line, 0, sep - 1)
			-- add the line from 0 to before the = seperator
			table.insert(names, presetName)
		end
		line = file:readLine()
	end
	file:close()
	return names
end

function ContainerPreset:loadPreset(presetName)
	local containerPreset = {
		containersFilters = {},
		Include={},
		Exclude={},
		containerName = "",
		advanceView=false
	}

	if presetName == nil then
		return nil
	end

	local file = getFileReader(ContainerPreset.FILE_PRESETS, true);

	if file == nil then
		error("Preset file " .. ContainerPreset.FILE_PRESETS .. " does not exist")
		return nil
	end

	local line = file:readLine()
	local presetMarker = presetName..'='

	while line ~= nil and (not line:find(presetMarker,1,true)) do
		line = file:readLine()
	end

	if line == nil then
		file:close()
		error("Preset name " .. presetName .. " was not found in the preset file:".. ContainerPreset.FILE_PRESETS)
		return nil
	end

	for name, propValue in pairs(containerPreset) do
		local presetPropName = name .. ":"
		local presetPropIndex = line:find(presetPropName,1,true)
		local searchScope = string.sub(line, presetPropIndex + string.len(presetPropName))
		local endIndex = searchScope:find(";",1,true) - 1

		local values = string.sub(searchScope, 0, endIndex)

		--print("values for "..name .." is " .. values)

		if type(propValue) == "table" then 
			values = luautils.split(values, ",")
		end

		containerPreset[name] = values
	end

	file:close()

	return containerPreset
end

function ContainerPreset:serializedContainerPreset(containerPresetData)
	local result = ""
	--print("include list:" .. table.concat(containerPresetData.Include, ", "))
	for presetName, presetData in pairs(containerPresetData) do
		result = result .. presetName .. ":"
		if type(presetData) == "table" then 
			result = result .. table.concat(presetData, ",") .. ";"
		elseif type(presetData) == "boolean" then
		  result = result .. (presetData and "true" or "false") .. ";"
		else
		  result = result .. presetData .. ";"
		end
	end
	return result
end

function ContainerPreset:writePreset(presetName, content)
	local presetMarker = presetName..'='
	local presetsContent = ""

	local file = getFileReader(ContainerPreset.FILE_PRESETS, true);

	if file ~= nil then
		local line = file:readLine()

		while line ~= nil and not line:find(presetMarker,1,true) do
			presetsContent = presetsContent .. line .. "\r\n"
			line = file:readLine()
		end

		line = file:readLine()
		while line ~= nil do
			presetsContent = presetsContent .. line .. "\r\n"
			line = file:readLine()
		end

		file:close()
	end

	presetsContent = presetsContent .. content .. "\r\n"

	-- Overwrite with changes
	local fileWriter = getFileWriter(ContainerPreset.FILE_PRESETS, true, false);
	fileWriter:write(presetsContent)
	fileWriter:close()
end

function ContainerPreset.isValidPresetName(text)
	for _, char in ipairs(ContainerPreset.ILLEGAL_CHARS_PRESET_NAME) do
		if text:contains(char) then
			return false
		end
	end
	return true
end

function ContainerPreset.getPresetNameIllegalCharsString()
	return table.concat(ContainerPreset.ILLEGAL_CHARS_PRESET_NAME, "  ")
end