--[[

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

--]]


-- global save vars
MonkeyQuestVars = {};

-- per character save vars
MonkeyQuestTable = {};		-- used for specific quest entry options, like is it hidden?

-- unsaved vars
MonkeyQuestPings = {};

--[[
MonkeyQuestPings = {
	["quest title"] = {
		frame = frame,		-- reference to the frame to "ping"
		timer = 1,			-- time to go before the ping is done
		fadeIn = true,		-- true: fading in, false: fading out
	},
};

--]]



function MonkeyQuest_Init()

	MonkeyQuestTitleBtn_Txt:SetText(MONKEYQUEST_FRAME_TITLE);

	local MonkeyQuestDefaults = {
		frameLocked = {
			default = false,
			},
		showTitleNumQuests = {
			default = true,
			},
		showTitleName = {
			default = true,
			},
		rightClickOpensConfig = {
			default = true,
			},
		showQuestLevels = {
			default = true,
			},
		showQuestDifficultyColour = {
			default = true,
			},
		showCurrentZone = {
			default = true,
			},
		showUnitTooltip = {
			default = true,
			},
		showObjectivePings = {
			default = true,
			},
		showObjectiveColour = {
			default = true,
			},
		sortAscending = {
			default = true,
			},
        sortHeadersAscending = {
			default = true,
			},
		showAllQuests = {
		    default = false,
		    hideFromBuddy = true,
			},
		borderColour = {
		    order = 90,
			default = {r = TOOLTIP_DEFAULT_COLOR.r, g = TOOLTIP_DEFAULT_COLOR.g, b = TOOLTIP_DEFAULT_COLOR.b, a = 1},
			},
		zoneColour = {
		    order = 80,
			default = {r = 1, g = 1, b = 1, a = 1},
			},
		pingColour = {
		    order = 70,
			default = {r = 1, g = 0, b = 0},
			},
		questTitleColour = {
		    order = 60,
			default = {r = 1, g = 1, b = 1},
			},
		objectiveColour0 = {
		    order = 10,
			default = {r = 1, g = 0, b = 0},
			},
		objectiveColour0plus = {
		    order = 20,
			default = {r = 0, g = 0, b = 1},
			},
		objectiveColour50 = {
		    order = 30,
			default = {r = 1, g = 1, b = 0},
			},
		objectiveColour100minus = {
		    order = 40,
			default = {r = 0.5, g = 0.75, b = 0.25},
			},
		objectiveColour100 = {
		    order = 50,
			default = {r = 0, g = 1, b = 0},
			},
		frameAlpha = {
			default = 255,
			min = 63,
			max = 255,
			step = 1,
			},
		backdropAlpha = {
			default = 255,
			min = 0,
			max = 255,
			step = 1,
			},
		showQuestTags = {
		    default = "All",
		    list = {"All", "None", "Only completed"},
			},
	};

	MonkeyQuest_CreateListFrames();
	MonkeyAPI_InitMonkeyTable(MONKEYQUEST_TITLE, MonkeyQuestDefaults, MonkeyQuestVars);
	
	-- finally, sort the table
	--table.sort(MonkeyQuestVars, MonkeyAPI_CompareOrderAscending);
	
	MonkeyBuddy_CreateOptions(MONKEYQUEST_TITLE);

	--MonkeyQuest_CreateQuestTable();
	MonkeyQuest_BlizzRefresh();
	

	if (DEFAULT_CHAT_FRAME) then
		DEFAULT_CHAT_FRAME:AddMessage(MONKEYQUEST_LOADED_MSG);
	end
end


function MonkeyQuest_CreateListFrames()

	local height = 0;
	local firstFrame = true;
	local newFrame;

	-- create the scroll frame
	local prevFrame = MonkeyQuestListScrollFrame_ScrollChild;

	for i = 1, MAX_QUESTLOG_QUESTS, 1 do

		newFrame = CreateFrame("Frame", "MonkeyQuestListHeader" .. i, MonkeyQuestListScrollFrame_ScrollChild, "MonkeyQuestListHeader");
		newItem = CreateFrame("Button", "MonkeyQuestListItem" .. i, MonkeyQuestListScrollFrame_ScrollChild, "MonkeyQuestListItem");

		
		getglobal("MonkeyQuestListHeader" .. i .. "_Title_Txt"):SetText("Zone Header " .. i);
		getglobal("MonkeyQuestListItem" .. i .. "_Txt"):SetText("Quest Item " .. i);

		if (firstFrame == true) then
			newFrame:SetPoint("TOPLEFT", prevFrame, "TOPLEFT", 0, 0);
			firstFrame = false;
		else
			newFrame:SetPoint("TOPLEFT", prevFrame, "BOTTOMLEFT", -24, -1);
		end

		newItem:SetPoint("TOPLEFT", getglobal("MonkeyQuestListHeader" .. i .. "_Children"), "BOTTOMLEFT", 24, 0);

		newFrame:SetPoint("RIGHT", MonkeyQuestListScrollFrame_ScrollChild, "RIGHT", 0, 0);
		newItem:SetPoint("RIGHT", MonkeyQuestListScrollFrame_ScrollChild, "RIGHT", 0, 0);

		height = height + newFrame:GetHeight() + 1;

		prevFrame = newItem;
		
		--MonkeyAPI_DebugMsg("MonkeyQuestListHeader" .. i .. " created.");
	end

	
	MonkeyQuestListScrollFrame_ScrollChild:SetHeight(height - 8);
	MonkeyQuestListScrollFrame:UpdateScrollChildRect();

end
