local E, L, V, P, G = unpack(ElvUI); 
local UB = E:GetModule('SLE_UIButtons');
local Btemplate = "SecureActionButtonTemplate"
local NumBut = 5
local BorderColor = E['media'].bordercolor

local function OnEnter(self)
	UIBFrame:SetAlpha(1)
end

local function OnLeave(self)
	if E.db.sle.uibuttons.mouse then
		UIBFrame:SetAlpha(0)
	end
end

function UB:UpdateMouseOverSetting()
	if E.db.sle.uibuttons.mouse then
		UIBFrame:SetAlpha(0)
	else
		UIBFrame:SetAlpha(1)
	end
end

local UIBFrame = CreateFrame('Frame', "UIBFrame", E.UIParent);
UIBFrame:SetFrameLevel(5);
UIBFrame:SetFrameStrata('BACKGROUND');
UIBFrame:Point("LEFT", E.UIParent, "LEFT", -2, 0);
UIBFrame:SetClampedToScreen(true)
UIBFrame:HookScript('OnEnter', OnEnter)
UIBFrame:HookScript('OnLeave', OnLeave)

local Cbutton = CreateFrame("Button", "ConfigUIButton", UIBFrame, Btemplate)
local Rbutton = CreateFrame("Button", "ReloadUIButton", UIBFrame, Btemplate)
local Mbutton = CreateFrame("Button", "MoveUIButton", UIBFrame, Btemplate)
local Bbutton = CreateFrame("Button", "Bbutton", UIBFrame, Btemplate)
local Abutton = CreateFrame("Button", "Abutton", UIBFrame, Btemplate)

if IsAddOnLoaded("iFilger_ConfigUI") then
	local Fbutton = CreateFrame("Button", "Fbutton", UIBFrame)
	NumBut = 6
end

local ButtonTable = {
	Cbutton,
	Rbutton,
	Mbutton,
	Bbutton,
	Abutton,
}

local function CreateB(button, symbol, text, name, desc)
	button:CreateBackdrop()
	local button_text = button:CreateFontString(nil, 'OVERLAY')
	button_text:SetFont(E["media"].normFont, 10)
	button_text:SetText(symbol)
	button_text:SetPoint("CENTER", button, "CENTER")
	button:SetAttribute("type1", "macro")
	if button == Bbutton then
		if IsAddOnLoaded("DXE_Loader") then
			button:SetAttribute("macrotext1", "/dxe config")
		elseif IsAddOnLoaded("Bigwigs") then
			button:SetAttribute("macrotext1", "/bigwigs")
		elseif IsAddOnLoaded("DBM-Core") then
			button:SetAttribute("macrotext1", "/dbm options")
		elseif IsAddOnLoaded("VEM-Core") then
			button:SetAttribute("macrotext1", "/vem")
		end
	elseif button == Abutton then
		-- if IsAddOnLoaded("ACP") then
			-- button:SetAttribute("macrotext1", "/acp")
		-- elseif IsAddOnLoaded("Ampere") then
			-- button:SetAttribute("macrotext1", "/ampere")
		-- else
			-- button:SetAttribute("macrotext1", "/stam")
		-- end
		button:SetScript("OnClick", function(self) GameMenuButtonAddons:Click() end)
	else
		button:SetAttribute("macrotext1", text)
	end

	button:SetScript("OnEnter", function(self)
		GameTooltip:SetOwner(self, "ANCHOR_TOP", 30,0)
		GameTooltip:AddLine(name, .6, .6, .6, .6, .6, 1)
		GameTooltip:AddLine(desc, 1, 1, 1, 1, 1, 1)
		GameTooltip:Show()
	end)

	button:SetScript("OnLeave", function(self) 
		GameTooltip:Hide() 
	end)
	button:HookScript('OnEnter', OnEnter)
	button:HookScript('OnLeave', OnLeave)
end

