--[[
	TitanGarrisonResources: A simple display of your current Garrison Resources value.
	Author: Canettieri, Eliote
	
	Base written by subwired.
--]]

-- Default language (enUS)
local L = {}
L["buttonLabel"] = "Resources: "
L["tooltipTitle"] = "Garrison Resources"
L["tooltipDescription"] = "Earn resources to build-up and\rexpand your garrison."
L["tooltipCountLabel"] = "Total Maximum: "
if GetLocale() == 'zhCN' then
	L["buttonLabel"] = "各类物资: "
	L["tooltipTitle"] = "各类物資"
	L["tooltipDescription"] = "要塞物资,远古魔力,战争物资,虚空碎片."
	L["tooltipCountLabel"] = "总量: "
elseif GetLocale() == 'zhTW' then
	L["buttonLabel"] = "各类物資: "
	L["tooltipTitle"] = "各类物資"
	L["tooltipDescription"] = "要塞物资,远古魔力,战争物资,虚空碎片."
	L["tooltipCountLabel"] = "總量: "
end


local menutext = "Titan|cffff8800 "..L["tooltipTitle"].."|r"
local ID = "GR"
local elap, currencyCount = 0, 0.0
local currencyMaximum

-- Main button frame and addon base
local f = CreateFrame("Button", "TitanPanelGRButton", CreateFrame("Frame", nil, UIParent), "TitanPanelComboTemplate")
f:SetFrameStrata("FULLSCREEN")
f:SetScript("OnEvent", function(this, event, ...) this[event](this, ...) end)
f:RegisterEvent("ADDON_LOADED")
local GARRISON_ICON = format("\124T%s:%d:%d:0:0:64:64:4:60:4:60\124t", select(3, GetCurrencyInfo(1220)), 16, 16)
local MAGIC_ICON = format("\124T%s:%d:%d:0:0:64:64:4:60:4:60\124t", select(3, GetCurrencyInfo(1155)), 16, 16)--1155远古魔力
local SUMMONABLE_ICON = format("\124T%s:%d:%d:0:0:64:64:4:60:4:60\124t", select(3, GetCurrencyInfo(1342)), 16, 16)--1342抗魔联军战争物资
local VOID_ICON =  format("\124T%s:%d:%d:0:0:64:64:4:60:4:60\124t", select(3, GetCurrencyInfo(1226)), 16, 16)--1226虚空碎片
local numGarrisonResources, numMagicResources, numSummonableResources, numVoidResources = 0, 0, 0, 0

function f:ADDON_LOADED(a1)
--print ("a1 = " .. a1)
	if a1 ~= "Titan" then -- needs to be the name of the folder that the addon is in
	return 
	end
	self:UnregisterEvent("ADDON_LOADED")
	self.ADDON_LOADED = nil
	
	-- Other languages
	if GetLocale() == "ptBR" then
		L["buttonLabel"] = "Recursos: "
		L["tooltipTitle"] = "Recursos da Guarnição"
		L["tooltipDescription"] = "Ganhe recursos para expandir ou\rconstruir sua Guarnição."
		L["tooltipCountLabel"] = "Máximo total: "
	end

	local name, isHeader, isExpanded, isUnused, isWatched, count, icon, maximum, hasWeeklyLimit, currentWeeklyAmount, unknown
	local i = 0
	local CurrencyIndex = 0
	local myicon = "Interface\\Icons\\inv_garrison_resource.blp"
	local mycheck = "Interface\\Icons\\Inv_Garrison_Resource"

	self.registry = {
		id = ID,
		menuText = menutext,
		buttonTextFunction = "TitanPanelGRButton_GetButtonText",
		tooltipTitle = L["tooltipTitle"],
		tooltipTextFunction = "TitanPanelGRButton_GetTooltipText",
		frequency = 2,
		icon = myicon,
		iconWidth = 16,
		category = "Information",
		savedVariables = {
			ShowIcon = 1,
			ShowLabelText = false,

		},
	}
	self:SetScript("OnUpdate", function(this, a1)
		elap = elap + a1
		if elap < 1 then return end
		
		numGarrisonResources = select(2, GetCurrencyInfo(1220))
		numMagicResources = select(2, GetCurrencyInfo(1155))
		numSummonableResources = select(2, GetCurrencyInfo(1342))
		numVoidResources = select(2, GetCurrencyInfo(1226))
	--	name, isHeader, isExpanded, isUnused, isWatched, count, icon, maximum, hasWeeklyLimit, currentWeeklyAmount, unknown = GetCurrencyListInfo(1155)

	--	 currencyCount = count
	--	currencyMaximum = maximum
		
		TitanPanelButton_UpdateButton(ID)
		elap = 0
	end)
		
	--TitanPanelButton_OnLoad(self)
end


----------------------------------------------
function TitanPanelGRButton_GetButtonText()
----------------------------------------------
	local currencyCountText
--	if not currencyCount then
--		currencyCountText = TitanUtils_GetHighlightText("0")
--	else
--		currencyCountText = TitanUtils_GetHighlightText(string.format("%.0f", currencyCount) .."")
--	end
	currencyCountText = TitanUtils_GetHighlightText(string.format("%s %s %s %s %s %s %s",numGarrisonResources,MAGIC_ICON,numMagicResources,SUMMONABLE_ICON,numSummonableResources,VOID_ICON,numVoidResources))
	return L["buttonLabel"], currencyCountText
end

-----------------------------------------------
function TitanPanelGRButton_GetTooltipText() --Thanks to my friend, Eliote
-----------------------------------------------
--	local valorAtual = TitanUtils_GetHighlightText(Util_StringComDefault(numMagicResources, "0"))
--	local valorMaximo = TitanUtils_GetHighlightText(Util_StringComDefault(currencyMaximum, "0"))
	--local ccccc = string.format("%s %s %s %s %s %s %s",numGarrisonResources,MAGIC_ICON,numMagicResources,SUMMONABLE_ICON,numSummonableResources,VOID_ICON,numVoidResources))
	local ccc = "\n"..GARRISON_ICON.." 要塞物资:  "..numGarrisonResources..'\n'..MAGIC_ICON..' 远古魔力:  '..numMagicResources..'\n'..SUMMONABLE_ICON..' 战争物资:  '..numSummonableResources..'\n'..VOID_ICON..' 虚空碎片:  '..numVoidResources

	return ccc;
--	return L["tooltipDescription"].."\r\r"..L["tooltipCountLabel"]..valorAtual.." / "..valorMaximo
end

local temp = {}
local function UIDDM_Add(text, func, checked, keepShown)
	temp.text, temp.func, temp.checked, temp.keepShownOnClick = text, func, checked, keepShown
	Lib_UIDropDownMenu_AddButton(temp)
end
----------------------------------------------------
function TitanPanelRightClickMenu_PrepareGRMenu()
----------------------------------------------------
	TitanPanelRightClickMenu_AddTitle(TitanPlugins[ID].menuText)
	
	TitanPanelRightClickMenu_AddToggleIcon(ID)
	TitanPanelRightClickMenu_AddToggleLabelText(ID)
	TitanPanelRightClickMenu_AddSpacer()
	TitanPanelRightClickMenu_AddCommand(TITAN_PANEL_MENU_HIDE, ID, TITAN_PANEL_MENU_FUNC_HIDE)
end

----------------------------------------------------
function Util_StringComDefault(v,d)
----------------------------------------------------
	if not v then
		return d
	end
	
	return v
end