-- I used to use a mod called PriceEach, it got abandoned and unmaintained.
-- I took it, ripped out all the options, saved variables, and had it just do what the name said.
-- Recently, I found PricePer and adjusted3 things in GPriceEach to do things smarter based on it.
-- However, it is still pretty much PriceEach-lite.

GPriceEach = AceLibrary("AceAddon-2.0"):new("AceHook-2.1")

function GPriceEach:OnEnable()
  self:SecureHook("AuctionFrameBrowse_Update")
end

function GPriceEach:AuctionFrameBrowse_Update()
	local offset = FauxScrollFrame_GetOffset(BrowseScrollFrame)
	for i=1, NUM_BROWSE_TO_DISPLAY do
		local name, _, count, _, _, _, _, _, buyoutPrice, _, _, _ =  GetAuctionItemInfo("list", offset + i)
		if name then
			local _, _, aiItemID = strfind(GetAuctionItemLink("list", offset + i) or "", "item:(%d+):")
			local _, _, _, _, _, _, _, iCount = GetItemInfo(aiItemID)
			if (buyoutPrice > 0) and (iCount > 1) then
				local itemName = getglobal("BrowseButton"..i.."Name")
				local pereach = floor(buyoutPrice / count)
				local peText = " ("..self:FormatPrice(floor(buyoutPrice / count))..")"
				itemName:SetText(itemName:GetText()..peText)
			end
		end
	end
end

-- The following is a very very very very trimmed down and modified function of Abacus since I couldn't make Abacus do this the way I wanted it to look.
function GPriceEach:FormatPrice(value)
	local COLOR_COPPER = "eda55f"
	local COLOR_SILVER = "c7c7cf"
	local COLOR_GOLD = "ffd700"
	local gold = floor(math.abs(value) / 10000)
	local silver = mod(floor(math.abs(value) / 100), 100)
	local copper = mod(floor(math.abs(value)), 100)

		if gold ~= 0 then
			if copper ~= 0 then return format("|cff%s%d|r.|cff%s%02d|r.|cff%s%02d|r", COLOR_GOLD, gold, COLOR_SILVER, silver, COLOR_COPPER, copper) end
			return format("|cff%s%d|r.|cff%s%02d|r", COLOR_GOLD, gold, COLOR_SILVER, silver)
		elseif silver ~= 0 then
			return format("|cff%s%d|r.|cff%s%02d|r", COLOR_SILVER, silver, COLOR_COPPER, copper)
		else
			return format("|cff%s%d|r", COLOR_COPPER, copper)
		end
end
