local RCFrameTex = {}
local FloatingBG = {}
local Buttons = {}
local maxLevelBar = {}
local offset2
local mainFrame = CreateFrame("Frame", "CABMainFrame", UIParent)
local offFrame = CreateFrame("Frame", "CABOffFrame", MainMenuBarArtFrame)
local petFrame = CreateFrame("Frame", "CABPetFrame", UIParent)
local bAfterCombatPetFrame = false;
local bAfterCombatCmd = false;

local function CreateTextureFrames()
	for i = 0, 3 do
		RCFrameTex[i] = CreateFrame("Frame", "CABBarTexture" .. i, offFrame)
		RCFrameTex[i]:SetSize(256.0, 43.0)
		RCFrameTex[i].texture = RCFrameTex[i]:CreateTexture("CABBarTextureSub" .. i, "ARTWORK")
		RCFrameTex[i].texture:SetAllPoints()
		RCFrameTex[i].texture:SetTexture("Interface\\AddOns\\ClassicActionBars\\UI-MainMenuBar-Dwarf.blp")
		RCFrameTex[i]:Show()

		maxLevelBar[i] = maxLevelBar[4]:CreateTexture("CABMaxLevelBar" .. i, BACKGROUND)
		maxLevelBar[i]:SetAllPoints()
		maxLevelBar[i]:SetTexture("Interface\\AddOns\\ClassicActionBars\\UI-MainMenuBar-MaxLevel.blp")
		maxLevelBar[i]:SetSize(256, 7)
	end
end

for i = 1, 6 do
	local button = _G["MultiBarBottomRightButton" .. i]
	button.noGrid = nil;
	local texture = button:CreateTexture("MultiBarBottomRightButton" .. i .. "FloatingBG", "BACKGROUND", nil, -1)
	texture:SetTexture("Interface\\Buttons\\UI-Quickslot.blp")
	texture:SetAlpha(0.4)
	texture:SetPoint("TOPLEFT", button, "TOPLEFT", -15, 15)
	texture:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", 15, -15)
end

mainFrame:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 0)
mainFrame:SetSize(1024.0, 53.0)
offFrame:SetPoint("TOPLEFT", mainFrame, "TOPLEFT" , 0.0, 0.0)
offFrame:SetSize(1024.0, 53.0)
offFrame:SetFrameLevel(1)
maxLevelBar[4] = CreateFrame("Frame", "CABMaxLevelBar", offFrame)
maxLevelBar[4]:SetSize(1024.0, 7.0)
maxLevelBar[4]:SetPoint("TOP", mainFrame, "TOP" , 0.0, -4.0)

CreateTextureFrames()
RCFrameTex[0]:SetPoint("BOTTOM", offFrame, "BOTTOM", -384.0, 0.0)
RCFrameTex[0].texture:SetTexCoord(0.0, 1.0, 0.83203125, 1.0)
RCFrameTex[1]:SetPoint("BOTTOM", offFrame, "BOTTOM", -128.0, 0.0)
RCFrameTex[1].texture:SetTexCoord(0.0, 1.0, 0.58203125, 0.75)
RCFrameTex[2]:SetPoint("BOTTOM", offFrame, "BOTTOM", 128.0, 0.0)
RCFrameTex[2].texture:SetTexCoord(0.0, 1.0, 0.33203125, 0.5)
RCFrameTex[3]:SetPoint("BOTTOM", offFrame, "BOTTOM", 384.0, 0.0)
RCFrameTex[3].texture:SetTexCoord(0.0, 1.0, 0.08203125, 0.25)

maxLevelBar[0]:SetPoint("BOTTOM", maxLevelBar[4], "TOP", -384.0, 0.0)
maxLevelBar[0]:SetTexCoord(0, 1.0, 0, 0.21875)
maxLevelBar[1]:SetPoint("LEFT", maxLevelBar[0], "RIGHT")
maxLevelBar[1]:SetTexCoord(0, 1.0, 0.25, 0.46875)
maxLevelBar[2]:SetPoint("LEFT", maxLevelBar[1], "RIGHT")
maxLevelBar[2]:SetTexCoord(0, 1.0, 0.5, 0.71875)
maxLevelBar[3]:SetPoint("LEFT", maxLevelBar[2], "RIGHT")
maxLevelBar[3]:SetTexCoord(0, 1.0, 0.75, 0.96875)

