--[[

	MonkeyAPI:
	Common functions for the MonkeyMods
	
	Website:	www.toctastic.net
	Author:		Trentin
	Email:		monkeymods@gmail.com

--]]

--[[

	This function extracts the ARGB values out of a colour encoded string.

	@param	strColour	the string with the colour info.
	
	@return		the alpha value
	@return		the red value
	@return		the green value
	@return		the blue value

--]]
function MonkeyAPI_ColourStrToARGB(strColour)
	-- "|cFFFFFFFF"
	local i = 3;
	
	local iAlpha = tonumber(string.sub(strColour, i, i + 1), 16);
	local iRed = tonumber(string.sub(strColour, i + 2, i + 3), 16);
	local iGreen = tonumber(string.sub(strColour, i + 4, i + 5), 16);
	local iBlue = tonumber(string.sub(strColour, i + 6, i + 7), 16);
	
	iAlpha = iAlpha / 255;
	iRed = iRed / 255;
	iGreen = iGreen / 255;
	iBlue = iBlue / 255;
	
	--DEFAULT_CHAT_FRAME:AddMessage("A = "..iAlpha.." R = "..iRed.." G = "..iGreen.." B = "..iBlue);
	
	return iAlpha, iRed, iGreen, iBlue;
end

--[[

	This function converts RGBA values into a colour encoded string.

	@param		the red value
	@param		the green value
	@param		the blue value
	@param		the alpha value

	@return		the string colour encoded
--]]
function MonkeyAPI_RGBAToColourStr(r, g, b, a)
	-- "|cFFFFFFFF"
	local strColour;
	
	
	-- floor it so it doesn't go over 255
	if (a ~= nil) then
		a = floor(a * 255);
	else
		a = 255;
	end
	
	r = floor(r * 255);
	g = floor(g * 255);
	b = floor(b * 255);
	
	strColour = format("|c%2x%2x%2x%2x", a, r, g, b);

	return strColour;
end


function MonkeyAPI_DebugMsg(strMsg)

	if (MONKEYAPI_DEBUG) then
		if (DEFAULT_CHAT_FRAME) then
			if (strMsg ~= nil) then
				DEFAULT_CHAT_FRAME:AddMessage("MonkeyLib: " .. strMsg);
			else
				DEFAULT_CHAT_FRAME:AddMessage("MonkeyLib: nil");
			end
		end
	end
end


function MonkeyAPI_InitMonkeyTable(modName, defaultTable, varsTable)
	
	MonkeyAPI_DebugMsg("MonkeyAPI_InitMonkeyTable");

	-- run through the Default table and check if the vars are in the saved addon vars table
	for key, value in pairs(defaultTable) do

		-- first check if the key even exists yet in the vars table, create it if it doesn't
		if (varsTable[key] == nil) then
			--MonkeyAPI_DebugMsg("new var " .. tostring(key));
			varsTable[key] = value;
		else
			-- the vars exist but we should reset min and max values
			if (value.min ~= nil) then
				varsTable[key].min = value.min;
			end
			if (value.max ~= nil) then
				varsTable[key].max = value.max;
			end
		end

		-- next check if the key current value even exists yet, set it to the default if it doesn't
		if (varsTable[key].current == nil) then
			--MonkeyAPI_DebugMsg("set current " .. tostring(key));
			varsTable[key].current = value.default;
		end
		
		-- set some values that may or may not be there, but that the user can't change
		varsTable[key].hideFromBuddy = value.hideFromBuddy;
		
		if (value.order ~= nil) then
			varsTable[key].order = value.order;
		else
		    varsTable[key].order = 0;
		end
	end
	
	-- run through the Default table and execute callbacks
	for key, value in pairs(defaultTable) do

		-- finally, see if this var has a callback function and call it if it does
		local func = getglobal(modName .. "_" .. tostring(key) .. "_Changed");

		if (func ~= nil) then
			--MonkeyAPI_DebugMsg("call " .. modName .. "_" .. tostring(key) .. "_Changed");
			func();
		end
	end
	
	MonkeyAPI_DebugMsg("done MonkeyAPI_InitMonkeyTable for " .. modName);
end


function MonkeyAPI_PairsBy_order(t, f)
	local a = {}
	for n in pairs(t) do
		table.insert(a, n)
	end

	table.sort(a, function (var1, var2)
	                    if (t[var1] == nil or t[var2] == nil) then
							return false;
						end

						if (t[var1].order == nil or t[var2].order == nil) then
							return false;
						end

					    if (t[var1].order < t[var2].order) then
							return true;
						end

						return false;
					end);

	local i = 0      -- iterator variable
	local iter = function ()   -- iterator function

					i = i + 1
					if a[i] == nil then
						return nil
					else
						return a[i], t[a[i]]
					end
				end


	return iter
end
