require "libs/TSerial"

local targetFolder = "art"
local reanalyzeCells = true

function getAllAssets()
	local objectnames = {}
	local topFolder = "art"
	local files = love.filesystem.getDirectoryItems(topFolder)
	for _, folder in pairs(files) do
		if string.sub(folder, 1, 1) ~= "_" then
			-- we ignore directories which are prepended w/ an underscore
			if not string.find(folder, "%.") then
				objectnames[folder] = {}
				-- love.filesystem.createDirectory("art/" .. folder)
				-- this is a folder, probably
				local newFiles = love.filesystem.getDirectoryItems(topFolder .. "/" .. folder)
				for _, fileToAnalyze in pairs(newFiles) do
					if fileToAnalyze:find("_r.png") or fileToAnalyze:find("_g.png") or fileToAnalyze:find("_b.png")then
						local color
						if fileToAnalyze:find("_r.png") then color = "red"
						elseif fileToAnalyze:find("_g.png") then color = "green"
						elseif fileToAnalyze:find("_b.png") then color = "blue" end

						local name = fileToAnalyze:sub(1, fileToAnalyze:len() - 6)

						if not globalAssetProperties[name] then 
							globalAssetProperties[name] = {}
							globalAssetProperties[name].directory = folder
							globalAssetProperties[name].sprites = {}
							globalAssetProperties[name].class = "unmoveable"
							globalAssetProperties[name].preload = false
						end

						local sprite = topFolder .. "/" .. folder .. "/" .. fileToAnalyze
						globalAssetProperties[name].sprites[color] = sprite

						if not globalAssetProperties[name].cells or reanalyzeCells then
							globalAssetProperties[name].cells = {}
							globalAssetProperties[name].cells[color] = getCellsFromSprite(love.graphics.newImage(sprite))
						end 
					end
				end
			end
		end
	end

	str = TSerial.pack(globalAssetProperties, nil, true)
	love.filesystem.write("assets.prop", str)
	return globalAssetProperties
end