local E, L, V, P, G = unpack(ElvUI);
local MERS = E:GetModule('MuiSkins')

-- Cache global variables
-- Lua functions
local pairs, select, unpack = pairs, select, unpack
-- WoW API / Variables
local IsAddOnLoaded = IsAddOnLoaded
-- GLOBALS: WeakAuras

-- WEAKAURAS SKIN
local frame = CreateFrame("Frame")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
frame:SetScript("OnEvent", function(self, event)
	if not IsAddOnLoaded("WeakAuras") or not E.private.muiSkins.addonSkins.wa then return end

	local function Skin_WeakAuras(frame, ftype)
		if not frame.backdrop then
			MERS:CreateBackdrop(frame, 'Transparent')
			MERS:SkinTexture(frame.icon)
			frame.icon.SetTexCoord = function () end

			if ftype == 'icon' then
				frame.Backdrop:HookScript('OnUpdate', function(self)
					self:SetAlpha(self:GetParent().icon:GetAlpha())
				end)
			end
		end

		if ftype == 'aurabar' then
			frame.Backdrop:Hide()
		end

		if ftype == 'icon' then
			E:RegisterCooldown(frame.cooldown)
		end

		if frame.icon then
			frame.icon:SetTexCoord(unpack(E.TexCoords))
			frame.icon.SetTexCoord = function() end
		end

		if frame.border then
			frame.border:Hide()
		end

		if frame.bar then
			frame.bar.fg:SetTexture(E["media"].normTex)
			frame.bar.bg:SetTexture(E["media"].blankTex)
		end

		if frame.stacks then
			frame.stacks:SetFont(E['media'].muiRoboto, 14, "OUTLINE")
			frame.stacks:SetShadowOffset(0, -0)
		end

		if frame.timer then
			frame.timer:SetFont(E['media'].muiRoboto, 16, "OUTLINE")
			frame.timer:SetShadowOffset(0, -0)
		end

		if frame.text then
			frame.text:SetFont(E['media'].muiRoboto, 14, "OUTLINE")
			frame.text:SetShadowOffset(0, -0)
		end

		if frame.cooldown then
			frame.cooldown:GetRegions():SetFont(E['media'].muiRoboto, 14, "OUTLINE")
		end
	end

	local Create_Icon, Modify_Icon = WeakAuras.regionTypes.icon.create, WeakAuras.regionTypes.icon.modify
	local Create_AuraBar, Modify_AuraBar = WeakAuras.regionTypes.aurabar.create, WeakAuras.regionTypes.aurabar.modify

	WeakAuras.regionTypes.icon.create = function(parent, data)
		local region = Create_Icon(parent, data)
		Skin_WeakAuras(region, 'icon')
		return region
	end

	WeakAuras.regionTypes.aurabar.create = function(parent)
		local region = Create_AuraBar(parent)
		Skin_WeakAuras(region, 'aurabar')
		return region
	end

	WeakAuras.regionTypes.icon.modify = function(parent, region, data)
		Modify_Icon(parent, region, data)
		Skin_WeakAuras(region, 'icon')
	end

	WeakAuras.regionTypes.aurabar.modify = function(parent, region, data)
		Modify_AuraBar(parent, region, data)
		Skin_WeakAuras(region, 'aurabar')
	end

	for weakAura, _ in pairs(WeakAuras.regions) do
		if WeakAuras.regions[weakAura].regionType == "icon" or WeakAuras.regions[weakAura].regionType == "aurabar" then
			Skin_WeakAuras(WeakAuras.regions[weakAura].region)
		end
	end
end)