local E, L, V, P, G = unpack(ElvUI); --Import: Engine, Locales, PrivateDB, ProfileDB, GlobalDB
local AB = E:GetModule("ActionBars");

--Cache global variables
--Lua functions
local _G = _G
local getn = table.getn
local mod = math.mod
--WoW API / Variables
local CreateFrame = CreateFrame

local microBar = CreateFrame("Frame", "microBar", E.UIParent)
microBar:SetFrameStrata("BACKGROUND")

local MICRO_BUTTONS = {
	"CharacterMicroButton",
	"SpellbookMicroButton",
	"TalentMicroButton",
	"QuestLogMicroButton",
	"SocialsMicroButton",
	"WorldMapMicroButton",
	"MainMenuMicroButton",
	"HelpMicroButton"
}

local function Button_OnEnter()
	if AB.db.microbar.mouseover then
		UIFrameFadeIn(microBar, .2, microBar:GetAlpha(), AB.db.microbar.alpha)
	end
end

local function Button_OnLeave()
	if AB.db.microbar.mouseover then
		UIFrameFadeOut(microBar, .2, microBar:GetAlpha(), 0)
	end
end

function AB:HandleMicroButton(button)
	local pushed = button:GetPushedTexture()
	local normal = button:GetNormalTexture()
	local disabled = button:GetDisabledTexture()

	button:SetParent(microBar)
	button:Show()
	button:SetAlpha(self.db.microbar.alpha)

	E:Kill(button:GetHighlightTexture())
	HookScript(button, "OnEnter", Button_OnEnter)
	HookScript(button, "OnLeave", Button_OnLeave)

	local f = CreateFrame("Frame", nil, button)
	f:SetFrameLevel(1)
	E:Point(f, "BOTTOMLEFT", button, "BOTTOMLEFT", 2, 0)
	E:Point(f, "TOPRIGHT", button, "TOPRIGHT", -2, -28)
	E:SetTemplate(f, "Default", true)
	button.backdrop = f

	pushed:SetTexCoord(0.17, 0.87, 0.5, 0.908)
	E:SetInside(pushed, f)

	normal:SetTexCoord(0.17, 0.87, 0.5, 0.908)
	E:SetInside(normal, f)

	if disabled then
		disabled:SetTexCoord(0.17, 0.87, 0.5, 0.908)
		E:SetInside(disabled, f)
	end
end

function AB:UpdateMicroPositionDimensions()
	if not microBar then return end

	local numRows = 1
	local button, prevButton, lastColumnButton
	for i = 1, getn(MICRO_BUTTONS) do
		button = _G[MICRO_BUTTONS[i]]
		prevButton = _G[MICRO_BUTTONS[i-1]] or microBar
		lastColumnButton = _G[MICRO_BUTTONS[i-self.db.microbar.buttonsPerRow]]

		button:ClearAllPoints()

		if prevButton == microBar then
			E:Point(button, "TOPLEFT", prevButton, "TOPLEFT", -2 + E.Border, 28 - E.Border)
		elseif mod((i - 1), self.db.microbar.buttonsPerRow) == 0 then
			E:Point(button, "TOP", lastColumnButton, "BOTTOM", 0, 28 - self.db.microbar.yOffset)
			numRows = numRows + 1
		else
			E:Point(button, "LEFT", prevButton, "RIGHT", - 4 + self.db.microbar.xOffset, 0)
		end
	end

	if self.db.microbar.mouseover then
		microBar:SetAlpha(0)
	else
		microBar:SetAlpha(self.db.microbar.alpha)
	end

	E:Size(microBar, ((CharacterMicroButton:GetWidth() - 4) * self.db.microbar.buttonsPerRow) + (self.db.microbar.xOffset * (self.db.microbar.buttonsPerRow - 1)) + E.Border * 2, ((CharacterMicroButton:GetHeight() - 28) * numRows) + (self.db.microbar.yOffset * (numRows - 1)) + E.Border * 2)

	if self.db.microbar.enabled then
		microBar:Show()
		if microBar.mover then
			E:EnableMover(microBar.mover:GetName())
		end
	else
		microBar:Hide()
		if microBar.mover then
			E:DisableMover(microBar.mover:GetName())
		end
	end
end

function AB:SetupMicroBar()
	E:Point(microBar, "TOPLEFT", 4, -48)

	for i = 1, getn(MICRO_BUTTONS) do
		self:HandleMicroButton(_G[MICRO_BUTTONS[i]])
	end

	E:SetInside(MicroButtonPortrait, CharacterMicroButton.backdrop)

	self:UpdateMicroPositionDimensions()

	E:CreateMover(microBar, "MicrobarMover", L["Micro Bar"], nil, nil, nil, "ALL,ACTIONBARS")
end