local function ForceSetPoint(frame, p1, to, p2, x, y)
	frame:SetMovable(true);
	frame:ClearAllPoints();
	frame:SetPoint(p1, to, p2, x, y);
	frame:SetUserPlaced(true);
	frame:SetMovable(false);
end

local function PetFrameFix()
	if InCombatLockdown() then
		bAfterCombatPetFrame = true
	else
		petFrame:SetSize(509, 43)
		local a, b, c, d, e
		local kids = {PetActionBarFrame:GetChildren()}
		for _, child in ipairs(kids) do
			a, b, c, d, e = child:GetPoint()
			if(b == PetActionBarFrame) then
				child:ClearAllPoints()
				child:SetPoint(a, petFrame, c, d, e)
			end
		end
		a, b, c, d, e = SlidingActionBarTexture0:GetPoint()
		SlidingActionBarTexture0:ClearAllPoints()
		SlidingActionBarTexture0:SetPoint(a, petFrame, c, d, e)
	end
end

local function OnFirstConnect_Post()
	PetFrameFix()
	if IsAddOnLoaded("Titan") then
		hooksecurefunc("TitanPanel_AdjustFrames", function(...)
			if InCombatLockdown() then return end
			local titanOffset = 0
			if IsAddOnLoaded("Titan") then
				if not TitanPanelGetVar("AuxScreenAdjust") then
					local titanBarCount = 0
					
					if TitanPanelGetVar("AuxBar2_Show") then
						titanBarCount = 2
					elseif TitanPanelGetVar("AuxBar_Show") then
						titanBarCount = 1
					end
					
					local titanScale = TitanPanelGetVar("Scale")
					
					if titanBarCount ~= 0 and titanScale then
						titanOffset = (TITAN_PANEL_BAR_HEIGHT * titanScale) * (titanBarCount);
					end
				end
			end
			mainFrame:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 0 + titanOffset)
		end)
	end
end

mainFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
mainFrame:SetScript("OnEvent",function(event, ...)
	C_Timer.After(1, OnFirstConnect_Post)
end)

hooksecurefunc(StatusTrackingBarManager, "LayoutBar", function(self, bar, barWidth, isTopBar, isDouble)
	if not self.largeSize then
		if (isDouble) then
				self.SingleBarSmallUpper:Hide();
				self.SingleBarLargeUpper:Show();
				self.SingleBarSmall:Hide();
				self.SingleBarLarge:Show();
		else
			self.SingleBarSmall:Hide();
			self.SingleBarLarge:Show(); 
		end
	end
	bar:ClearAllPoints()
	if (isDouble) then
		self.SingleBarLargeUpper:SetSize(1024, 8);
		self.SingleBarLargeUpper:SetPoint("CENTER", bar, 0, 0);
		self.SingleBarLarge:SetSize(1024, 11); 
		self.SingleBarLarge:SetPoint("CENTER", bar, 0, -9);
		if (isTopBar) then
			bar:SetPoint("TOP", mainFrame, "TOP", 0, 7)
			bar.StatusBar:SetSize(1024, 7.0);
			bar.StatusBar:SetPoint("RIGHT", bar.StatusBar:GetParent(), "RIGHT", 0, 0);
			bar:SetSize(1024, 8);
			bar.OverlayFrame:SetPoint("TOPLEFT", bar, "TOPLEFT", 0, -1)
		else
			bar:SetPoint("TOP", mainFrame, "TOP", 0, -1)
			bar.StatusBar:SetSize(1024, 10.0);
			bar.StatusBar:SetPoint("RIGHT", bar.StatusBar:GetParent(), "RIGHT", 0, 0);
			bar:SetSize(1024, 11);
			bar.OverlayFrame:SetPoint("TOPLEFT", bar, "TOPLEFT", 0, -3)
		end
		bar.StatusBar.Background:SetAlpha(0.5)
	else 
		self.SingleBarLarge:SetSize(1024, 11); 
		self.SingleBarLarge:SetPoint("CENTER", bar, 0, 0); -- 4

		bar:SetPoint("TOP", mainFrame, "TOP", 0, -0.5)-- -3
		bar.StatusBar:SetSize(1024, 11.0);
		bar.StatusBar:SetPoint("RIGHT", bar.StatusBar:GetParent(), "RIGHT", 0, 0);
		bar:SetSize(1024, 11);
		bar.OverlayFrame:SetPoint("TOPLEFT", bar, "TOPLEFT", 0, -2) --0, -4
		
		bar.StatusBar.Background:SetAlpha(0.5)
	end
end)

