local bAfterCombatGrid = false;
local bAfterCombatBar = false;
local rightContainer = CreateFrame("Frame", "CABRightContainer", MainMenuBar)
rightContainer.animation = rightContainer:CreateAnimationGroup()
local translation = rightContainer.animation:CreateAnimation("Translation")
translation:SetOffset(200,0)
translation:SetDuration(0.7)
rightContainer.animation2 = rightContainer:CreateAnimationGroup()
translation = rightContainer.animation2:CreateAnimation("Translation")
translation:SetOffset(0,180)
translation:SetDuration(0.4)
--CABRightRight
local rightRight = CreateFrame("Frame", "CABRightRight", rightContainer, "VerticalMultiBar3")
rightRight:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", 2.0, 98)
rightRight:SetSize(41, 503)
rightRight:Show()
--CABRightLeft
local rightLeft = CreateFrame("Frame", "CABRightLeft", rightContainer, "VerticalMultiBar4")
rightLeft:SetPoint("TOPRIGHT", rightRight, "TOPLEFT", -2.0, 0)
rightLeft:SetSize(41, 503)
rightLeft:Show()

-- RegisterStateDriver(MultiBarLeft, "visibility", "hide");
-- RegisterStateDriver(MultiBarRight, "visibility", "hide");
for i = 1, 12 do
	local button = _G["MultiBarLeftButton" .. i]
	button:EnableMouse(false);
	_G["MultiBarLeftButton" .. i .. "Cooldown"]:SetDrawBling(false);
	button = _G["MultiBarRightButton" .. i]
	button:EnableMouse(false);
	_G["MultiBarRightButton" .. i .. "Cooldown"]:SetDrawBling(false);
end
MultiBarLeft:EnableMouse(false)
MultiBarRight:EnableMouse(false)
MultiBarLeft:SetAlpha(0.0)
MultiBarRight:SetAlpha(0.0)

local function CAB_RestoreAlpha()
	rightContainer:SetAlpha(1.0)
end

hooksecurefunc("BeginActionBarTransition", function(bar, animIn)
	if bar == MainMenuBar then
		if animIn then
			rightContainer:SetAlpha(0.0)
			C_Timer.After(0.1, CAB_RestoreAlpha)
		end
		rightContainer.animation:Play(animIn);
		rightContainer.animation2:Play(animIn);
	end
end)

local function CAB_ToggleBarVisibility()
	if SHOW_MULTI_ACTIONBAR_3 then
		rightRight:Show()
	else
		rightRight:Hide()
	end
	
	if SHOW_MULTI_ACTIONBAR_3 and SHOW_MULTI_ACTIONBAR_4 then
		rightLeft:Show()
	else
		rightLeft:Hide()
	end
end

hooksecurefunc("MultiActionBar_Update", function(bar, animIn)
	if InCombatLockdown() then
		bAfterCombatBar = true;
	else
		CAB_ToggleBarVisibility()
	end
end)

local function CAB_ShowGrid(button)
	assert(button);
	button:SetAttribute("showgrid", button:GetAttribute("showgrid") + 1);

	if ( button.NormalTexture ) then
		button.NormalTexture:SetVertexColor(1.0, 1.0, 1.0, 0.5);
	end

	if ( button:GetAttribute("showgrid") >= 1 and not button:GetAttribute("statehidden") ) then
		button:Show();
	end
end

local function CAB_HideGrid(button)
	assert(button);
	local showgrid = button:GetAttribute("showgrid");

	if ( showgrid > 0 ) then
		button:SetAttribute("showgrid", showgrid - 1);
	end

	if ( button:GetAttribute("showgrid") == 0 and not HasAction(button.action) ) then
		button:Hide();
	end
end

local function CAB_UpdateGrid (barName, show)
	for i = 1, 12 do
		local button = _G[barName.."Button"..i];
		if ( show and not button.noGrid) then
			CAB_ShowGrid(button);
		else
			CAB_HideGrid(button);
		end
	end
end

local function CAB_ShowAllGrids()
	CAB_UpdateGrid("CABRightRight", true)
	CAB_UpdateGrid("CABRightLeft", true)
end

local function CAB_HideAllGrids()
	CAB_UpdateGrid("CABRightRight", false)
	CAB_UpdateGrid("CABRightLeft", false)
end

local function CAB_UpdateGridVisibility()
	if ( ALWAYS_SHOW_MULTIBARS == "1" or ALWAYS_SHOW_MULTIBARS == 1 ) then
		CAB_ShowAllGrids();
	else
		CAB_HideAllGrids();
	end
end

hooksecurefunc("MultiActionBar_UpdateGridVisibility", function(bar, animIn)
	if InCombatLockdown() then
		bAfterCombatGrid = true;
	else
		CAB_UpdateGridVisibility();
	end
end)

rightContainer:RegisterEvent("PLAYER_REGEN_ENABLED")
rightContainer:SetScript("OnEvent",function(event, ...)
	if bAfterCombatGrid == true then
		CAB_UpdateGridVisibility();
		bAfterCombatGrid = false;
	end
	
	if bAfterCombatBar == true then
		CAB_ToggleBarVisibility()
		bAfterCombatBar = false;
	end
end)

local function CAB_ActionButtonDown(bar, id)
	local button = _G[bar.."Button"..id];
	if ( button:GetButtonState() == "NORMAL" ) then
		button:SetButtonState("PUSHED");
	end
end

local function CAB_ActionButtonUp(bar, id)
	local button = _G[bar.."Button"..id];
	if ( button:GetButtonState() == "PUSHED" ) then
		button:SetButtonState("NORMAL");
	end
end

hooksecurefunc("MultiActionButtonDown", function(bar, id)
	if bar == "MultiBarLeft" then
		CAB_ActionButtonDown("CABRightLeft", id)
	end
	
	if bar == "MultiBarRight" then
		CAB_ActionButtonDown("CABRightRight", id)
	end
end)

hooksecurefunc("MultiActionButtonUp", function(bar, id)
	if bar == "MultiBarLeft" then
		CAB_ActionButtonUp("CABRightLeft", id)
	end
	
	if bar == "MultiBarRight" then
		CAB_ActionButtonUp("CABRightRight", id)
	end
end)