


--===================================================
--								Document Texture
--===================================================	
-- used for document material and document backgrounds
function GHU_DocTexture(_parent,_width,_height,_xOff,_yOff,_level)
	local class = Class("GHU_DocTexture",_parent);
	
	local parent,width,height,xOff,yOff,level = _parent,_width,_height,_xOff,_yOff,_level;
	local textures = {};
	
	local mainFrame;
	
	local texFrames = {};
	
	
	local getTextures = function(texType) -- get all available textures of a given type
	
	end
	
	class.SetTextures = function(self,...)
		assert(#({...}) > 0,"No textures given");
		for _,tex in pairs({...}) do
			local Type = tex:GetTexType();
			textures[Type] = textures[Type] or {};
			table.insert(textures[Type],tex);
		end
				
		self.Update();
	end
	
	
	
	class.Show = function(self)
		mainFrame:Show();
	end
	class.Hide = function(self)
		mainFrame:Hide();
	end
	
	
	
	class.Update = function(self)
		if #(textures["center"] or {}) == 0 then return end
				
		-- determine the smallest texture size
		local minX,minY,minXY;
		for _,cat in pairs(textures) do
			for _,tex in pairs(cat or {}) do
				minX = min(minX or tex:GetWidth(),tex:GetWidth());
				minY = min(minY or tex:GetHeight(),tex:GetHeight());
			end
		end
		minXY = min(minX,minY);
		
		-- calculate the used size of each texture and the amount of textures
		local cX = max(math.floor((width/minXY)+0.5),3);
		local cY = max(math.floor((height/minXY)+0.5),3);
		
		local xSize,ySize = math.floor(width/cX),math.floor(height/cY);
		
				
		-- calculate the needed frames
		local framesNeeded = cX*cY;
		
		-- create evt missing frames
		while (#(texFrames) < framesNeeded) do
			local f = CreateFrame("Frame",mainFrame);		
			f:SetFrameStrata("BACKGROUND");
			f:SetFrameLevel(level);
			table.insert(texFrames,f);
		end
		
		-- place frames
		for iX = 1,cX do
			for iY = 1,cY do
				local f = texFrames[(iX-1)*cY + iY];
				f:SetWidth(xSize);
				f:SetHeight(ySize);
				-- evt clear all points?
				f:ClearAllPoints();
				f:SetPoint("TOPLEFT",mainFrame,"TOPLEFT",(iX-1)*xSize,-(iY-1)*ySize);
				
				-- decide and set texture
				local texType;
				if iX == 1 and iY == 1 then -- topleft
					texType = "topleft";
				elseif iX == cX and iY == 1 then -- topright		
					texType = "topright";		
				elseif iX == 1 and iY == cY then -- bottomleft
					texType = "bottomleft";
				elseif iX == cX and iY == cY then -- bottomright
					texType = "bottomright";
				elseif iX == 1 then -- left
					texType = "left";
				elseif iX == cX then -- right
					texType = "right";
				elseif iY == 1 then -- top
					texType = "top";
				elseif iY == cY then -- bottom
					texType = "bottom";
				end
				
				if not(texType) or not(textures[texType]) then
					texType = "center";				
				end
				
				local tex = textures[texType][random(#( textures[texType]))]; -- use a random of the fitting textures
				tex:ApplyTexture(f);				
				f:Show();
			end		
		end
		
		-- hide eventual unused frames
		for i=framesNeeded+1,#(texFrames) do
			if texFrames[i] then
				texFrames[i]:Hide();
			end	
		end
		
					
	end
	
	mainFrame = CreateFrame("Frame",parent);
	mainFrame:SetHeight(height);
	mainFrame:SetWidth(width);
	mainFrame:SetPoint("TOPLEFT",parent,"TOPLEFT",xOff,yOff)
	
	return class;
end

local textureTypes = {
	top=true,bottom=true,left=true,right=true,
	topleft=true,topright=true,buttomleft=true,buttomright=True,
	center=true,all=true,
};
function GHU_Texture(_path,_width,_height,_type)
	assert(type(_path)=="string" and type(_width)=="number" and type(_height)=="number","Useage Texture(parent,path,width,height)");
	if not(type(_types)=="table") then
		_types = {"center"}; --default
	end

	assert(textureTypes[_type]==true,"Unknown type "..(_type or "nil"));	
	
	
	local class = Class("GHU_Texture",nil);
	
	local path,width,height,Type = _path,_width,_height,_type;
	
	
	class.GetWidth = function(self) return width; end;
	class.GetHeight = function(self) return height; end;
	
	class.GetTexType = function(self) return Type; end;
	
	class.IsTexType = function(self,str)
		assert(type(textureTypes[str])==true,"Unknown texture type lookup");
		return str == Type;
	end;
		
	class.ApplyTexture = function(self,frame)
		if not(frame.texture) then
			frame.texture = frame:CreateTexture(nil,"BACKGROUND")
		end
		frame.texture:SetTexture(path)
		frame.texture:SetAllPoints(frame)

	end
	
	return class;
end


--===================================================
--								Document Texture Data
--===================================================	
-- Textures for document material and backgrounds

--[[ Glossary:
 - Material: The texture right behind the text. e.g. parchment
 - Background: The background behind the material. E.g. a wooden board or a Color
 - Template: A whole ghu_document set up for a specific purpose
 - Collection: A set of textures, forming a meterial or background

--]]


local textures = {};
local collections = {};

function GHU_GetCollectionCats()

end

function GHU_GetCollectionNames(cat)

end

function GHU_GetCollection(catagory,name) -- gives all texture objects in the collection
	for cat,cols in pairs(collections) do
		if cat == catagory then
			for _,col in pairs(cols) do
				if col.name == name then
					local t={};
					for i=1,#(col) do
						local path = col[i].path;
						if path then
							local width,height = 64,64;
							if textures[path] then
								width,heigh = textures[path][1] or 64,textures[path][2] or 64; -- 64x64 are standard
							end
							t[i] = GHU_Texture(path,width,height,col[i].type);
						end
					end
					return unpack(t);			
				end
			end	
		end
	end
end

local col = { --Requirement. All textures must be squared
	["material"] = {
		{	name="Parchment 1",
			{path="Interface\\Icons\\Spell_Holy_WordFortitude",type="topleft"},
			{path="Interface\\Icons\\Spell_Arcane_Arcane02",type="center"},
		},
		{	name="Parchment 2",
			{path="INTERFACE\\GLUES\\MODELS\\UI_BLOODELF\\bloodelf_mountains",type="center"},
		},
	},
	
	["background"] = {
		{	name="Green",
			{path="INTERFACE\\RAIDFRAME\\UI-RaidFrame-GroupBg",type="center"},
		},
	},
	
};
collections = col;

local tex = { -- this can be auto generated, but might give a large memory use
	["INTERFACE\\GLUES\\MODELS\\UI_BLOODELF\\bloodelf_mountain"]={256,256},	
	["INTERFACE\\RAIDFRAME\\UI-RaidFrame-GroupBg"]={256,256},	
};

textures = tex;