local function CAB_BarFix()
	MainMenuBarArtFrame.LeftEndCap:ClearAllPoints()
	MainMenuBarArtFrame.LeftEndCap:SetPoint("BOTTOM", offFrame, "BOTTOM", -544.0, 0)
	MainMenuBarArtFrame.RightEndCap:ClearAllPoints()
	MainMenuBarArtFrame.RightEndCap:SetPoint("BOTTOM", offFrame, "BOTTOM", 544.0, 0)
	MainMenuBarArtFrame.PageNumber:ClearAllPoints()
	MainMenuBarArtFrame.PageNumber:SetPoint("CENTER", offFrame, "CENTER", 30.0, -5.0)
	
	local offset, extraHeight
	
	if (StatusTrackingBarManager:GetNumberVisibleBars() == 2) then 
		extraHeight = StatusTrackingBarManager.SingleBarLarge:GetHeight() + StatusTrackingBarManager.SingleBarLargeUpper:GetHeight()
		offset = -16.0
		offset2 = 22
		maxLevelBar[4]:Hide()
	elseif (StatusTrackingBarManager:GetNumberVisibleBars() == 1) then
		extraHeight = StatusTrackingBarManager:GetHeight()
		offset = -8.0
		offset2 = 13
		maxLevelBar[4]:Hide()
	else 
		extraHeight = 0
		offset = 0
		offset2 = 8
		maxLevelBar[4]:Show()
	end
	
	if IsNormalActionBarState() then
		ForceSetPoint(CharacterMicroButton, "BOTTOMLEFT", offFrame, "BOTTOMLEFT", 556.0, 2)
		MainMenuBarPerformanceBar:SetPoint("CENTER", MainMenuMicroButton, "CENTER", 0, 12)
		HelpOpenTicketButton:SetPoint("CENTER", MainMenuMicroButton, "TOPRIGHT", -3, -5);
		HelpOpenWebTicketButton:SetPoint("CENTER", MainMenuMicroButton, "TOPRIGHT", -3, -5);
	end
	
	if InCombatLockdown() then return end
	
	-- titan panel check
	local titanOffset = 0
	if IsAddOnLoaded("Titan") then
		if not TitanPanelGetVar("AuxScreenAdjust") then
			local titanBarCount = 0
			
			if TitanPanelGetVar("AuxBar2_Show") then
				titanBarCount = 2
			elseif TitanPanelGetVar("AuxBar_Show") then
				titanBarCount = 1
			end
			
			local titanScale = TitanPanelGetVar("Scale")
			
			if titanBarCount ~= 0 and titanScale then
				titanOffset = (TITAN_PANEL_BAR_HEIGHT * titanScale) * (titanBarCount);
			end
		end
	end

	mainFrame:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 0 + titanOffset)

	ForceSetPoint(offFrame, "TOPLEFT", mainFrame, "TOPLEFT" , offset + extraHeight)
	MainMenuBarArtFrame:SetSize(1024.0, 53.0)
	MainMenuBarArtFrameBackground:Hide()

	-- bags & micro bars
	ForceSetPoint(MainMenuBarBackpackButton, "BOTTOMRIGHT", offFrame, "BOTTOMRIGHT", -4.0, 6.0)
	MainMenuBarBackpackButton:SetSize(CharacterBag3Slot:GetSize())
	ForceSetPoint(CharacterBag0Slot, "RIGHT", MainMenuBarBackpackButton, "LEFT", -2.0, 0.0)
	MicroButtonAndBagsBar.MicroBagBar:Hide()
	MainMenuBarBackpackButtonNormalTexture:SetSize(50, 50)
	MainMenuBarBackpackButtonCount:SetPoint("CENTER", MainMenuBarBackpackButton, "CENTER", 1.00, -7.00)

	--action bars
	ForceSetPoint(ActionButton1, "BOTTOMLEFT", offFrame, "BOTTOMLEFT", 8.0, 4.0)
	ForceSetPoint(MultiBarBottomLeft, "BOTTOMLEFT", ActionButton1, "TOPLEFT", 0.0, offset2 + 4)
	ForceSetPoint(MultiBarBottomRight, "LEFT" ,MultiBarBottomLeft, "RIGHT", 10.0, 0.0)
	ForceSetPoint(MultiBarBottomRightButton7, "LEFT",MultiBarBottomRightButton6,"RIGHT",6.0, 0.0)

	--Stance & pet bar
	if MultiBarBottomLeft:IsShown() then
		ForceSetPoint(StanceBarFrame, "BOTTOMLEFT", mainFrame, "TOPLEFT", 29.0, 32.0 + offset2)
		if StanceBarFrame:IsShown() then
			ForceSetPoint(petFrame, "TOPLEFT", mainFrame, "BOTTOMLEFT", 500.0, 129.0 + offset2)
		else
			ForceSetPoint(petFrame, "TOPLEFT", mainFrame, "BOTTOMLEFT", 36.0, 129.0 + offset2)
		end
	else
		ForceSetPoint(StanceBarFrame, "BOTTOMLEFT", mainFrame, "TOPLEFT", 29.0, -13.0 + offset2)
		if StanceBarFrame:IsShown() then
			ForceSetPoint(petFrame, "TOPLEFT", mainFrame, "BOTTOMLEFT", 36.0, 129.0 + offset2)
		else
			ForceSetPoint(petFrame, "TOPLEFT", mainFrame, "BOTTOMLEFT", 36.0, 84.0 + offset2)
		end
	end
