local AddName, Ns = ...
local L = Ns.L

------------------------------
--		Options Equates		--
------------------------------
local Current, Level, Spark = 1, 1, 1
local Earned, Honor, Splinter = 2, 2, 2
local Both = 3

--------------------------
--		Currency		--
--------------------------

-- Money
function Ns.FunctionList.Gold(HUD, data, options, ...)

	local money = GetMoney()
	local gold = floor(money / 1e4)
	local formattedMoney = (C_CurrencyInfo.GetCoinTextureString(money))

	if options.Display_Gold then
		formattedMoney = Ns.DecimalNumber(options.Decimals_Default, gold)
	end

	HUD:UpdateText(data, formattedMoney)
end
------------------------------------------------

-- WoW Token
local tokenNextUpdate = 0
local TOKEN_UPDATE_INTERVAL = 300 -- 5 minutes
function Ns.FunctionList.WowToken(HUD, data, options, ...)

	local now = GetTime()
	if now >= tokenNextUpdate then
		C_WowTokenPublic.UpdateMarketPrice()
		tokenNextUpdate = now + TOKEN_UPDATE_INTERVAL
	end
	local token = C_WowTokenPublic.GetCurrentMarketPrice() or 0
	local display, color = 0, ""

	token = floor(token / 10000)

	display = Ns.DecimalNumber(options.Decimals_Default, token)

	HUD:UpdateText(data, color .. display)
end
------------------------------------------------

-- Honor
function Ns.FunctionList.Honor(HUD, data, options, ...)

	local EB = options.Level_HonorPoints
	local honorLevel = UnitHonorLevel("player")
	local honorPoints = C_CurrencyInfo.GetCurrencyInfo(1792)--UnitHonor("player")
	local ratedHonor = C_CurrencyInfo.GetCurrencyInfo(1891)
	local ratedTotal, hpDisplay = 0, 0
	local returnText

	if honorPoints.name and tonumber(honorPoints.quantity) then hpDisplay = honorPoints.quantity else hpDisplay = 0 end
	if ratedHonor.name and tonumber(ratedHonor.quantity) then ratedTotal = ratedHonor.quantity end

	if options.Display_Rated then hpDisplay = ratedTotal end

	if Ns.Band(EB, Level) then returnText = honorLevel end
	if Ns.Band(EB, Honor) then returnText = hpDisplay end
	if Ns.Band(EB, Both) then returnText = honorLevel .. "/" .. hpDisplay end
	if returnText == nil then returnText = 0 end

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Crests
local function CrestCurrency(currencyId)
	return function(HUD, data, options, ...)
		local EB = options.Current_Earned
		local currency = C_CurrencyInfo.GetCurrencyInfo(currencyId)
		local max = currency.maxQuantity or 0
		local earn = currency.totalEarned or 0
		local display = 0
		local capColor = ""
		local cap = ""
		local returnText

		if currency.name and tonumber(currency.quantity) then display = currency.quantity
		else display = 0 end

		if options.Display_Weekly then cap = "/" .. max end
		if options.Display_Max and earn == max then capColor = Ns.redText end

		if Ns.Band(EB, Current) then returnText = capColor .. display .. cap end
		if Ns.Band(EB, Earned) then returnText = capColor .. earn .. cap end
		if Ns.Band(EB, Both) then returnText = capColor .. display .. "/" .. earn .. cap end
		if returnText == nil then returnText = 0 end

		HUD:UpdateText(data, returnText)
		HUD:UpdateBar(data, earn, max)
	end
end

for name, currencyId in pairs(Ns.CrestData) do
	Ns.FunctionList[name] = CrestCurrency(currencyId)
end
----------------------------------------------

-- Other currencies
local function Currency(currencyId, hasDecimals)
	return function(HUD, data, options, ...)
		local currency = C_CurrencyInfo.GetCurrencyInfo(currencyId)
		local max = currency.maxQuantity or 0
		local earn = currency.totalEarned or 0
		local weekly = currency.maxWeeklyQuantity or 0
		local capColor, cap = "", ""
		local display = 0

		if currency.name and tonumber(currency.quantity) then display = currency.quantity end

		if options.Display_Max and earn == weekly then capColor = Ns.redText end
		if options.Display_Weekly then cap = "/" .. weekly end

		if hasDecimals then
			display = Ns.DecimalNumber(options.Decimals_Default, display)
		end

		HUD:UpdateText(data, capColor .. display .. cap)
	end
end

for name, info in pairs(Ns.CurrencyData) do
	Ns.FunctionList[name] = Currency(info.id, info.decimals)
end
------------------------------------------------

-- Spark of Radiance
function Ns.FunctionList.Spark(HUD, data, options, ...)

	local EB = options.Splint_Spark
	local returnText
	local spark = C_Item.GetItemCount(232875, true)
	local dust = C_CurrencyInfo.GetCurrencyInfo(3212)
	local splinter = 0

	if dust.name and tonumber(dust.quantity) then splinter = dust.quantity
	else splinter = 0 end

	if Ns.Band(EB, Spark) then returnText = spark end
	if Ns.Band(EB, Splinter) then returnText = splinter end
	if Ns.Band(EB, Both) then returnText = spark .. " (" .. splinter .. ")" end
	if returnText == nil then returnText = 0 end

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Mysterious Skyshards
function Ns.FunctionList.Skyshards(HUD, data, options, ...)

	local item = C_Item.GetItemCount(255826, true)

	if item == nil then item = 0 end

	HUD:UpdateText(data, item)
end
------------------------------------------------

-- Vile Essence
function Ns.FunctionList.VileEssence(HUD, data, options, ...)

	local item = C_Item.GetItemCount(259361, true)

	if item == nil then item = 0 end

	HUD:UpdateText(data, item)
end
------------------------------------------------