--the positions in the texture table at which each texture goes
local key_index_map =
{
	arm_armor_left = 1,
	arm_armor_right = 2,
	arm_skin_left = 3,
	arm_skin_right = 4,
	torso_armor = 5,
	hair = 6,
	head = 7,
	helmet = 8,
	leg_armor_left = 9,
	leg_armor_right = 10,
	leg_skin_left = 11,
	leg_skin_right = 12,
	torso_skin = 13,
}


local classes = {}





local function default_process(self)
	return self[1]
end


--adds functions to the mod api table
return function(classname)
	table.insert(classes, classname)
	local textures = {}

	--register a texture. Example:
	--[[
		dwarf_characters.register_texture("texture",
			{
				{
					"helmet.png",
					process =
						function(self)
							return self[1] .. "^[multiply:#"ABCDEF"
						end
				}
			})
	]]
	dwarf_characters["register_" .. classname] = function(name, def)
		assert(not textures[name], "There already is a "..
		classname ..
		" texture registered with name " .. name)

		local postprocessed = {}
		for k, t in pairs(def)
		do
			if not t.process
			then
				t.process = default_process
			end
			postprocessed[key_index_map[k]] = t
		end
		textures[name] = postprocessed
	end


	--overwrites the fields of the base with textures registered under
	--<name>. During this the process function is called with mod
	--as argument
	dwarf_characters["apply_" .. classname] = function(base, name, mod)
		if textures[name]
		then
			for k, t in pairs(textures[name])
			do
				base[k] = t:process(mod)
			end
		end
	end



	--returns a list of all texture names registered in this class
	dwarf_characters["get_" .. classname .."_list"] = function()
		local ret = {}
		local i = 0
		for name, _ in pairs(textures)
		do
			i = i + 1
			ret[i] = name
		end
		return ret
	end

	dwarf_characters["get_random_" .. classname] = function()
		local list = dwarf_characters["get_" .. classname .."_list"]()
		return list[math.random(#list)]
	end

end
