local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local mMT = E:GetModule(mPlugin);
local DT = E:GetModule("DataTexts");
local addon, ns = ...

--Lua functions
local format = format
local strjoin = strjoin

--WoW API / Variables
local _G = _G
local C_CurrencyInfo = C_CurrencyInfo

--Variables
local displayString, lastPanel = ""
local mText = format("mMediaTag %s", L["Grateful Offering"])
local mTextName = "mGratefulOffering"
local mCurrencyID = 1885

local function OnEnter(self)
	DT:SetupTooltip(self)
	DT.tooltip:SetHyperlink(mMT:mCurrencyLink(mCurrencyID))
	DT.tooltip:Show()
end

local function OnEvent(self, event, ...)
	local info = C_CurrencyInfo.GetCurrencyInfo(mCurrencyID)
	if info then
		local name = info.name .. " "
		local CurrencValue = info.quantity
		
		if not E.db[mPlugin].mGratefulOffering.name then
			name = ""
		end
		
		if E.db[mPlugin].mGratefulOffering.icon then
			name = format("%s %s", mMT:mIcon(info.iconFileID), name)
		end
		
		if E.db[mPlugin].mGratefulOffering.short then
			CurrencValue = E:ShortValue(info.quantity, 2)
		end
		
		if E.db[mPlugin].mGratefulOffering.style == "auto" then
			self.text:SetFormattedText(displayString, name ~= "" and name, CurrencValue)
		elseif E.db[mPlugin].mGratefulOffering.style == "color" then
			self.text:SetText(format("%s|CFFF4D03F%s|r", name, CurrencValue))
		elseif E.db[mPlugin].mGratefulOffering.style== "white" then
			self.text:SetText(format("|CFFFFFFFF%s%s|r", name, CurrencValue))
		end
	else
		self.text:SetText("|CFFE74C3CERROR!|r")
	end
	
	lastPanel = self
end

local function OnLeave(self)
	DT.tooltip:Hide()
end

local function ValueColorUpdate(hex)
	displayString = strjoin('', '%s', hex, '%s|r')
	
	if lastPanel ~= nil then
		OnEvent(lastPanel, "ELVUI_COLOR_UPDATE")
	end
end

E.valueColorUpdateFuncs[ValueColorUpdate] = true

DT:RegisterDatatext(mTextName, _G.CURRENCY, {'CHAT_MSG_CURRENCY', 'CURRENCY_DISPLAY_UPDATE'}, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, ValueColorUpdate)