local function CreateButtons()
	CreateB(Cbutton, "C", "/ec", L["ElvUI Config"], L["Click to toggle config window"]) --Config
	CreateB(Rbutton, "R", "/rl", L["Reload UI"], L["Click to reload your interface"]) --Reload
	CreateB(Mbutton, "M", "/moveui", L["Move UI"], L["Click to unlock moving ElvUI elements"]) --Move UI
	CreateB(Bbutton, "B", nil, L["Boss Mod"], L["Click to toggle the Configuration/Option Window from the Bossmod (DXE, DBM or Bigwigs) you have enabled."])--Boss Mod
	CreateB(Abutton, "A", nil, L["AddOns Manager"], L["Click to toggle the AddOn Manager frame."])--Addon Manager
	if Fbutton then
		CreateB(Fbutton, "F", "/ifilger", "iFilger", L["Click to toggle iFilger's config UI"])
	end
end

local function Positioning()
	local db = E.db.sle.uibuttons
	for i = 1, 5 do
		ButtonTable[i]:ClearAllPoints()
	end
	if Fbutton then
		Fbutton:ClearAllPoints()
	end
	--position check
	if db.position == "uib_vert" then
		Cbutton:Point("TOP", UIBFrame, "TOP", 0, (E.PixelMode and -1 or -2))
		Rbutton:Point("TOP", Cbutton, "BOTTOM", 0, (E.PixelMode and -db.spacing or -(db.spacing+2)))
		Mbutton:Point("TOP", Rbutton, "BOTTOM", 0, (E.PixelMode and -db.spacing or -(db.spacing+2)))
		Bbutton:Point("TOP", Mbutton, "BOTTOM", 0, (E.PixelMode and -db.spacing or -(db.spacing+2)))
		Abutton:Point("TOP", Bbutton, "BOTTOM", 0, (E.PixelMode and -db.spacing or -(db.spacing+2)))
		if Fbutton then
			Fbutton:Point("TOP", Abutton, "BOTTOM", 0, (E.PixelMode and -db.spacing or -(db.spacing+2)))
		end
	else
		Cbutton:Point("LEFT", UIBFrame, "LEFT", (E.PixelMode and 1 or 2), 0)
		Rbutton:Point("LEFT", Cbutton, "RIGHT", (E.PixelMode and db.spacing or db.spacing+2), 0)
		Mbutton:Point("LEFT", Rbutton, "RIGHT", (E.PixelMode and db.spacing or db.spacing+2), 0)
		Bbutton:Point("LEFT", Mbutton, "RIGHT", (E.PixelMode and db.spacing or db.spacing+2), 0)
		Abutton:Point("LEFT", Bbutton, "RIGHT", (E.PixelMode and db.spacing or db.spacing+2), 0)
		if Fbutton then
			Fbutton:Point("LEFT", Abutton, "RIGHT", (E.PixelMode and db.spacing or db.spacing+2), 0)
		end
	end
end

local function MoverSize()
	local db = E.db.sle.uibuttons
	if db.position == "uib_vert" then
		UIBFrame:SetWidth(db.size + (E.PixelMode and 2 or 4))
		UIBFrame:SetHeight((db.size*NumBut)+((E.PixelMode and db.spacing or db.spacing+2)*(NumBut-1))+2)
	else
		UIBFrame:SetWidth((db.size*NumBut)+((E.PixelMode and db.spacing or db.spacing+2)*(NumBut-1))+2)
		UIBFrame:SetHeight(db.size + (E.PixelMode and 2 or 4))
	end
end

function UB:FrameSize()
	local db = E.db.sle.uibuttons
	MoverSize()

	for i = 1, 5 do
		ButtonTable[i]:Size(db.size)
	end
	if Fbutton then
		Fbutton:Size(db.size)
	end

	Positioning()
end

function UB:Toggle()
	if not E.db.sle.uibuttons.enable then
		UIBFrame:Hide()
	else
		UIBFrame:Show()
		UB:UpdateMouseOverSetting()
	end
end

function UB:Initialize()
	UB:FrameSize()
	CreateButtons()
	UB:Toggle()

	E.FrameLocks['UIBFrame'] = true

	E:CreateMover(UIBFrame, "UIBFrameMover", L["UI Buttons"], nil, nil, nil, "ALL,S&L,S&L MISC")
	MoverSize()
end