Quantcast
-- Scanning functions
local GetMountInfo, GetMountTypeSpeed
do
	local L = {
		fast = 'This is a very fast %w+',
		fastest = 'This is an extremely fast %w+',
		flying = 'Outland %w+ Northrend',
		turtle = '[Tt]urtle',
		aq = 'Emits a high frequency sound, forcing a silithid tank',
	}
	local speeds = {
		flying_fastest = 310, flying_fast = 280, flying = 150,
		ground_fast = 100, ground = 60, ground_slow = 0
	}

	local cache = {}
	local scantt = CreateFrame('GameTooltip', 'CompanionCountTooltip', UIParent, 'GameTooltipTemplate')

	function GetMountType(spellid)
		local rval
		if not cache[spellid] then
			local link = GetSpellLink(spellid)

			scantt:SetOwner(UIParent, 'ANCHOR_NONE')
			scantt:SetHyperlink(link)

			if CompanionCountTooltipTextLeft3 then
				local text = CompanionCountTooltipTextLeft3:GetText()

				scantt:Hide()

				if text:match(L.flying) then
					if text:match(L.fastest) then
						rval = 'flying_fastest'
					elseif text:match(L.fast) then
						rval = 'flying_fast'
					else
						rval = 'flying'
					end
				else
					if text:match(L.fast) then
						rval = 'ground_fast'
					elseif text:match(L.turtle) then
							rval = 'ground_slow'
					elseif text:match(L.aq) then
						rval = 'ground'
					else
						rval = 'ground'
					end
				end
			end
			cache[spellid] = rval
		else
			rval = cache[spellid]
		end

		return rval
	end

	function GetMountTypeSpeed(typ)
		return speeds[typ] or 0
	end
end


-- Text update
do
	local TOTAL_FORMAT = ' ('..NUMBER_OF_RESULTS_TEMPLATE:match('%( (.-) %)')..')'

	hooksecurefunc("PetPaperDollFrame_SetCompanionPage", function(num)
		local text = CompanionPageNumber:GetText()
		local count = GetNumCompanions(PetPaperDollFrameCompanionFrame.mode)

		CompanionPageNumber:SetFormattedText(text..TOTAL_FORMAT, count)
	end)

	local point1, parent, point2, x, y = CompanionPrevPageButton:GetPoint(1)
	CompanionPrevPageButton:ClearAllPoints()
	CompanionPrevPageButton:SetPoint(point1, parent, point2, x-20, y)

	point1, parent, point2, x, y = CompanionNextPageButton:GetPoint(1)
	CompanionNextPageButton:ClearAllPoints()
	CompanionNextPageButton:SetPoint(point1, parent, point2, x+33, y)
end


-- Tooltip
do
	local mounts = {}
	local function DoMountTooltip(self, tt)
		local show = function(msg, typ)
			if mounts[typ] then
				tt:AddDoubleLine(msg:format(GetMountTypeSpeed(typ)), mounts[typ], 1, 1, 1)
			end
		end

		table.wipe(mounts)
		for i=1, GetNumCompanions'MOUNT' do
			local id, name, spellid = GetCompanionInfo('MOUNT', i)
			local typ = GetMountType(spellid)

			mounts[typ] = (mounts[typ] or 0) + 1
		end

		tt:SetOwner(self, 'ANCHOR_NONE')
		tt:SetPoint('BOTTOMLEFT', self, 'TOPRIGHT')
		tt:ClearLines()
		tt:AddLine'Detailed Info'

		show('Flying (%d%%)', 'flying_fastest')
		show('Flying (%d%%)', 'flying_fast')
		show('Flying (%d%%)', 'flying')
		show('Ground (%d%%)', 'ground_fast')
		show('Ground (%d%%)', 'ground')
		show('Ground (%d%%)', 'ground_slow')

		tt:Show()
	end

	local function OnEnter(self)

		if PetPaperDollFrameCompanionFrame.mode == 'MOUNT' then
			DoMountTooltip(self, GameTooltip)
		end
	end

	local function OnLeave(self)
		GameTooltip:Hide()
	end

	local ttparent = CreateFrame('Frame', nil, CompanionPageNumber:GetParent())
	ttparent:SetWidth(CompanionPageNumber:GetWidth())
	ttparent:SetHeight(CompanionPageNumber:GetHeight())
	ttparent:SetAllPoints(CompanionPageNumber)
	ttparent:EnableMouse(true)
	ttparent:SetScript('OnEnter', OnEnter)
	ttparent:SetScript('OnLeave', OnLeave)
end