Fizzle_Inspect = Fizzle:NewModule("Inspect")
local _G = getfenv(0)
local deformat = AceLibrary("Deformat-2.0")
local L = AceLibrary("AceLocale-2.2"):new("Fizzle_Inspect")
L:RegisterTranslations("enUS", function() return {
	["Inspect"] = true,
	["Show item quality when inspecting people."] = true,
} end)

L:RegisterTranslations("koKR", function() return {
	["Inspect"] = "살펴보기",
	["Show item quality when inspecting people."] = "대상을 살펴볼때 아이템의 품질을 표시합니다.",
} end)

function Fizzle_Inspect:OnInitialize()
	self.db = Fizzle:AcquireDBNamespace("Inspect")

	-- Table 'o' goodies
	self.slots = self.slots or {
		{slot = "Head"},
		{slot = "Shoulder"},
		{slot = "Chest"},
		{slot = "Waist"},
		{slot = "Legs"},
		{slot = "Feet"},
		{slot = "Wrist"},
		{slot = "Hands"},
		{slot = "MainHand"},
		{slot = "SecondaryHand"},
		{slot = "Ranged"},
		{slot = "Ammo"},
		{slot = "Neck"},
		{slot = "Back"},
		{slot = "Finger0"},
		{slot = "Finger1"},
		{slot = "Trinket0"},
		{slot = "Trinket1"},
		{slot = "Relic"}
	}

	-- toggle for the module
	Fizzle.options.args.inspect = {
		name = L["Inspect"],
		desc = L["Show item quality when inspecting people."],
		type = "toggle",
		get = function() return Fizzle:IsModuleActive("Inspect") end,
		set = function(v) Fizzle:ToggleModuleActive("Inspect", v) end
	}
end

function Fizzle_Inspect:OnEnable()
	if IsAddOnLoaded("Blizzard_InspectUI") then
		self:SecureHook("InspectFrame_OnShow")
		self:SecureHook("InspectFrame_OnHide")
		self:InspectFrame_OnShow()
	else
		self:RegisterEvent("ADDON_LOADED")
	end
end

function Fizzle_Inspect:OnDisable()
	-- Hide all borders if we get disabled.
	for _, item in pairs(self.slots) do
		local border = _G[item.slot .."FizzspectB"]
		if border then
			border:Hide()
		end
	end
end

function Fizzle_Inspect:CreateBorders()
	for _, item in pairs(self.slots) do
		-- Create borders
		Fizzle:CreateBorder("Inspect", item.slot, "Fizzspect", false)
	end
	self.booted = true
end

function Fizzle_Inspect:UpdateBorders()
	if not InspectFrame:IsVisible() then return end
	for _, item in pairs(self.slots) do
		local id
		if _G["Character".. item.slot .."Slot"] then
			id = _G["Character".. item.slot .."Slot"]:GetID()
		end
		if not id then return end
		local link = GetInventoryItemLink("target", id)
		local border = _G[item.slot .."FizzspectB"]
		if link then
			local itemID = select(2, deformat(link, "%s|Hitem:%d:%d:%d:%d:%d:%d:%d:%d|h[%s]|h%s"))
			local quality = select(3, GetItemInfo(itemID))
			
			if quality then
				local r, g, b, _ = GetItemQualityColor(quality)
				border:SetVertexColor(r, g, b)
				border:Show()
			else
				border:Hide()
			end
		else
			if border then
				border:Hide()
			end
		end
	end
end

function Fizzle_Inspect:ADDON_LOADED()
	-- If the Blizzard InspectUI is loading, fire up the addon!
	if arg1 == "Blizzard_InspectUI" then
		self:SecureHook("InspectFrame_OnShow")
		self:SecureHook("InspectFrame_OnHide")
		self:UnregisterEvent("ADDON_LOADED")
		self:InspectFrame_OnShow()
	end
end

function Fizzle_Inspect:Registration(event, register)
	if register then
		-- Register the event
		if not self:IsEventRegistered(event) then
			self:RegisterEvent(event, "UpdateBorders")
		end
	else
		-- Unregister the event
		if self:IsEventRegistered(event) then
			self:UnregisterEvent(event)
		end
	end
end

function Fizzle_Inspect:InspectFrame_OnShow()
	-- Hook the unit changed thing, so that the borders update if equipment is changed
	self:Registration("UNIT_INVENTORY_CHANGED", true)
	self:Registration("PLAYER_TARGET_CHANGED", true)

	-- Create the borders if we're just loading.
	if not self.booted then
		self:CreateBorders()
	end
	
	-- Update the borders
	self:UpdateBorders()
end

function Fizzle_Inspect:InspectFrame_OnHide()
	self:Registration("UNIT_INVENTORY_CHANGED", false)
	self:Registration("PLAYER_TARGET_CHANGED", false)
end
