--Macaroon, a World of Warcraft® user interface addon.
--Copyright© 2006-2010 Connor H. Chenoweth, aka Maul - All rights reserved.

local M = Macaroon

local groups, group, bf, SD, pew = {}
local gmatch = string.gmatch

local function createBF()

	if (LibStub) then

		bf = LibStub("LibButtonFacade", true)

		if (bf) then

			bf:RegisterSkinCallback("Macaroon", M.BFskinCallback, Macaroon)

			-- because my version is prettier
			bf:AddSkin("Blizzard",{

				-- Skin data start.
				Normal = {
					Width = 56,
					Height = 56,
					Texture = [[Interface\Buttons\UI-Quickslot2]],
					EmptyTexture = [[Interface\Buttons\UI-Quickslot]],
				},
				Pushed = {
					Width = 32,
					Height = 32,
					Texture = [[Interface\Buttons\UI-Quickslot-Depress]],
				},
				Checked = {
					Width = 32,
					Height = 32,
					Texture = [[Interface\Buttons\CheckButtonHilight]],
					BlendMode = "ADD",
				},
				Highlight = {
					Width = 32,
					Height = 32,
					Texture = [[Interface\Buttons\ButtonHilight-Square]],
					BlendMode = "ADD",
				},
				Border = {
					Width = 60,
					Height = 60,
					Texture = [[Interface\Buttons\UI-ActionButton-Border]],
					BlendMode = "ADD",
				},
				Gloss = {
					Width = 31,
					Height = 31,
					Texture = [[]],
				},
				Disabled = {
					Hide = true,
				},
				Icon = {
					Width = 31,
					Height = 31,
					TexCoords = {0.07,0.93,0.07,0.93},
				},
				Cooldown = {
					Width = 31,
					Height = 31,
				},
				Backdrop = {
					Width = 31,
					Height = 31,
					Texture = [[Interface\Buttons\UI-EmptySlot]],
					OffsetY = -0.5,
					TexCoords = {0.2,0.8,0.2,0.8},
					Red = 1,
					Green = 1,
					Blue = 1,
					Alpha = 1,
				},
				HotKey = {
					Width = 36,
					Height = 10,
					OffsetX = -2,
					OffsetY = 11,
				},
				Count = {
					Width = 36,
					Height = 10,
					OffsetX = -2,
					OffsetY = -11,
				},
				Name = {
					Width = 36,
					Height = 10,
					OffsetY = -11,
				},
				AutoCast = {
					Width = 27,
					Height = 27,
				},
				AutoCastable = {
					Width = 54,
					Height = 54,
					Texture = [[Interface\Buttons\UI-AutoCastableOverlay]],
				},
				Flash = {
					Width = 32,
					Height = 32,
					Texture = [[Interface\Buttons\UI-QuickslotRed]],
				},
				-- Skin data end.

			},true)
		end
	end
end

function M.SkinBar(bar, parent)

	if (not SD.bfskin) then SD.bfskin = {} end

	if (not bf) then createBF() end

	if (bf) then

		group = bar.config.name

		if (not SD.bfskin[group] and parent and SD.bfskin[parent.config.name]) then
			SD.bfskin[group] = M.CopyTable(SD.bfskin[parent.config.name])
		elseif (not SD.bfskin[group]) then
			SD.bfskin[group] = {}
		end

		groups[group] = bar

		if (bar.config.buttonList) then

			for state, btnIDs in pairs(bar.config.buttonList) do

				for btnID in gmatch(btnIDs, "[^;]+") do

					button = _G[bar.btnType..btnID]

					if (button and not button.skinset) then

						if (button.bagelement) then

							local btnData = {
								Normal = button.normaltexture,
								Icon = button.icontexture,
								Count = button.count,
							}

							bf:Group("Macaroon", group):AddButton(button.bagelement, btnData)

						elseif (button.menuelement) then

							--local menuRatios = { w = 0.71, h = 0.95 }

							--button.menuelement.icontexture:SetDrawLayer("BACKGROUND")
							--button.menuelement.icontexture:SetTexCoord(-0.1, 1.1, 0.3, 1.07)
							--button.menuelement:SetWidth(button:GetWidth())
							--button.menuelement:SetHeight(button:GetHeight())
							--button.menuelement:SetHitRectInsets(0,0,0,0)

							--local btnData = {
							--	Normal = button.menuelement.normaltexture,
							--}

							--bf:Group("Macaroon", group):AddButton(button.menuelement, btnData)

						elseif (button.config.type == "macro" or
						        button.config.type == "action" or
						        button.config.type == "pet") then

							local btnData = {
								Normal = button.normaltexture,
								Icon = button.iconframeicon,
								Cooldown = button.iconframecooldown,
								HotKey = button.hotkey,
								Count = button.count,
								Name = button.name,
								Border = button.border,
								AutoCast = false,
							}

							bf:Group("Macaroon", group):AddButton(button, btnData)
						end

						button.skinset = true
					end
				end
			end
		end

		if (bar.buttonCountChanged) then

			bf:Group("Macaroon", group):Skin(SD.bfskin[group].Skin,
				      SD.bfskin[group].Gloss,
				      SD.bfskin[group].Backdrop,
				      SD.bfskin[group].Colors)

			bar.buttonCountChanged = nil
		end
	end
end

function M.BFskinCallback(arg, skin, gloss, backdrop, group, button, colors)

	if (not group) then return end

	if (not SD.bfskin[group]) then SD.bfskin[group] = {} end

	SD.bfskin[group].Skin = skin
	SD.bfskin[group].Gloss = gloss
	SD.bfskin[group].Backdrop = backdrop
	SD.bfskin[group].Colors = colors

	if (groups[group]) then

		local bar, skins, button = groups[group], bf:GetSkins()

		if (skins[skin] and skins[skin].Normal.Texture ~= "Interface\\Buttons\\UI-Quickslot2") then

			bar.hasAction = false
			bar.noAction = false

		elseif (skins[skin]) then

			bar.hasAction = skins[skin].Normal.Texture
			bar.noAction = skins[skin].Normal.EmptyTexture
		end

		bar.bf_skin = skin:lower()

		for state, btnIDs in pairs(bar.config.buttonList) do

			for btnID in gmatch(btnIDs, "[^;]+") do

				button = _G[bar.btnType..btnID]

				if (button and button.updateData) then
					button.updateData(button, bar, state)
				end
			end
		end
	end

	M.Save()
end

function M.UpdateSkinData()
	if (pew) then

		local bar

		for k,v in pairs(SD.bfskin) do

			bar = M.BarIndexByName[k]

			if (not bar or (bar and not bar.config.skinnable)) then
				SD.bfskin[k] = nil
			end
		end
	end
end

local function controlOnEvent(self, event, ...)

	if (event == "ADDON_LOADED" and ... == "Macaroon") then

		SD = MacaroonSavedState

		tinsert(M.UpdateFunctions, M.UpdateSkinData)

	elseif (event == "PLAYER_ENTERING_WORLD" and not pew) then

		pew = true

		M.UpdateSkinData()

	end
end

local frame = CreateFrame("Frame", nil, UIParent)
frame:SetScript("OnEvent", controlOnEvent)
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")