local RewardIcons_DisableBountyColors = false
local RewardIcons_Size = 36

local GetCurrentMapID, floor, pairs = 
	  function() return WorldMapFrame:GetMapID() or 0 end, floor, pairs
local IsQuestComplete, IsQuestCriteriaForBounty = C_QuestLog.IsComplete, C_QuestLog.IsQuestCriteriaForBounty
local function GetQuestTagInfo(id)
	local data = C_QuestLog.GetQuestTagInfo(id)
	return data.tagID, data.tagName, data.worldQuestType, data.quality, data.isElite, data.tradeskillLineID
end

local GetNumQuestLogRewardCurrencies, GetQuestLogRewardCurrencyInfo = GetNumQuestLogRewardCurrencies, GetQuestLogRewardCurrencyInfo
if not GetNumQuestLogRewardCurrencies then
	local reward_cache = {}
	local function GetData(questID)
		local data = reward_cache[questID]
		local t = GetTime()
		if not data or t > data.expTime then
			data = C_QuestLog.GetQuestRewardCurrencies(questID)
			data.expTime = t + 5
		end
		return data
	end
	function GetNumQuestLogRewardCurrencies(questID)
		local data = GetData(questID)
		return #data
	end
	function GetQuestLogRewardCurrencyInfo(i, questID)
		local data = GetData(questID)[i]
		if not data then
			return
		end
		local amount = data.totalRewardAmount
		return data.name, data.texture, amount, data.currencyID
	end
end

local ItemInfoCache = {}
local function GetItemInfoFromLink(itemLink)
	if not itemLink then return 0, 0 end
	if ItemInfoCache[itemLink] then
		return ItemInfoCache[itemLink].itemLevel, ItemInfoCache[itemLink].quality
	end

	local tooltipData = C_TooltipInfo.GetHyperlink(itemLink)
	local foundIlvl = 0
	local foundQuality = 0
	if tooltipData and tooltipData.lines then
		for _, line in ipairs(tooltipData.lines) do
			if line.itemLevel and line.itemLevel > 0 then
				foundIlvl = line.itemLevel
			end
			if line.quality and line.quality > 0 then
				foundQuality = line.quality
			end
			if foundIlvl > 0 and foundQuality > 0 then
				break
			end
		end
	end

	if foundIlvl == 0 then
		local _, _, _, baseIlvl = C_Item.GetItemInfo(itemLink)
		foundIlvl = baseIlvl or 0
	end
	if foundQuality == 0 then
		local _, _, baseQuality = C_Item.GetItemInfo(itemLink)
		foundQuality = baseQuality or 0
	end

	ItemInfoCache[itemLink] = {itemLevel = foundIlvl, quality = foundQuality}
	return foundIlvl, foundQuality
end

local CacheQuestItemReward = {}
local CacheIsAnimaItem = {}

