--[[

    MonkeyQuest:
    Quest tracker.
    
    Website:	www.toctastic.net
	Author:		Trentin
	Email:		monkeymods@gmail.com

--]]


function MonkeyQuest_GetQuestTable(strTitle)

	local key, value, questKey, questValue;

	for key, value in pairs(MonkeyQuestTable) do

		--MonkeyAPI_DebugMsg("Compare: " .. value.headerTitle .. " == " .. strTitle);
		if (value.headerTitle == strTitle) then
			return value;
		end

		if (value.quests ~= nil) then
			for questKey, questValue in pairs(value.quests) do
	
				if (questValue.questTitle == strTitle) then
					return questValue;
				end
			end
		end
	end

	-- not found
	MonkeyAPI_DebugMsg("Could not find: " .. strTitle);
	
	return nil;
end


function MonkeyQuest_AddNewHeader(strName, custom)

	local newHeader = {
		headerTitle = strName,
		expanded = true,
		custom = custom,
		quests = {},
		};

	table.insert(MonkeyQuestTable, newHeader);
	
	return MonkeyQuestTable[table.maxn(MonkeyQuestTable)];
end

function MonkeyQuest_CleanQuestTable()

	local key, value, questKey, questValue, i, ii;

	i = 1;
	for key, value in pairs(MonkeyQuestTable) do

		if (value.active == false and value.custom == false) then
			table.remove(MonkeyQuestTable, i);
		else
			value.active = false;
		end
		
		i = i + 1;

		if (value ~= nil and value.quests ~= nil) then
		
			ii = 1;

			for questKey, questValue in pairs(value.quests) do
	
				if (questValue.active == false) then
					table.remove(value.quests, ii);
				else
					questValue.active = false;
				end
				
				ii = ii + 1;
			end
		end
	end
end


function MonkeyQuest_SortQuestTable()

	-- first sort the quest headers
	if (MonkeyQuestVars.sortHeadersAscending.current == true) then
		table.sort(MonkeyQuestTable, MonkeyQuest_CompareHeadersAscending);
	else
		table.sort(MonkeyQuestTable, MonkeyQuest_CompareHeadersDescending);
	end

	

	local key, value;


	for key, value in pairs(MonkeyQuestTable) do

		if (value ~= nil and value.quests ~= nil) then
		
			if (MonkeyQuestVars.sortAscending.current == true) then
				table.sort(value.quests, MonkeyQuest_CompareQuestsAscending);
			else
				table.sort(value.quests, MonkeyQuest_CompareQuestsDescending);
			end
		end
	end
end

function MonkeyQuest_CompareHeadersAscending(header1, header2)

	if (header1.headerTitle < header2.headerTitle) then
		return true;
	end
	
	return false;
end

function MonkeyQuest_CompareHeadersDescending(header1, header2)

	if (header1.headerTitle > header2.headerTitle) then
		return true;
	end
	
	return false;
end

function MonkeyQuest_CompareQuestsAscending(quest1, quest2)

	if (quest1.level == quest2.level) then
		if (quest1.questTitle < quest2.questTitle) then
			return true;
		else
			return false;
		end
	end
	
	if (quest1.level < quest2.level) then
		return true;
	end
	
	return false;
end

function MonkeyQuest_CompareQuestsDescending(quest1, quest2)

	if (quest1.level == quest2.level) then
		if (quest1.questTitle > quest2.questTitle) then
			return true;
		else
			return false;
		end
	end
	
	if (quest1.level > quest2.level) then
		return true;
	end
	
	return false;
end
