local IC = InvenCraftInfo2
LoadAddOn("InvenCraftInfo2_DB")

local _G = _G
local type = _G.type
local tonumber = _G.tonumber
local GetAddOnMetadata = _G.GetAddOnMetadata
local GetTradeSkillRecipeLink = _G.GetTradeSkillRecipeLink

IC.itemQuality = {}
for i = 0, 7 do
	IC.itemQuality[i] = {}
	IC.itemQuality[i].r, IC.itemQuality[i].g, IC.itemQuality[i].b = GetItemQualityColor(i)
	IC.itemQuality[i].font = CreateFont(IC:GetName().."FontItemQuality"..i)
	IC.itemQuality[i].font:SetFont(TradeSkillSkill1Text:GetFont())
	IC.itemQuality[i].font:SetTextColor(IC.itemQuality[i].r, IC.itemQuality[i].g, IC.itemQuality[i].b)
	IC.itemQuality[i].font:SetShadowColor(0, 0, 0)
	IC.itemQuality[i].font:SetShadowOffset(1, -1)
	IC.itemQuality[i].font = IC.itemQuality[i].font:GetName()
end

function IC:GetColorID(id)
	if type(id) == "string" then
		if id:find("item:(%d+)") then
			return tonumber(GetAddOnMetadata("InvenCraftInfo2_UI", "X-Color-Item-"..id:match("item:(%d+)")) or "")
		elseif id:find("enchant:(%d+)") then
			return tonumber(GetAddOnMetadata("InvenCraftInfo2_UI", "X-Color-Spell-"..id:match("enchant:(%d+)")) or "")
		end
	elseif type(id) == "number" then
		return tonumber(GetAddOnMetadata("InvenCraftInfo2_UI", "X-Color-Spell-"..id) or "")
	end
	return nil
end

function IC:GetTradeSkillRecipeID(id)
	id = GetTradeSkillRecipeLink(id)
	if id then
		return tonumber(id:match("enchant:(%d+)") or "")
	end
	return nil
end

function ContainerFrameItemButton_OnModifiedClick(self, button)
	if not HandleModifiedItemClick(GetContainerItemLink(self:GetParent():GetID(), self:GetID())) then
		if IsModifiedClick("SOCKETITEM") then
			SocketContainerItem(self:GetParent():GetID(), self:GetID())
		elseif IsModifiedClick("SPLITSTACK") then
			local texture, itemCount, locked = GetContainerItemInfo(self:GetParent():GetID(), self:GetID())
			if not locked and itemCount and itemCount > 1 then
				OpenStackSplitFrame(itemCount, self, "BOTTOMRIGHT", "TOPRIGHT")
			end
		end
	end
end