do
	local prevTime = 0
	local prevRes
	function WorldQuestList_GetCallingQuests()
		local nowTime = GetTime()
		if (nowTime - prevTime < 15) and prevRes then
			return unpack(prevRes)
		end
		prevTime = nowTime
		prevRes = {}
		for i=1,C_QuestLog.GetNumQuestLogEntries() do
			local questID = C_QuestLog.GetQuestIDForLogIndex(i)
			if questID ~= 0 and C_QuestLog.IsQuestCalling(questID) then
				prevRes[#prevRes+1] = questID
			end
		end
		return unpack(prevRes)
	end
end

local GENERAL_MAPS = {
	[947] = 2,
	[875] = 1,
	[876] = 1,
	[619] = 4,
	[905] = 3,
	[994] = 3,
	[572] = 4,
	[113] = 4,
	[424] = 4,
	[12] = 4,
	[13] = 4,
	[101] = 4,
	[1550] = 1,
	[1978] = 1,
	[2274] = 1,
}
local LE = {
	LE_QUEST_TAG_TYPE_PET_BATTLE = Enum.QuestTagType.PetBattle,
	LE_QUEST_TAG_TYPE_PVP = Enum.QuestTagType.PvP,
	LE_QUEST_TAG_TYPE_FACTION_ASSAULT = Enum.QuestTagType.FactionAssault,
	LE_QUEST_TAG_TYPE_INVASION = Enum.QuestTagType.Invasion,
	LE_QUEST_TAG_TYPE_DUNGEON = Enum.QuestTagType.Dungeon,
	LE_QUEST_TAG_TYPE_RAID = Enum.QuestTagType.Raid,
	LE_QUEST_TAG_TYPE_PROFESSION = Enum.QuestTagType.Profession,
	ITEM_SPELL_TRIGGER_ONUSE = ITEM_SPELL_TRIGGER_ONUSE,
	LE_PREY_WORLD_QUEST_TAG_ID = 295,
	LE_DRAGON_RACING_TAG_ID = 281,
}

local function CreateMapTextOverlay(mapFrame, pinName)
	local mapCanvas = mapFrame:GetCanvas()
	local textsFrame = CreateFrame("Frame", nil, mapCanvas)
	textsFrame:SetPoint("TOPLEFT")
	textsFrame:SetSize(1, 1)
	textsFrame:SetFrameLevel(10000)

	local textsTable = {}
	textsTable.s = 1
	textsTable.f = textsFrame
	textsTable.c = mapCanvas

	local function UpdateScale()
		local pins = mapFrame.pinPools[pinName]
		if not pins then return end

		local scaleFactor, startScale, endScale
		local hasPin = false
		for obj in mapFrame:EnumeratePinsByTemplate("WorldMap_WorldQuestPinTemplate") do
			scaleFactor = obj.scaleFactor
			startScale = obj.startScale
			endScale = obj.endScale
			hasPin = true
			break
		end
		if not hasPin then return end

		local scale
		if startScale and endScale then
			local parentScaleFactor = 1.0 / mapFrame:GetCanvasScale()
			scale = parentScaleFactor * Lerp(startScale, endScale, Saturate(scaleFactor * mapFrame:GetCanvasZoomPercent()))
		else
			scale = 1
		end

		if scale then
			scale = scale * mapFrame:GetGlobalPinScale()
			for i = 1, #textsTable do
				textsTable[i]:SetScale(scale)
			end
		end
		textsTable.s = scale or 1
	end

	return textsTable, UpdateScale
end

local WorldMapFrame_TextTable, UpdateMapTextScale = CreateMapTextOverlay(WorldMapFrame, "WorldMap_WorldQuestPinTemplate")

local function AddText(table,obj,num,text)
	num = num + 1
	local t = table[num]
	if not t then
		t = CreateFrame("Frame",nil,table.c)
		t:SetSize(1,1)
		t.t = t:CreateFontString(nil,"OVERLAY","GameFontWhite")
		t.t:SetPoint("CENTER")
		t:SetScale(table.s)
		table[num] = t
	end

	t.obj = obj:GetParent()

	t:SetScript("OnUpdate", function(self)
		if not self.obj or not self.obj:IsVisible() then
			self:Hide()
		else
			local lvl = self.obj:GetFrameLevel()
			if self.frLvl ~= lvl then
				self:SetFrameLevel(lvl)
				self.frLvl = lvl
			end
			if not self:IsShown() then
				self:Show()
			end
		end
	end)

	if t.obj then
		local lvl = t.obj:GetFrameLevel()
		if t.frLvl ~= lvl then
			t:SetFrameLevel(lvl)
			t.frLvl = lvl
		end
	end

	t.t:SetFont(STANDARD_TEXT_FONT,12,"THICKOUTLINE")
	t.t:SetTextColor(1,1,1,1)

	t:SetPoint("CENTER",obj,0,0)
	t.t:SetText(text)

	if not t:IsShown() then
		t:Show()
	end	

	return num
end

function WorldQuestList_WQIcons_AddIcons(frame,pinName)
	frame = frame or WorldMapFrame
	local pins = frame.pinPools[pinName or "WorldMap_WorldQuestPinTemplate"]
	if pins then
		local isWorldMapFrame = frame == WorldMapFrame
		local isRibbonDisabled = isWorldMapFrame and GENERAL_MAPS[GetCurrentMapID()]
		local tCount = 0
		local bountyMapID = frame:GetMapID() or 0
		if bountyMapID == 1014 then bountyMapID = 876 
		elseif bountyMapID == 1011 then bountyMapID = 875 end
		local bounties = C_QuestLog.GetBountiesForMapID(bountyMapID) or {}
		for _,bountyData in pairs(bounties) do
			local t = C_TaskQuest.GetQuestTimeLeftMinutes(bountyData.questID) or 0
			if t < 1440 then
				bountyData.lowTime = true
			elseif t < 2880 then
				bountyData.middleTime = true
			end
			if IsQuestComplete(bountyData.questID) or t == 0 then
				bountyData.completed = true
			end
		end
		local mapsToHighlightCallings = {}
		do
			local p = 1
			local questID = WorldQuestList_GetCallingQuests()
			while questID do
				local mapID, worldQuests, worldQuestsElite, dungeons, treasures = C_QuestLog.GetQuestAdditionalHighlights(questID)
				if mapID and mapID ~= 0 then
					local callingData = {questID = questID, mapID = mapID, worldQuests = worldQuests, worldQuestsElite = worldQuestsElite, dungeons = dungeons, treasures = treasures}

					local t = C_TaskQuest.GetQuestTimeLeftMinutes(questID) or 0
					if t < 1440 then
						callingData.lowTime = true
					elseif t < 2880 then
						callingData.middleTime = true
					end
					if IsQuestComplete(questID) or t == 0 then
						callingData.completed = true
					end

					mapsToHighlightCallings[#mapsToHighlightCallings+1] = callingData
				end
				p = p + 1
				questID = select(p,WorldQuestList_GetCallingQuests())
			end
		end
		if isWorldMapFrame then
			if not WorldMapFrame_TextTable.f:IsShown() then
				WorldMapFrame_TextTable.f:Show()
			end
		end
		local warMode = C_PvP.IsWarModeDesired()
		local warModeBonus = C_PvP.GetWarModeRewardBonus() / 100 + 1		
		local selectedQuestID = C_SuperTrack.GetSuperTrackedQuestID()
		
		for obj in frame:EnumeratePinsByTemplate("WorldMap_WorldQuestPinTemplate") do
			local icon = obj.WQL_rewardIcon
			if obj.questID then
				if not icon then
					icon = obj:CreateTexture(nil,"OVERLAY")
					obj.WQL_rewardIcon = icon
					icon:SetPoint("CENTER",0,0)
					icon:SetSize(RewardIcons_Size,RewardIcons_Size)
					local Border = obj:CreateTexture(nil, 'OVERLAY', nil, 1)
					Border:SetPoint('CENTER', 0, 0)
					Border:SetAtlas('Artifacts-PerkRing-MainProc')
					Border:SetSize(RewardIcons_Size + 4, RewardIcons_Size +4)
					obj.Border = Border
					
					local eliteBorder = obj:CreateTexture(nil, "OVERLAY", nil, 2)
					obj.WQL_EliteBorder = eliteBorder
					eliteBorder:SetPoint("CENTER", icon, "CENTER", 1, 0)
					eliteBorder:SetSize(RewardIcons_Size * 1.2, RewardIcons_Size * 1.2)
					eliteBorder:SetAtlas("worldquest-questmarker-dragon")
					eliteBorder:Hide()

					obj:HookScript("OnEnter", function(self)
						if self.Border then self.Border:SetVertexColor(0, 1, 0) end
						if self.WQL_EliteBorder then self.WQL_EliteBorder:SetVertexColor(0, 1, 0) end
					end)

					obj:HookScript("OnLeave", function(self)
						local currentSelected = C_SuperTrack.GetSuperTrackedQuestID()
						if currentSelected and self.questID == currentSelected then
							if self.Border then self.Border:SetVertexColor(0, 1, 0) end
							if self.WQL_EliteBorder then self.WQL_EliteBorder:SetVertexColor(0, 1, 0) end
						else
							if self.Border then self.Border:SetVertexColor(1, 1, 1) end
							if self.WQL_EliteBorder then self.WQL_EliteBorder:SetVertexColor(1, 1, 1) end
						end
					end)
							
					local iconWMask = obj:CreateTexture(nil,"OVERLAY")
					obj.WQL_rewardIconWMask = iconWMask
					iconWMask:SetPoint("CENTER",0,0)
					iconWMask:SetSize(RewardIcons_Size,RewardIcons_Size)
					iconWMask:SetMask("Interface\\Minimap\\UI-Minimap-Background")
					
					local ribbon = obj:CreateTexture(nil,"BACKGROUND")
					obj.WQL_rewardRibbon = ribbon
					ribbon:SetPoint("TOP",obj,"BOTTOM",0,16)
					ribbon:SetSize(RewardIcons_Size*2,RewardIcons_Size)
					
					if not isWorldMapFrame then
						local ribbonText = obj:CreateFontString(nil,"BORDER","GameFontWhite")
						obj.WQL_rewardRibbonText = ribbonText
						local a1,a2 = ribbonText:GetFont()
						ribbonText:SetFont(a1,12)
						ribbonText:SetPoint("CENTER",ribbon,0,0)
						ribbonText:SetTextColor(0,0,0,1)
					end
					
					local iconTopRight = obj:CreateTexture(nil, "OVERLAY", nil, 7)
					obj.WQL_iconTopRight = iconTopRight
					iconTopRight:SetPoint("CENTER",obj,"TOPRIGHT",0,0)
					iconTopRight:SetSize(RewardIcons_Size*0.7,RewardIcons_Size*0.7)
					iconTopRight.SIZE_MOD = 0.7

					local warbandIcon = obj:CreateTexture(nil, "OVERLAY", nil, 7)
					obj.WQL_WarbandIcon = warbandIcon
					warbandIcon:SetPoint("BOTTOM", obj, "TOP", 0, 2)
					warbandIcon:SetSize(RewardIcons_Size*0.5,RewardIcons_Size*0.5)
					warbandIcon:SetTexture("interface\\warbands\\uiwarbandsicons")
					warbandIcon:SetTexCoord(0, 0.4, 0, 0.4) 
					warbandIcon:Hide()
					
					obj.WQL_BountyRing_defSize = obj.BountyRing and obj.BountyRing:GetSize()
				end

				local tagID, tagName, worldQuestType, rarity, isElite, tradeskillLineIndex = GetQuestTagInfo(obj.questID)

				if isElite then
					obj.WQL_EliteBorder:Show()
				else
					obj.WQL_EliteBorder:Hide()
				end

				local factionID = select(2, C_TaskQuest.GetQuestInfoByQuestID(obj.questID))
				local hasWarbandReward = factionID ~= nil
				if obj.WQL_WarbandIcon then
					if hasWarbandReward then
						obj.WQL_WarbandIcon:Show()
					else
						obj.WQL_WarbandIcon:Hide()
					end
				end
				
				if selectedQuestID and obj.questID == selectedQuestID then
					if obj.Border then obj.Border:SetVertexColor(0, 1, 0) end
					if obj.WQL_EliteBorder then obj.WQL_EliteBorder:SetVertexColor(0, 1, 0) end
				else
					if obj.Border then obj.Border:SetVertexColor(1, 1, 1) end
					if obj.WQL_EliteBorder then obj.WQL_EliteBorder:SetVertexColor(1, 1, 1) end
				end
				
				local iconAtlas,iconTexture,iconVirtual = nil
				local ajustSize,ajustMask = 0
				local amount,amountIcon,amountColor = 0

				local money = GetQuestLogRewardMoney(obj.questID)
				local goldAmount = 0
				local hasGoldReward = false
				local goldOnly = false
				
				if money > 0 then
					goldAmount = floor(money / 10000 * (warMode and C_QuestLog.QuestCanHaveWarModeBonus(obj.questID) and warModeBonus or 1))
					local numItems = GetNumQuestLogRewards(obj.questID)
					local numCurrencies = GetNumQuestLogRewardCurrencies(obj.questID)
					if goldAmount > 500 then
						hasGoldReward = true
					elseif numItems == 0 and numCurrencies == 0 then
						hasGoldReward = true
						goldOnly = true
					end
					if hasGoldReward then
						iconAtlas = "Auctioneer"
						amount = goldAmount
					end
				end

				local currencies = {}
				local items = {}
				if not goldOnly then
					for i = 1, GetNumQuestLogRewardCurrencies(obj.questID) do
						local name, texture, numItems, currencyID = GetQuestLogRewardCurrencyInfo(i, obj.questID)
						if numItems and numItems > 0 then
							local warModeMultiplier = (warMode and C_QuestLog.QuestCanHaveWarModeBonus(obj.questID) and C_CurrencyInfo.DoesWarModeBonusApply(currencyID) and warModeBonus or 1)
							local finalAmount = floor(numItems * warModeMultiplier)
							currencies[#currencies+1] = {
								texture = texture,
								amount = finalAmount,
							}
						end
					end

					for i = 1, GetNumQuestLogRewards(obj.questID) do
						local name, icon, numItems, quality, _, itemID = GetQuestLogRewardInfo(i, obj.questID)
						if itemID then
							local itemLink = CacheQuestItemReward[obj.questID]
							if not itemLink then
								local tooltipData = C_TooltipInfo.GetQuestLogItem("reward", i, obj.questID)
								if tooltipData then
									itemLink = tooltipData.hyperlink
								end
								CacheQuestItemReward[obj.questID] = itemLink
							end
							local realItemLevel, realItemQuality = GetItemInfoFromLink(itemLink)
							if realItemLevel == 0 then
								realItemLevel = select(4, C_Item.GetItemInfo(itemID)) or 0
							end
							if realItemQuality == 0 then
								realItemQuality = quality
							end

							local isEquippable = C_Item.IsEquippableItem(itemID)
							
							local itemData = {
								itemID = itemID,
								icon = icon,
								numItems = numItems or 1,
								quality = realItemQuality,
								itemLevel = realItemLevel,
								iconTexture = icon,
								ajustMask = true,
								ajustSize = 4,
								amount = realItemLevel,
								equippable = isEquippable,
							}

							if isEquippable then
								itemData.amount = realItemLevel
							else
								itemData.amount = itemData.numItems
							end

							if itemID == 245653 then
								itemData.iconTexture = 133016
								itemData.amount = numItems
							elseif itemID == 124124 or itemID == 151568 then
								itemData.amount = numItems
							elseif itemID == 152668 and numItems and numItems > 1 then
								itemData.amount = numItems
							elseif itemID == 198048 or itemID == 198056 or itemID == 198058 or itemID == 198059 or itemID == 204673 then
								itemData.amount = (itemID == 198048 and "I" or itemID == 198056 and "II" or itemID == 198058 and "III" or itemID == 204673 and "V" or "IV")
							elseif itemID == 228339 or itemID == 228338 then
								itemData.amount = (itemID == 228338 and "I" or itemID == 228339 and "II" or "III")
							end

							if CacheIsAnimaItem[itemID] then
								itemData.iconTexture = 613397
								itemData.ajustMask = true
								itemData.ajustSize = 10
								local animaAmount = numItems * CacheIsAnimaItem[itemID]
								if warMode and C_QuestLog.QuestCanHaveWarModeBonus(obj.questID) then
									local bonus = floor(animaAmount * (warModeBonus - 1) + .5)
									bonus = bonus - bonus % 3
									animaAmount = animaAmount + bonus
								end
								itemData.amount = animaAmount
							elseif select(2, C_Item.GetItemInfoInstant(itemID)) == MISCELLANEOUS then
								local tooltipData = C_TooltipInfo.GetQuestLogItem("reward", i, obj.questID)
								if tooltipData then
									local isAnima
									for j=2, #tooltipData.lines do
										local tooltipLine = tooltipData.lines[j]
										local text = tooltipLine.leftText
										if text and text:find(WORLD_QUEST_REWARD_FILTERS_ANIMA.."|r$") then
											isAnima = 1
										elseif text and isAnima and text:find("^"..LE.ITEM_SPELL_TRIGGER_ONUSE) then
											local num = text:gsub("(%d+)[ %.,]+(%d+)","%1%2"):match("%d+")
											isAnima = tonumber(num or "") or 1
											break
										end 
									end
									if isAnima then
										if isAnima ~= 1 then
											CacheIsAnimaItem[itemID] = isAnima
										end
										itemData.iconTexture = 613397
										itemData.ajustMask = true
										itemData.ajustSize = 10
										local animaAmount = numItems * isAnima
										if warMode and C_QuestLog.QuestCanHaveWarModeBonus(obj.questID) then
											local bonus = floor(animaAmount * (warModeBonus - 1) + .5)
											bonus = bonus - bonus % 3
											animaAmount = animaAmount + bonus
										end
										itemData.amount = animaAmount
									end
								end
							end

							items[#items+1] = itemData
						end
					end
				end

				local selected = nil
				if hasGoldReward then
					selected = { type = "gold", iconAtlas = iconAtlas, amount = amount }
				elseif #items > 0 then
					table.sort(items, function(a, b)
						if a.itemLevel ~= b.itemLevel then return a.itemLevel > b.itemLevel end
						return (a.numItems or 0) > (b.numItems or 0)
					end)
					selected = items[1]
					selected.type = "item"
				elseif #currencies > 0 then
					table.sort(currencies, function(a, b) return a.amount > b.amount end)
					selected = currencies[1]
					selected.type = "currency"
				end

				if not selected then
					selected = { type = "default", iconAtlas = "worldquest-icon" }
				end

				if selected and selected.type == "gold" then
					iconAtlas = selected.iconAtlas
					amount = selected.amount
					iconTexture = nil
					iconVirtual = nil
					ajustSize = 0
					ajustMask = false
				elseif selected and selected.type == "item" then
					iconAtlas = selected.iconAtlas
					iconTexture = selected.iconTexture
					ajustMask = selected.ajustMask
					ajustSize = selected.ajustSize
					amount = selected.amount
					amountIcon = nil
					amountColor = nil
					if selected.equippable and selected.quality and selected.quality > 1 then
						local r, g, b = C_Item.GetItemQualityColor(selected.quality)
						if r and g and b then
							amountColor = "|cff"..string.format("%02x%02x%02x", r*255, g*255, b*255)
						end
					end
				elseif selected and selected.type == "currency" then
					iconAtlas = nil
					iconTexture = selected.texture
					ajustMask = true
					ajustSize = 0
					amount = selected.amount
					amountIcon = nil
				elseif selected and selected.type == "default" then
					iconAtlas = selected.iconAtlas
					iconTexture = nil
					ajustMask = true
					ajustSize = 0
					amount = nil
					amountIcon = nil
					amountColor = nil
				end

				if worldQuestType ~= LE.LE_QUEST_TAG_TYPE_PVP then
					obj.WQL_iconTopRight:SetTexCoord(0, 1, 0, 1)
				end

				if worldQuestType == LE.LE_QUEST_TAG_TYPE_PVP then
					if obj.WQL_iconTopRight.curr ~= "pvp" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.4, RewardIcons_Size*0.4)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 12)
						obj.WQL_iconTopRight:SetTexture("interface\\pvpframe\\pvpcurrency-conquest-neutral")
						obj.WQL_iconTopRight:SetTexCoord(0.2, 0.8, 0.2, 0.8)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight.curr = "pvp"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_FACTION_ASSAULT then
					local icon = UnitFactionGroup("player") == "Alliance" and "worldquest-icon-alliance" or "worldquest-icon-horde"
					if obj.WQL_iconTopRight.curr ~= icon then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.2,RewardIcons_Size*0.2)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas(icon)
						obj.WQL_iconTopRight.curr = icon
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_PET_BATTLE then
					if obj.WQL_iconTopRight.curr ~= "petbattle" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.3, RewardIcons_Size*0.3)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetTexture("Interface\\Icons\\tracking_wildpet")
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight.curr = "petbattle"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_INVASION then
					if obj.WQL_iconTopRight.curr ~= "invasion" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.38, RewardIcons_Size*0.38)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas("worldquest-icon-burninglegion")
						obj.WQL_iconTopRight.curr = "invasion"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_DUNGEON then
					if obj.WQL_iconTopRight.curr ~= "dungeon" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas("Dungeon")
						obj.WQL_iconTopRight.curr = "dungeon"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_RAID then
					if obj.WQL_iconTopRight.curr ~= "raid" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas("Raid")
						obj.WQL_iconTopRight.curr = "raid"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif worldQuestType == LE.LE_QUEST_TAG_TYPE_PROFESSION then
					if obj.WQL_iconTopRight.curr ~= "profession" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.3, RewardIcons_Size*0.3)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas("worldquest-icon-engineering")
						obj.WQL_iconTopRight.curr = "profession"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif tagID == LE.LE_PREY_WORLD_QUEST_TAG_ID then
					if obj.WQL_iconTopRight.curr ~= "prey" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 15, 10)
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight:SetAtlas("UI-EventPoi-PreyCrystal")
						obj.WQL_iconTopRight.curr = "prey"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				elseif tagID == LE.LE_DRAGON_RACING_TAG_ID then
					if obj.WQL_iconTopRight.curr ~= "dragonracing" then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size*0.4, RewardIcons_Size*0.4)
						obj.WQL_iconTopRight:SetPoint("TOP", obj, "TOP", 14, 12)
						obj.WQL_iconTopRight:SetAtlas("worldquest-icon-race")
						obj.WQL_iconTopRight:SetDrawLayer("OVERLAY", 7)
						obj.WQL_iconTopRight.curr = "dragonracing"
					end
					obj.WQL_iconTopRight:Show()
					if not obj.WQL_goldRing then
						obj.WQL_goldRing = obj:CreateTexture(nil, "OVERLAY", nil, 7)
						obj.WQL_goldRing:SetAtlas("worldquest-tracker-ring-selected")
					end
					obj.WQL_goldRing:SetSize(RewardIcons_Size*0.5, RewardIcons_Size*0.5)
					obj.WQL_goldRing:SetPoint("CENTER", obj.WQL_iconTopRight, "CENTER", 0, 0)
					obj.WQL_goldRing:Show()
				else
					if obj.WQL_iconTopRight.curr then
						obj.WQL_iconTopRight:SetSize(RewardIcons_Size, RewardIcons_Size)
						obj.WQL_iconTopRight:SetTexture()
						obj.WQL_iconTopRight.curr = nil
					end
					obj.WQL_iconTopRight:Hide()
					if obj.WQL_goldRing then
						obj.WQL_goldRing:Hide()
					end
				end

				if iconTexture or iconAtlas then
					local res_size = (RewardIcons_Size+ajustSize) * 0.5
					icon:SetSize(res_size, res_size)
					obj.WQL_rewardIconWMask:SetSize(res_size, res_size)
					
					if iconTexture then
						if ajustMask then
							obj.WQL_rewardIconWMask:SetTexture(iconTexture)
							obj.WQL_rewardIconWMask.curr = iconTexture
							icon:SetTexture()
							icon.curr = nil
						else
							icon:SetTexture(iconTexture)
							icon.curr = iconTexture
							icon:SetDesaturated(false)
						end
					else
						icon:SetAtlas(iconAtlas)
						icon.curr = iconAtlas
						icon:SetDesaturated(false)
					end
					
					icon:Show()
					obj.WQL_rewardIconWMask:Show()
					
					if obj.Display and obj.Display.Icon then
						local parent = obj.Display.Icon:GetParent()
						if parent then
							icon:SetParent(parent)
							obj.WQL_rewardIconWMask:SetParent(parent)
							icon:SetDrawLayer("OVERLAY", 7)
							obj.WQL_rewardIconWMask:SetDrawLayer("OVERLAY", 7)
						end
					end
				else
					icon:SetTexture()
					obj.WQL_rewardIconWMask:SetTexture()
				end
				icon:Show()
				obj.WQL_rewardIconWMask:Show()

				local showRibbon = false
				local ribbonText = ""
				if iconVirtual then
					showRibbon = true
					ribbonText = (amountIcon and "|T"..amountIcon..":0|t" or "")..(amountColor or "")..amount
				elseif selected and selected.type == "item" and ((type(amount)=="number" and amount > 0) or type(amount) == "string") then
					showRibbon = true
					ribbonText = (amountIcon and "|T"..amountIcon..":0|t" or "")..(amountColor or "")..amount
				elseif selected and selected.type == "currency" and amount > 0 then
					showRibbon = true
					ribbonText = (amountIcon and "|T"..amountIcon..":0|t" or "")..(amountColor or "")..amount
				elseif selected and selected.type == "gold" and amount > 0 then
					showRibbon = true
					ribbonText = amount
				end

				if showRibbon and not isRibbonDisabled then
					if not obj.WQL_rewardRibbon:IsShown() then obj.WQL_rewardRibbon:Show() end
					if not isWorldMapFrame then
						obj.WQL_rewardRibbonText:SetFont(STANDARD_TEXT_FONT,12,"THICKOUTLINE")
						obj.WQL_rewardRibbonText:SetTextColor(1,1,1,1)
					end
					obj.WQL_rewardRibbon:SetAlpha(1)
					if not isWorldMapFrame then
						obj.WQL_rewardRibbonText:SetText(ribbonText)
					end
					obj.WQL_rewardRibbon:SetWidth( ((#tostring(amount) + (amountIcon and 1.5 or 0)) * RewardIcons_Size * 2) * 0.6 )

					obj.WQL_rewardRibbon:ClearAllPoints()
					obj.WQL_rewardRibbon:SetPoint("TOP", obj, "BOTTOM", 0, 14)

					obj.TimeLowFrame:SetPoint("LEFT", -16, 15)
					if isWorldMapFrame then
						tCount = AddText(WorldMapFrame_TextTable,obj.WQL_rewardRibbon,tCount,ribbonText)							
					end
				else
					if obj.WQL_rewardRibbon:IsShown() then
						obj.WQL_rewardRibbon:Hide()
						if not isWorldMapFrame then
							obj.WQL_rewardRibbonText:SetText("")
						end
						obj.TimeLowFrame:SetPoint("LEFT", -16, 15)				
					end
				end
				if selected and selected.type == "default" then
					local bg = obj:CreateTexture(nil, "BACKGROUND")
					bg:SetTexture("Interface\\Common\\WhiteIcon")
					bg:SetSize(RewardIcons_Size, RewardIcons_Size)
					bg:SetPoint("CENTER", 0, 0)
					bg:Show()
					icon:SetAtlas("completiondialog-warwithincampaign-worldquests-icon")
					icon:SetSize(RewardIcons_Size, RewardIcons_Size)
					icon:SetPoint("CENTER", 0, 0)
					icon:SetDrawLayer("OVERLAY", 1)
					icon:Show()
					obj.WQL_rewardIconWMask:Hide()
				end
				obj.WQL_questID = obj.questID

				if obj.BountyRing then
					obj.BountyRing:SetVertexColor(1,1,1)
					obj.BountyRing:SetSize(obj.WQL_BountyRing_defSize,obj.WQL_BountyRing_defSize)
					obj.BountyRing.WQL_color = 4
					if not RewardIcons_DisableBountyColors then
						obj.BountyRing:Hide()
						for _,bountyData in pairs(bounties) do
							if IsQuestCriteriaForBounty(obj.questID, bountyData.questID) and not bountyData.completed then
								obj.BountyRing:SetSize(RewardIcons_Size*2.5,RewardIcons_Size*2.5)
								obj.BountyRing:Show()
								if bountyData.lowTime and obj.BountyRing.WQL_color > 1 then
									obj.BountyRing:SetVertexColor(1,0,0)
									obj.BountyRing.WQL_color = 1
								elseif bountyData.middleTime and obj.BountyRing.WQL_color > 2 then
									obj.BountyRing:SetVertexColor(1,.5,0)
									obj.BountyRing.WQL_color = 2
								elseif not bountyData.lowTime and not bountyData.middleTime and obj.BountyRing.WQL_color > 3 then
									obj.BountyRing:SetVertexColor(.3,1,.3)
									obj.BountyRing.WQL_color = 3
								end
							end
						end
					end
				end
			else
				if obj.WQL_rewardIcon then
					if obj.WQL_rewardIconWMask.curr then
						obj.WQL_rewardIconWMask:SetTexture()
						obj.WQL_rewardIconWMask.curr = nil
					end
					if obj.WQL_rewardIcon.curr then
						obj.WQL_rewardIcon:SetTexture()
						obj.WQL_rewardIcon.curr = nil
					end
					if obj.WQL_iconTopRight.curr then
						obj.WQL_iconTopRight:SetTexture()
						obj.WQL_iconTopRight.curr = nil
					end
					obj.WQL_rewardRibbon:Hide()
					if not isWorldMapFrame then
						obj.WQL_rewardRibbonText:SetText("")
					end
					obj.TimeLowFrame:SetPoint("LEFT", -16, 15)
					if obj.BountyRing then
						obj.BountyRing:SetSize(obj.WQL_BountyRing_defSize,obj.WQL_BountyRing_defSize)
						obj.BountyRing:SetVertexColor(1,1,1)
					end
				end
				obj.WQL_questID = nil
				if obj.SelectionIndicator then
					obj.SelectionIndicator:Hide()
				end
				if obj.SelectionIndicator then
					obj.SelectionIndicator:Hide()
				end
			end
		end
		if isWorldMapFrame then
			for i=tCount+1,#WorldMapFrame_TextTable do
				WorldMapFrame_TextTable[i]:Hide()
			end
			if WorldMapFrame_TextTable then
				WorldMapFrame_TextTable.prevScale = nil
				WorldMapFrame_TextTable.prevZoom = nil
			end
		end

	elseif frame == WorldMapFrame then
		for i=1,#WorldMapFrame_TextTable do
			WorldMapFrame_TextTable[i]:Hide()
		end
	end
end

local function CleanupWorldQuestPins(frame)
	frame = frame or WorldMapFrame
	if not frame or not frame.EnumeratePinsByTemplate then
		return
	end
	
	for obj in frame:EnumeratePinsByTemplate("WorldMap_WorldQuestPinTemplate") do
		if obj.WQL_rewardIcon then
			obj.WQL_rewardIcon:SetTexture(nil)
			obj.WQL_rewardIconWMask:SetTexture(nil)				
			if obj.WQL_iconTopRight then
				obj.WQL_iconTopRight:SetTexture(nil)
				obj.WQL_iconTopRight.curr = nil
			end		
			if obj.WQL_WarbandIcon then
				obj.WQL_WarbandIcon:Hide()
			end
			obj.WQL_rewardRibbon:Hide()
			if obj.WQL_rewardRibbonText then
				obj.WQL_rewardRibbonText:SetText("")
			end
			obj.WQL_EliteBorder:Hide()
			if obj.WQL_goldRing then
				obj.WQL_goldRing:Hide()
			end
			obj.WQL_questID = nil
		end
	end
end

local function SetupSelectionTracking()
	local registered = false
	WorldMapFrame:HookScript("OnShow", function()
		CleanupWorldQuestPins()
		if not registered then
			WorldMapFrame:RegisterEvent("QUEST_LOG_UPDATE")
			WorldMapFrame:RegisterEvent("SUPER_TRACKING_CHANGED")
			registered = true
		end
	end)
	
	WorldMapFrame:HookScript("OnHide", function()
		CleanupWorldQuestPins()
		if registered then
			WorldMapFrame:UnregisterEvent("QUEST_LOG_UPDATE")
			WorldMapFrame:UnregisterEvent("SUPER_TRACKING_CHANGED")
			registered = false
		end
		
		table.wipe(CacheQuestItemReward)
		table.wipe(CacheIsAnimaItem)
		if WorldMapFrame_TextTable and WorldMapFrame_TextTable.f then
			for i = 1, #WorldMapFrame_TextTable do
				local t = WorldMapFrame_TextTable[i]
				if t then
					t:Hide()
					t.obj = nil
				end
			end
			WorldMapFrame_TextTable.f:Hide()
			WorldMapFrame_TextTable.prevScale = nil
			WorldMapFrame_TextTable.prevZoom = nil
		end
	end)

	if WorldMapFrame.ScrollContainer then
		local wheelHandler = function()
			C_Timer.After(0, function()
				CleanupWorldQuestPins()
				WorldQuestList_WQIcons_AddIcons()
				if UpdateMapTextScale then
					UpdateMapTextScale()
				end
			end)
		end
		WorldMapFrame.ScrollContainer:HookScript("OnMouseWheel", wheelHandler)
	end

	hooksecurefunc(WorldMapFrame, "SetMapID", function()
		C_Timer.After(0, function()
			CleanupWorldQuestPins()
			WorldQuestList_WQIcons_AddIcons()
			WorldMapFrame:OnCanvasScaleChanged()
			if UpdateMapTextScale then UpdateMapTextScale() end
		end)
	end)
	
	WorldMapFrame:HookScript("OnEvent", function(self, event, ...)
		if event == "QUEST_LOG_UPDATE" or event == "SUPER_TRACKING_CHANGED" then
			C_Timer.After(0, function()
				CleanupWorldQuestPins()
				WorldQuestList_WQIcons_AddIcons()
				if UpdateMapTextScale then UpdateMapTextScale() end
			end)
		end
	end)
end

SetupSelectionTracking()