Quantcast
--[[
	Copyright (c) 2014 Eyal Shilony <Lynxium>

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

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