Quantcast
local A, L = ...

-----------------------------
-- rSkin Global
-----------------------------
rSkin = CreateFrame("Frame")
rSkin.addonName = A
-- init skins table
rSkin.skins = {}

local Hider = CreateFrame("Frame", nil, UIParent) Hider:Hide()

local function ThrowError(err, message)
	if not err then return end

	err = format("%s: %s Error\n%s", A, message, err)

	if _G.BaudErrorFrameHandler then
		_G.BaudErrorFrameHandler(err)
	else
		_G.ScriptErrorsFrame:OnError(err, false, false)
	end
end

-- Kills --
local function Kill(self)
	if (self.UnregisterAllEvents) then
		self:UnregisterAllEvents()
		self:SetParent(Hider)
	else
		self.Show = self.Hide
	end

	self:Hide()
end
L.Kill = Kill

local function StripTextures(self)
	for i = 1, self:GetNumRegions() do
		local Region = select(i, self:GetRegions())
		if (Region and Region:GetObjectType() == "Texture") then
			Region:SetTexture(nil)
			Kill(Region)
		end
	end
end
L.StripTextures = StripTextures

--CreateBackdrop
local function CreateBackdrop(self, relativeTo)
  local backdrop = oUF_SimpleConfig.backdrop
  local bd = CreateFrame("Frame", nil, self, BackdropTemplateMixin and "BackdropTemplate")
  bd:SetFrameLevel(self:GetFrameLevel()-1 or 0)
  bd:SetPoint("TOPLEFT", relativeTo or self, "TOPLEFT", -backdrop.inset, backdrop.inset)
  bd:SetPoint("BOTTOMRIGHT", relativeTo or self, "BOTTOMRIGHT", backdrop.inset, -backdrop.inset)
  bd:SetBackdrop(backdrop)
  bd:SetBackdropColor(unpack(backdrop.bgColor))
  bd:SetBackdropBorderColor(unpack(backdrop.edgeColor))
  return bd
end
L.CreateBackdrop = CreateBackdrop

local function Dummy() return end
L.Dummy = Dummy

function rSkin:RegisterSkin(name, func)
    if not self.skins[name] then
		self.skins[name] = func
	end
end

function rSkin:OnLogin()
    print("|cffffa6c9"..A.." loaded.|r")
    for name, func in next, self.skins do
		if name and type(func) == "function" then
			local _, catch = pcall(func)
			ThrowError(catch, format("%s Skin", name))
		end
	end
end

rSkin:RegisterEvent("PLAYER_LOGIN")

-- Handle the events as they happen
rSkin:SetScript("OnEvent", function(self, event, ...)
	if (event == "PLAYER_LOGIN") then
		self:OnLogin()
	end
end)