local addonName, Addon = ...;

function Addon:ShowFrame(text)
	if text and #text > 0 then
		local GossipCopyPasteFrame = _G["GossipCopyPasteFrame"];
		GossipCopyPasteFrame.ScrollFrame.ScrollText:SetText(text);
		GossipCopyPasteFrame.ScrollFrame.ScrollText:HighlightText();
		GossipCopyPasteFrame:Show();
		return true;
	end

	return false;
end

function ShowGossipCopyPasteFrame()
	local text = "";

	if QuestFrameDetailPanel:IsShown() then
		local questID = GetQuestID();
		local title = questID and "(Quest) "..C_QuestLog.GetTitleForQuestID(questID);
		if title then
			text = #text > 0 and text.."\n"..title or title;
		end

		if QuestInfoDescriptionText and QuestInfoDescriptionText:IsShown() then
			local line = QuestInfoDescriptionText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end

		if QuestInfoObjectivesHeader and QuestInfoObjectivesHeader:IsShown() then
			local line = QuestInfoObjectivesHeader:GetText();
			text = #text > 0 and text.."\n\n"..line or line;
		end

		if QuestInfoObjectivesText and QuestInfoObjectivesText:IsShown() then
			local line = QuestInfoObjectivesText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end
	elseif QuestFrameProgressPanel:IsShown() then
		if QuestProgressTitleText and QuestProgressTitleText:IsShown() then
			local line = "(Quest) "..QuestProgressTitleText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end

		if QuestProgressText and QuestProgressText:IsShown() then
			local line = QuestProgressText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end
	elseif QuestFrameRewardPanel:IsShown() then
		local questID = GetQuestID();
		local title = questID and "(Quest) "..C_QuestLog.GetTitleForQuestID(questID);
		if title then
			text = #text > 0 and text.."\n"..title or title;
		end

		if QuestInfoRewardText and QuestInfoRewardText:IsShown() then
			local line = QuestInfoRewardText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end
	elseif QuestMapFrame:IsShown() and QuestMapFrame.DetailsFrame:IsShown() then
		local questID = QuestMapFrame.DetailsFrame.questID;
		local title = questID and "(Quest) "..C_QuestLog.GetTitleForQuestID(questID);
		if title then
			text = #text > 0 and text.."\n"..title or title;
		end

		if QuestInfoObjectivesText and QuestInfoObjectivesText:IsShown() then
			local line = QuestInfoObjectivesText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end

		if QuestInfoDescriptionHeader and QuestInfoDescriptionHeader:IsShown() then
			local line = QuestInfoDescriptionHeader:GetText();
			text = #text > 0 and text.."\n\n"..line or line;
		end

		if QuestInfoDescriptionText and QuestInfoDescriptionText:IsShown() then
			local line = QuestInfoDescriptionText:GetText();
			text = #text > 0 and text.."\n"..line or line;
		end
	elseif GossipFrame:IsShown() then
		for index, frame in ipairs({GossipFrame.GreetingPanel.ScrollBox.ScrollTarget:GetChildren()}) do
			if frame.GreetingText then
				local line = frame.GreetingText:GetText();
				if line then
					text = #text > 0 and text.."\n"..line or line;
				end
			elseif frame.GetData and frame.GetOrderIndex then
				local info = frame:GetData().info;
				local orderIndex = frame:GetOrderIndex();
				if not info then
					-- Skip
				elseif info.questID then
					options[orderIndex] = "* (Quest) "..info.title;
				elseif info.name then
					options[orderIndex] = "* "..info.name;
				end
			end
		end

		local options = {};
		for orderIndex, option in pairs(options) do
			text = #text > 0 and text.."\n"..option or option;
		end	
	end

	if Addon:ShowFrame(text) then
		return;
	end

	local ItemTextFrame = _G["ItemTextFrame"];
	if ItemTextFrame:IsShown() then
		if Addon:ShowFrame(ItemTextGetText()) then
			return;
		end
	end

	local loaded, finished = C_AddOns.IsAddOnLoaded("WowheadQuickLink");
	if loaded and finished then
		_G["RunWowheadQuickLink"]();
	end
end

SetBinding("CTRL-C", "GOSSIP_COPY_PASTE_NAME");

local loadFrame = CreateFrame("Frame");
loadFrame:RegisterEvent("UPDATE_BINDINGS");
loadFrame:SetScript(
	"OnEvent",
	function(self, event, ...)
		if event == "UPDATE_BINDINGS" then
			self:UnregisterEvent("UPDATE_BINDINGS");
			SetBinding("CTRL-C", "GOSSIP_COPY_PASTE_NAME");
			self:RegisterEvent("UPDATE_BINDINGS");
		end
	end
);
