Quantcast
local _, addon = ...

local function getRepInfo(standingID, repMin, repMax, repValue)
	repMax = repMax - repMin
	repValue = repValue - repMin
	repMin = 0

	local gender = UnitSex("player")
	local standing = GetText("FACTION_STANDING_LABEL"..standingID, gender)

	local percent = ceil((repValue / repMax) * 100)
	local toGo, toGoPercent = 0, 0

	if percent ~= 100 then
		toGo = repMax - repValue
		toGoPercent = (repMax - repValue) / repMax * 100
	end

	return standing, repValue, repMax, percent, toGo, toGoPercent
end

function addon:GetFactionInfo(index)
	if not index then
		return
	end
	local name, _, standingID, repMin, repMax, repValue, _, _, _, _, _, _, _, factionID = GetFactionInfo(index)
	if name then

		local friendID, friendRep, _, _, _, _, _, friendThreshold, nextFriendThreshold = GetFriendshipReputation(factionID)
		if friendID ~= nil then
			if nextFriendThreshold then
				repMin, repMax, repValue = friendThreshold, nextFriendThreshold, friendRep
			else
				barMin, barMax, barValue = 0, 1, 1
			end
		end

		return name, getRepInfo(standingID, repMin, repMax, repValue)
	end
end

function addon:GetWatchedFactionInfo()
	local name, standingID, repMin, repMax, repValue = GetWatchedFactionInfo()
	if name then
		return name, getRepInfo(standingID, repMin, repMax, repValue)
	end
end

function addon:Paste(index)
	if not index then
		return
	end

	local name, standing, repValue, repMax, percent, toGo, toGoPercent = self:GetFactionInfo(index)

	if not name then
		return
	end

	local toGoText = ""
	if percent ~= 100 then
		toGoText = ", %d (%d%%) to go"
	end

	ChatFrame_OpenChat(format("%s (%s): %d / %d (%d%%)" .. toGoText .. ".", name, standing, repValue, repMax, percent, toGo, toGoPercent))
end