local _, ItemProfConstants = ...

local frame = CreateFrame( "Frame" )

local previousItemID = -1
local itemIcons = ""
local iconSize

local ITEM_PROF_FLAGS = ItemProfConstants.ITEM_PROF_FLAGS
local NUM_PROFS_TRACKED = ItemProfConstants.NUM_PROF_FLAGS - 1
local PROF_TEXTURES = ItemProfConstants.PROF_TEXTURES

local QUEST_TEXT

local showProfs
local showQuests
local profFilter
local questFilter

local t = {}



local function CreateItemIcons( itemFlags )

	for k in pairs( t ) do t[ k ] = nil end
	
	if showProfs then
	
		local enabledFlags = bit.band( itemFlags, profFilter )
		for i=0, NUM_PROFS_TRACKED do
			local bitMask = bit.lshift( 1, i )
			local isSet = bit.band( enabledFlags, bitMask )
			if isSet ~= 0 then
				t[ #t+1 ] = "|T"
				t[ #t+1 ] = PROF_TEXTURES[ bitMask ]
				t[ #t+1 ] = iconSize
			end
		end
	end
	
	if showQuests then
		-- Quest filter flags start at 0x400, shift to bit 0 will align with config filter
		local questFlags = bit.rshift( itemFlags, 10 )
		local isSet = bit.band( questFlags, questFilter )
		
		-- Check if the quest is faction exclusive
		local isFactionQuest = bit.band( questFlags, 0x06 )
		if isFactionQuest ~= 0 then
			-- Ignore the quest if the configuration isnt tracking this faction
			local isFactionEnabled = bit.band( isFactionQuest, questFilter )
			local showFaction = bit.band( isFactionQuest, isFactionEnabled )
			if showFaction == 0 then
				isSet = 0
			end
			
			-- Both flags must be set if the faction quest was for a specific class/profession
			if isSet < 0x08 and questFlags >= 0x08 then
				isSet = 0
			end
		end
		
		if isSet ~= 0 then
			t[ #t+1 ] = QUEST_TEXT
		end
	end

	return table.concat( t )
end


local function ModifyItemTooltip( tt ) 
		
	local itemName, itemLink = tt:GetItem() 
	if not itemName then return end
	local itemID = select( 1, C_Item.GetItemInfoInstant( itemName ) )
	
	if itemID == nil then
		-- Extract ID from link: GetItemInfoInstant unreliable with AH items (uncached on client?)
		itemID = tonumber( string.match( itemLink, "item:?(%d+):" ) )
		if itemID == nil then
			-- The item link doesn't contain the item ID field
			return
		end
	end
	
	-- Reuse the texture state if the item hasn't changed
	if previousItemID == itemID then
		tt:AddLine( itemIcons )
		return
	end
	
	-- Check if the item is a profession reagent
	local itemFlags = ITEM_PROF_FLAGS[ itemID ]
	if itemFlags == nil then
		-- Don't modify the tooltip
		return
	end
	
	-- Convert the flags into texture icons
	previousItemID = itemID
	itemIcons = CreateItemIcons( itemFlags )
	
	tt:AddLine( itemIcons )
end


function ItemProfConstants:ConfigChanged( config )

	showProfs = config.showProfs
	showQuests = config.showQuests
	profFilter = config.profFlags
	questFilter = config.questFlags
	iconSize = config.iconSize
	-- Refresh constants used in texture construction
	iconSize = ":" .. iconSize .. "|t "
	QUEST_TEXT = "|T" .. PROF_TEXTURES[ ItemProfConstants.QUEST_FLAG ] .. iconSize

	previousItemID = -1		-- Reset line
end


local function InitFrame()

	GameTooltip:HookScript( "OnTooltipSetItem", ModifyItemTooltip )
	ItemRefTooltip:HookScript( "OnTooltipSetItem", ModifyItemTooltip )
end


InitFrame()
