--[[

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

--]]


local MonkeyQuestLocal_drag = {
	started = false,
	};


--[[

	This function will open the quest log if the user is not trying to drag a
	quest.

	@return		nil
--]]
function MonkeyQuestListItem_OnClick(arg1)

	if (not IsShiftKeyDown() and arg1 == "LeftButton") then
	
	    -- if MonkeyLog is installed, open that instead
		if (MonkeyLog_SetQuest ~= nil) then
			if (MonkeyLog_Main_Frame:IsVisible()) then
				if (MonkeyLog_global_iCurrQuest == this.blizzIndex) then
					MonkeyLog_Main_Frame:Hide();
					return;
				end
			end
			MonkeyLog_SetQuest(this.blizzIndex);
			return;
		end
	
	    local numEntries, numQuests = GetNumQuestLogEntries();


		-- show the real questlog
		ShowUIPanel(QuestLogFrame);

		-- check if there's even a need to mess with the offset
		if (numEntries > QUESTS_DISPLAYED) then

			-- move the real quest log list scrollbar to the correct place
			if (this.blizzIndex < numEntries - QUESTS_DISPLAYED) then
				FauxScrollFrame_SetOffset(QuestLogListScrollFrame, this.blizzIndex - 1);
				QuestLogListScrollFrameScrollBar:SetValue((this.blizzIndex - 1) * QUESTLOG_QUEST_HEIGHT);
			else
				FauxScrollFrame_SetOffset(QuestLogListScrollFrame, numEntries - QUESTS_DISPLAYED);
				QuestLogListScrollFrameScrollBar:SetValue((numEntries - QUESTS_DISPLAYED) * QUESTLOG_QUEST_HEIGHT);
			end
		end

		-- actually select the quest entry
		SelectQuestLogEntry(this.blizzIndex);
		QuestLog_SetSelection(this.blizzIndex);

		-- update the real quest log
		QuestLog_Update();
		
	end
end

--[[
	This function will start the quest drag if shift is down

	@return		nil
--]]
function MonkeyQuestListItem_OnMouseDown(arg1)

    if (IsShiftKeyDown() and arg1 == "LeftButton") then
        MonkeyAPI_DebugMsg("MonkeyQuestListItem_OnMouseDown()");
        
        MonkeyQuestLocal_drag.started = true;
        MonkeyQuestLocal_drag.startHeaderKey = this.headerKey;
        MonkeyQuestLocal_drag.startQuestKey = this.questKey;
        
    elseif (not IsShiftKeyDown() and arg1 == "RightButton") then

	    -- hide the quest if that option is enabled
		-- TODO monkey var check
		if (getglobal(this:GetName() .. "_Check"):GetChecked()) then
		    MonkeyQuestTable[this.headerKey].quests[this.questKey].visible = false;
		else
	    	MonkeyQuestTable[this.headerKey].quests[this.questKey].visible = true;
		end

		MonkeyQuest_Render();

		MonkeyAPI_DebugMsg("Hiding a quest");
	end
end

--[[
	This function will figure out where, if anywhere, to drop the quest that was
	being dragged, if indeed any quest was being dragged

	@return		nil
--]]
function MonkeyQuestListItem_OnMouseUp(arg1)
    MonkeyAPI_DebugMsg("MonkeyQuestListItem_OnMouseUp()");
    
    if (MonkeyQuestLocal_drag.started == false) then
    	return;
    end
    
    if (MouseIsOver(MonkeyQuestFrame) and 
		MonkeyQuestLocal_drag.endHeaderKey ~= MonkeyQuestLocal_drag.startHeaderKey) then
		
		
		local questTitle = MonkeyQuestTable[MonkeyQuestLocal_drag.startHeaderKey].quests[MonkeyQuestLocal_drag.startQuestKey].questTitle;
		
		
		-- move the quest entry
    	table.insert(MonkeyQuestTable[MonkeyQuestLocal_drag.endHeaderKey].quests, 
			table.remove(MonkeyQuestTable[MonkeyQuestLocal_drag.startHeaderKey].quests, MonkeyQuestLocal_drag.startQuestKey));
    		
    	MonkeyQuest_SortQuestTable();
    	MonkeyQuest_Render();
    	
    	MonkeyQuest_PingQuest(questTitle);
    end
    
    MonkeyQuestLocal_drag.started = false;
end

--[[
	This function will always keep track of the last quest hovered over, used to
	figure out where to drop the quest

	@return		nil
--]]
function MonkeyQuestListItem_OnEnter()
    --MonkeyAPI_DebugMsg("MonkeyQuestListItem_OnEnter(): " .. MonkeyQuestTable[this.headerKey].quests[this.questKey].questTitle);
    
    MonkeyQuestLocal_drag.endHeaderKey = this.headerKey;
    MonkeyQuestLocal_drag.endQuestKey = this.questKey;
end

