local addonName, addon = ...
addonName = addonName:match("^(.+)_DevTools$")
addon = _G[addonName]
if not addon then return end

local GUI_DevBar = CreateFrame('Frame', "GUI_DevBar", UIParent)
GUI_DevBar:SetSize(145, 145)
GUI_DevBar:SetBackdrop{
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
		edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
		tile = true,
		tileSize = 32,
		edgeSize = 32,
		insets = {
			left = 11,
			right = 12,
			top = 12,
			bottom = 11
		}
	}

function addon:GrimUIMemUse()
	UpdateAddOnMemoryUsage()
	local GUIMemUse = GetAddOnMemoryUsage("GrimUI")
	print("|cff33ff99" .. addonName .. "|r: Current memory usage = ", GUIMemUse)
end

addon:GUI_BasicButtonCreate('DevBarButton1', GUI_DevBar,"Reload")
DevBarButton1:SetPoint("CENTER", 0, -40)
DevBarButton1:SetScript("OnClick", ReloadUI)
addon:GUI_BasicButtonCreate('DevBarButton2', GUI_DevBar,"Memory Use")
DevBarButton2:SetPoint("CENTER", 0, 0)
DevBarButton2:SetScript("OnClick", function() addon:GrimUIMemUse() end)
addon:GUI_BasicButtonCreate('DevBarButton3', GUI_DevBar,"Frame Stack")
DevBarButton3:SetPoint("CENTER", 0, 40)
DevBarButton3:SetScript("OnClick", function() SlashCmdList.FRAMESTACK() end)

function addon:GUI_ResetDevBar()
	GUI_DevBar:ClearAllPoints()
	GUI_DevBar:SetPoint("RIGHT", UIParent, "RIGHT", 0, 0)
end
addon:GUI_ResetDevBar()

function GUI_ShowDevBar()
	if addon.settings.showDevBar then
		addon:ShowFrame('GUI_DevBar')
	else
		addon:HideFrame('GUI_DevBar')
	end
end

function addon:BlizBuildInfo()
	local version, build, bdate, toc = GetBuildInfo()
	print(string.format("version = %s, build = %s, builddate = '%s', tocversion = %s.", version, build, bdate, toc))
end

addon.RegisterEvent("GDevBar-Initialize", 'ADDON_LOADED', function(self, event, name)
	addon.UnregisterEvent(self, event)
	GUI_ShowDevBar()
end)

-- Item ID # informatin on tooltips --
local hooksecurefunc, select, UnitBuff, UnitDebuff, UnitAura, UnitGUID, GetGlyphSocketInfo, tonumber, strfind, strsub, strmatch =
      hooksecurefunc, select, UnitBuff, UnitDebuff, UnitAura, UnitGUID, GetGlyphSocketInfo, tonumber, strfind, strsub, strmatch

local wod = select(4, GetBuildInfo()) >= 60000

local types = {
    spell      = "SpellID:",
    item       = "ItemID:",
    glyph      = "GlyphID:",
    unit       = "NPC ID:",
    quest      = "Quest ID:",
    talent     = "Talent ID:",
    achievment = "Achievement ID:"
}

local function addLine(tooltip, id, type)
    local found = false

    -- Check if we already added to this tooltip. Happens on the talent frame
    for i = 1,15 do
        local frame = _G[tooltip:GetName() .. "TextLeft" .. i]
        local text
        if frame then text = frame:GetText() end
        if text and text == type then found = true break end
    end

    if not found then
        tooltip:AddDoubleLine(type, "|cffffffff" .. id)
        tooltip:Show()
    end
end

-- All types, primarily for linked tooltips
local function onSetHyperlink(self, link)
    local type, id = string.match(link,"^(%a+):(%d+)")
    if not type or not id then return end
    if type == "spell" or type == "enchant" or type == "trade" then
        addLine(self, id, types.spell)
    elseif type == "glyph" then
        addLine(self, id, types.glyph)
    elseif type == "talent" then
        addLine(self, id, types.talent)
    elseif type == "quest" then
        addLine(self, id, types.quest)
    elseif type == "achievement" then
        addLine(self, id, types.achievement)
    elseif type == "item" then
        addLine(self, id, types.item)
    end
end

hooksecurefunc(ItemRefTooltip, "SetHyperlink", onSetHyperlink)
hooksecurefunc(GameTooltip, "SetHyperlink", onSetHyperlink)

-- Spells
hooksecurefunc(GameTooltip, "SetUnitBuff", function(self, ...)
    local id = select(11, UnitBuff(...))
    if id then addLine(self, id, types.spell) end
end)

hooksecurefunc(GameTooltip, "SetUnitDebuff", function(self,...)
    local id = select(11, UnitDebuff(...))
    if id then addLine(self, id, types.spell) end
end)

hooksecurefunc(GameTooltip, "SetUnitAura", function(self,...)
    local id = select(11, UnitAura(...))
    if id then addLine(self, id, types.spell) end
end)

hooksecurefunc("SetItemRef", function(link, ...)
    local id = tonumber(link:match("spell:(%d+)"))
    if id then addLine(ItemRefTooltip, id, types.spell) end
end)

GameTooltip:HookScript("OnTooltipSetSpell", function(self)
    local id = select(3, self:GetSpell())
    if id then addLine(self, id, types.spell) end
end)

-- NPCs
GameTooltip:HookScript("OnTooltipSetUnit", function(self)
    if C_PetBattles.IsInBattle() then return end
    local unit = select(2, self:GetUnit())
    if unit then
        local id = wod and tonumber(strmatch(UnitGUID(unit) or "", "-(%d+)-%x+$"), 10) or tonumber(strsub(UnitGUID(unit) or "", 6, 10), 16)
        -- ID 970 seems to be used for players
        if (id ~= 0) and (id ~= 970) then addLine(GameTooltip, id, types.unit) end
    end
end)

-- Items
local function attachItemTooltip(self)
    local link = select(2, self:GetItem())
    if link then
        local id = select(3, strfind(link, "^|%x+|Hitem:(%-?%d+):(%d+):(%d+).*"))
        if id then addLine(self, id, types.item) end
    end
end

GameTooltip:HookScript("OnTooltipSetItem", attachItemTooltip)
ItemRefTooltip:HookScript("OnTooltipSetItem", attachItemTooltip)
ItemRefShoppingTooltip1:HookScript("OnTooltipSetItem", attachItemTooltip)
ItemRefShoppingTooltip2:HookScript("OnTooltipSetItem", attachItemTooltip)
ShoppingTooltip1:HookScript("OnTooltipSetItem", attachItemTooltip)
ShoppingTooltip2:HookScript("OnTooltipSetItem", attachItemTooltip)

-- Glyphs
--hooksecurefunc(GameTooltip, "SetGlyph", function(self, ...)
   -- local id = select(4, GetGlyphSocketInfo(...))
    --if id then addLine(self, id, types.glyph) end
--end)

--hooksecurefunc(GameTooltip, "SetGlyphByID", function(self, id)
    --if id then addLine(self, id, types.glyph) end
--end)