----  Module by Hizuro (http://www.curseforge.com/profiles/hizuro/)


local addon, ns = ...
local C = ns.shared.color
local func = ns.shared
local L = ns.L
local obj = nil

local currency = {}
currency.data = {}
currency.iconByName = {}
currency.currencyIDs = {
	61,   -- Dalaran Jewlcrafter's Token
	81,   -- Epicurean's Award
	241,  -- Champion's Seal
	361,  -- Illustrious Jewelcrafter's Token
	384,  -- Dwarf Archaeology Fragment
	385,  -- Troll Arcaeology Fragment
	390,  -- Conquest Points
	391,  -- Toll Barad Commendation
	392,  -- Honor Points
	393,  -- Fossil Arcaeology Fragment
	394,  -- Night Elf Archaeology Fragment
	395,  -- Justice Points
	396,  -- Valor Points
	397,  -- Orc Arcaeolgy Fragment
	398,  -- Draenei Archaeology Fragment
	399,  -- Vrykul Arcaeology Fragment
	400,  -- Nerubian Archaeology Fragment
	401,  -- Tol'vir Arcaeology Fragment
	402,  -- Ironpaw Token
	416,  -- Mark of the World Tree
	515,  -- Darkmoon Prize Ticket
	614,  -- Mote of Darkness
	615,  -- Essence of Corrupted Deathwing
	676,  -- Pandaren Arcaeolgy Fragment
	677,  -- Mogu Archaeology Fragment
	678,  -- Zen Jewelcrafter's Token
	692,  -- Conquest Random BG Meta
	697,  -- Elder Charm of Good Fortune
	738,  -- Lesser Charm of Good Fortune
}
currency.events = {
	"PLAYER_ENTERING_WORLD",
	"KNOWN_CURRENCY_TYPES_UPDATE",
	"CURRENCY_DISPLAY_UPDATE"
}

-- collect currency data
local function currencyGetData()
	local cSize = GetCurrencyListSize()
	for i=0, cSize do
		local name, isHeader, _, _, _, count = GetCurrencyListInfo(i)
		currency.data[i] = {["name"] = name,["isHeader"] = isHeader,["count"] = count}
	end
end

-- build tooltip
local function currencyTooltip(t)
	if not t then return end
	t:AddHeader(C.dkyellow(L["Currency"]))
	for i,v in ipairs(currency.data) do
		if v.isHeader then
			t:AddLine(" ")
			t:AddLine(C.ltblue(v.name))
			t:AddSeparator()
		else	
			t:AddLine(C.ltyellow(v.name),v.count.."  "..currency.iconByName[v.name])
		end
	end
end

local function currencyInit(obj)
	if not obj then return end

	obj = obj.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Currency"])

	obj.OnLeave = function()
		if tooltip then
			LibQTip:Release(tooltip)
			tooltip = nil
		end
	end

	obj.OnEnter = function(_self)
		tooltip = LibQTip:Acquire("CurrencyTT", 2, "LEFT", "RIGHT")
		currencyTooltip(tooltip)
		func.createTooltip(_self,tooltip)
	end

	for i,v in ipairs(currency.currencyIDs) do
		local name,count,icon = GetCurrencyInfo(v)
		currency.iconByName[name] = "|TInterface\\Icons\\"..icon..":0|t"
	end
end

local function currencyOnEvent()
	local dataobj = obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["Currency"])
	if UnitFactionGroup("player") == "Neutral" then
		dataobj.icon = [[Interface\TARGETINGFRAME\UI-PVP-FFA]]
	end
	currencyGetData()
end

local function currencyOnClick()
	-- for later use.
	-- build and display menu
	-- options: choose 2 currencies as bottontext
end

Broker_Everything:Register(L["Currency"],
	[[Interface\PVPFrame\PVP-Currency-]]..UnitFactionGroup( "player" ),
	currencyInit,
	currencyOnEvent,
	currency.events,
	nil, -- currencyOnClick,
	0,
	nil,
	nil)
