local E, L, V, P, G = unpack(ElvUI)
local S = E:GetModule("Skins")

local _G = _G
local unpack = unpack
local format = string.format

local GetNumSockets = GetNumSockets
local GetSocketTypes = GetSocketTypes
local hooksecurefunc = hooksecurefunc

local function LoadSkin()
	if E.private.skins.blizzard.enable ~= true or E.private.skins.blizzard.socket ~= true then return end

	local ItemSocketingFrame = _G["ItemSocketingFrame"]
	ItemSocketingFrame:StripTextures()
	ItemSocketingFrame:CreateBackdrop("Transparent")
	ItemSocketingFrame.backdrop:Point("TOPLEFT", 11, -12)
	ItemSocketingFrame.backdrop:Point("BOTTOMRIGHT", -4, 27)

	ItemSocketingFramePortrait:Kill()

	S:HandleCloseButton(ItemSocketingCloseButton)

	ItemSocketingScrollFrame:StripTextures()
	ItemSocketingScrollFrame:CreateBackdrop("Transparent")

	S:HandleScrollBar(ItemSocketingScrollFrameScrollBar, 2)

	for i = 1, MAX_NUM_SOCKETS do
		local button = _G[format("ItemSocketingSocket%d", i)]
		local icon = _G[format("ItemSocketingSocket%dIconTexture", i)]
		local bracket = _G[format("ItemSocketingSocket%dBracketFrame", i)]
		local bg = _G[format("ItemSocketingSocket%dBackground", i)]

		button:StripTextures()
		button:SetTemplate("Default", true)
		button:StyleButton(false)

		icon:SetTexCoord(unpack(E.TexCoords))
		icon:SetInside()

		bracket:Kill()
		bg:Kill()
	end

	hooksecurefunc("ItemSocketingFrame_Update", function()
		local numSockets = GetNumSockets()
		for i = 1, numSockets do
			local button = _G[format("ItemSocketingSocket%d", i)]
			local gemColor = GetSocketTypes(i)
			local color = GEM_TYPE_INFO[gemColor]

			button:SetBackdropColor(color.r, color.g, color.b, 0.15)
			button:SetBackdropBorderColor(color.r, color.g, color.b)
		end
	end)

	S:HandleButton(ItemSocketingSocketButton)
end

S:AddCallbackForAddon("Blizzard_ItemSocketingUI", "ItemSocket", LoadSkin)