end

hooksecurefunc(MainMenuBar, "SetPositionForStatusBars", function()
	--MainMenuBarArtFrame.LeftEndCap:Hide()
	--MainMenuBarArtFrame.RightEndCap:Hide()
	CAB_BarFix()
	-- ForceSetPoint(MultiBarLeft, "TOPRIGHT", MultiBarRight, "TOPLEFT", -2.0, 0)
	-- ForceSetPoint(MultiBarRight, "BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", 2.0, 98)
end)

hooksecurefunc("MoveMicroButtons", function(anchor, anchorTo, relAnchor, x, y, isStacked)
	if ( isStacked == false ) then
		ForceSetPoint(CharacterMicroButton, "BOTTOMLEFT", offFrame, "BOTTOMLEFT", 556.0, 2)
		MainMenuBarPerformanceBar:SetPoint("CENTER", MainMenuMicroButton, "CENTER", 0, 12)
	end
end)

petFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
petFrame:SetScript("OnEvent",function(event, ...)
	if bAfterCombatPetFrame == true then
		PetFrameFix();
		bAfterCombatPetFrame = false;
	end
	CAB_BarFix()
end)

hooksecurefunc(ExpBarMixin, "Update", function(self)
	local currXP = UnitXP("player");
	local nextXP = UnitXPMax("player");
	self:SetBarText(XP_STATUS_BAR_TEXT:format(currXP, nextXP)); 
end)

hooksecurefunc(HonorBarMixin, "OnShow", function(self)
	self:UpdateTextVisibility();
end)

hooksecurefunc(ArtifactBarMixin, "Update", function(self)
	self.StatusBar.Overlay:Hide()
	self:UpdateOverlayFrameText()
end)