--[[
	This function will always keep track of the last quest hovered over, used to
	figure out where to drop the quest

	@param      the mouse button
	
	@return		nil
--]]
function MonkeyQuestListHeader_OnMouseUp(arg1)
    MonkeyAPI_DebugMsg("MonkeyQuestListHeader_OnMouseUp()");

    if (MonkeyQuestLocal_drag.started == false) then
    	return;
    end

    if (MouseIsOver(MonkeyQuestFrame) and
		MonkeyQuestLocal_drag.endHeaderKey ~= MonkeyQuestLocal_drag.startHeaderKey) then


		local questTitle = MonkeyQuestTable[MonkeyQuestLocal_drag.startHeaderKey].quests[MonkeyQuestLocal_drag.startQuestKey].questTitle;


		-- move the quest entry
    	table.insert(MonkeyQuestTable[MonkeyQuestLocal_drag.endHeaderKey].quests,
			table.remove(MonkeyQuestTable[MonkeyQuestLocal_drag.startHeaderKey].quests, MonkeyQuestLocal_drag.startQuestKey));

    	MonkeyQuest_SortQuestTable();
    	MonkeyQuest_Render();

    	MonkeyQuest_PingQuest(questTitle);
    end

    MonkeyQuestLocal_drag.started = false;
end

--[[
	This function will always keep track of the last quest hovered over, used to
	figure out where to drop the quest

	@return		nil
--]]
function MonkeyQuestListHeader_OnEnter()

    --MonkeyAPI_DebugMsg("MonkeyQuestListHeader_OnEnter(): " .. MonkeyQuestTable[this.headerKey].headerTitle);
    
    MonkeyQuestLocal_drag.endHeaderKey = this.headerKey;
end

--[[
	This function will always keep track of the last quest hovered over, used to
	figure out where to drop the quest

	@return		nil
--]]
function MonkeyQuestListHeader_Title_OnEnter()

    --MonkeyAPI_DebugMsg("MonkeyQuestListHeader_OnEnter(): " .. MonkeyQuestTable[this:GetParent().headerKey].headerTitle);

    MonkeyQuestLocal_drag.endHeaderKey = this:GetParent().headerKey;
end


--[[
	This function will set the Quest's visible flag

	@return		nil
--]]
function MonkeyQuestListItem_Check_OnClick()

	if (this:GetChecked()) then
	    MonkeyQuestTable[this:GetParent().headerKey].quests[this:GetParent().questKey].visible = true;
	else
    	MonkeyQuestTable[this:GetParent().headerKey].quests[this:GetParent().questKey].visible = false;
	end
	
	MonkeyQuest_Render();

	MonkeyAPI_DebugMsg("Hiding a quest");
end


--[[
	This function will ping a quest by it's name. Optionally, if you know the
	actual quest frame you can send that along too.
	
	@param      the pure string of the quest title
	@param      optional, the actual frame the quest is in

	@return		nil
--]]
function MonkeyQuest_PingQuest(strQuest, frame)

    local questFrame;
    
    
	if (frame == nil) then
		--questFrame = getglobal("MonkeyQuestListItem" .. questTable.frameIndex);
		questFrame = MonkeyQuest_GetQuestItemFrameByTitle(strQuest);
	else
	    questFrame = frame;
	end
	
	if (questFrame == nil) then
		return;
	end
	
	
    questFrame.ping = true;

	if (MonkeyQuestPings[strQuest] == nil) then
		MonkeyQuestPings[strQuest] = {};
	end

	MonkeyQuestPings[strQuest].frame = questFrame;
	MonkeyQuestPings[strQuest].timer = 1;
	MonkeyQuestPings[strQuest].fadeIn = true;
end

--[[
	This function will process all the pings currently active.

	@param      the time since the last screen "frame"

	@return		nil
--]]
function MonkeyQuest_ProcessPings(time)

	local key, value;
	local pingColour = MonkeyQuestVars.pingColour.current;


	for key, value in pairs(MonkeyQuestPings) do

		value.frame:LockHighlight();
		value.timer = value.timer - time;

		if (value.timer < 0) then
			value.timer = 0;
		end

		if (value.fadeIn == true) then
			-- fading in
			value.frame:GetHighlightTexture():SetVertexColor(pingColour.r, pingColour.g, pingColour.b, 1 - value.timer);

			if (value.timer == 0) then
				-- start fading out
				value.timer = 1;
				value.fadeIn = false;
			end
		else
			-- fading out
			value.frame:GetHighlightTexture():SetVertexColor(pingColour.r, pingColour.g, pingColour.b, value.timer);

			if (value.timer == 0) then
				-- done
				value.frame:GetHighlightTexture():SetVertexColor(pingColour.r, pingColour.g, pingColour.b, 1);
				value.frame.ping = nil;
				MonkeyQuestPings[key] = nil;

				MonkeyQuest_Render();
			end
		end
	end
end

--[[
	This function will find a quest frame given the quest title

	@param      the quest title

	@return		the quest frame
--]]
function MonkeyQuest_GetQuestItemFrameByTitle(strTitle)

	-- find the frame with this quest title
	local frame;
	
	for i = 1, MAX_QUESTLOG_QUESTS, 1 do
	
	    frame = getglobal("MonkeyQuestListItem" .. i);
	    
	    if (frame.questTitle == strTitle) then
	        return frame;
	    end
	end
	
	return nil;
end
