local addonName, addon = ...

function addon:InitiateActionBars()
UIPARENT_MANAGED_FRAME_POSITIONS['MultiBarBottomLeft'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MultiBarBottomRight'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MultiBarLeft'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MultiBarRight'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MultiCastActionBarFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MULTICASTACTIONBAR_YPOS'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['PetActionBarFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['PETACTIONBAR_YPOS'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['PossessBarFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ShapeshiftBarFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['SHAPESHIFTBAR_YPOS'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ContainerFrame1'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ContainerFrame2'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ContainerFrame3'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ContainerFrame4'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['ContainerFrame5'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_X"] = nil
UIPARENT_MANAGED_FRAME_POSITIONS["CONTAINER_OFFSET_Y"] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['BonusActionBarFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['BONUSACTIONBAR_YPOS'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['VEHICLEMENUBAR_YPOS'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['TargetFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['MainMenuBar'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['Boss1TargetFrame'] = nil
UIPARENT_MANAGED_FRAME_POSITIONS["PlayerPowerBarAlt"] = nil
UIPARENT_MANAGED_FRAME_POSITIONS['VehicleMenuBar'] = nil
--UIPARENT_MANAGED_FRAME_POSITIONS['UIParent'] = nil -- this does bad things
--table.wipe(UIPARENT_MANAGED_FRAME_POSITIONS); -- this also does bad things
--MainMenuBarArtFrame:Hide()


   -- local hider = CreateFrame("Frame")
   -- hider:Hide()
   -- MainMenuBar:SetParent(hider)
   -- local f = CreateFrame("Frame","MyMainBar",UIParent)
   -- f:SetSize(50,50)
   -- f:SetPoint("CENTER",0,0)
   -- MainMenuBarArtFrame:SetParent(f)
    
   -- ActionButton1:ClearAllPoints()
    --ActionButton1:SetPoint("CENTER",f,"CENTER",0,0)

MainMenuBarArtFrame:EnableMouse(false)
MainMenuBar:EnableMouse(false)
--Action Bars
MainMenuBar:SetScale(0.80)
	MainMenuBar:ClearAllPoints()
	MainMenuBar:SetPoint('CENTER', UIParent, 'CENTER', -122, 0)
	MainMenuBar:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 58)


local playerClass, englishClass, somefunnynumWHO = UnitClass("player");


addon:LockFrame(MainMenuBar)
StanceBarFrame:ClearAllPoints()
StanceBarFrame:SetPoint('BOTTOMLEFT', MainMenuBar, 'TOPLEFT')
addon:LockFrame(StanceBarFrame)
--MainMenuBarArtFrame:ClearAllPoints()
--MainMenuBarArtFrame:SetPoint('LEFT', MainMenuBar, 'LEFT')
--MainMenuBarArtFrame:SetPoint('RIGHT', MainMenuBar, 'RIGHT')

MultiBarBottomLeft:ClearAllPoints()
MultiBarBottomLeft:SetPoint('TOPLEFT', MainMenuBar, 'BOTTOMLEFT', 10, -2)
addon:LockFrame(MultiBarBottomLeft)

MultiBarBottomRight:ClearAllPoints()
MultiBarBottomRight:SetPoint('CENTER', UIParent, 'CENTER', 360, 0)
MultiBarBottomRight:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 17)
addon:LockFrame(MultiBarBottomRight)

if englishClass == 'HUNTER' then
	PetActionBarFrame:ClearAllPoints()
	PetActionBarFrame:SetPoint('CENTER', UIParent, 'CENTER', -260, 0)
	PetActionBarFrame:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 120)
	
	addon:LockFrame(PetActionBarFrame)

else
	PetActionBarFrame:ClearAllPoints()
	PetActionBarFrame:SetPoint('CENTER', UIParent, 'CENTER', -400, 0)
	PetActionBarFrame:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 120)
	--addon:LockFrame(PetActionBarFrame)

end

for id = 2, 12 do
	_G['MultiBarLeftButton'..id]:ClearAllPoints()
	_G['MultiBarRightButton'..id]:ClearAllPoints()
end

local buttons = { MultiBarLeftButton2, MultiBarLeftButton3, MultiBarLeftButton4, MultiBarLeftButton5, MultiBarLeftButton6, MultiBarLeftButton7, MultiBarLeftButton8, MultiBarLeftButton9, MultiBarLeftButton10, MultiBarLeftButton11, MultiBarLeftButton12 }

local anchorFrame, anchorPoint = MultiBarLeftButton1, 'RIGHT'
for index = #buttons, 1, -1 do
	local button = buttons[index]
	button:ClearAllPoints()
	button:SetPoint('LEFT', anchorFrame, anchorPoint, 6, 0)
	addon:LockFrame(button)
	anchorFrame, anchorPoint = button, 'RIGHT'
end

local buttons = { MultiBarRightButton2, MultiBarRightButton3, MultiBarRightButton4, MultiBarRightButton5, MultiBarRightButton6, MultiBarRightButton7, MultiBarRightButton8, MultiBarRightButton9, MultiBarRightButton10, MultiBarRightButton11, MultiBarRightButton12 }

local anchorFrame, anchorPoint = MultiBarRightButton1, 'RIGHT'
for index = #buttons, 1, -1 do
	local button = buttons[index]
	button:ClearAllPoints()
	button:SetPoint('LEFT', anchorFrame, anchorPoint, 6, 0)
	addon:LockFrame(button)
	anchorFrame, anchorPoint = button, 'RIGHT'
end

MultiBarRight:ClearAllPoints()
MultiBarRight:SetPoint('CENTER', UIParent, 'CENTER', 123, 0)
MultiBarRight:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, -400)
--MultiBarRight:SetScale(.80) -- after patch 6.0.2 they must have fixed the funky scaleing within their code because it now appears correct without this line
addon:LockFrame(MultiBarRight)

MultiBarLeft:ClearAllPoints()
MultiBarLeft:SetPoint('BOTTOMLEFT', MultiBarRight, 'TOPLEFT', 0, -455)
MultiBarLeft:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, -100)
addon:LockFrame(MultiBarLeft)

-- Fix the loss of the MicroBar buttons by killing the below blizzard function
function VehicleMenuBar_MoveMicroButtons(skinName)

end

local vehicanchor = CreateFrame("Frame", "vehicanchor", UIParent, "SecureActionButtonTemplate")
vehicanchor:SetPoint('CENTER', UIParent, 'CENTER', -300, 0)
vehicanchor:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 0)
vehicanchor:SetHeight(86)
vehicanchor:SetWidth(200)



local buttonborder, buttontexture
for i = 1, 6 do
	buttonborder = _G["OverrideActionBarButton"..i.."Border"]
		buttontexture = _G["OverrideActionBarButton"..i.."NormalTexture"]
		buttonborder:ClearAllPoints()
		buttontexture:ClearAllPoints()
		buttonborder:SetPoint("RIGHT", 4000, 0)
		buttontexture:SetPoint("RIGHT", 4000, 0)
end

local vehiclebar = CreateFrame("Frame", "GUIVehicleBar", UIParent, "SecureActionButtonTemplate")
local exitbutton = CreateFrame("BUTTON", "Exit Button", vehiclebar, "SecureActionButtonTemplate")

vehiclebar:RegisterEvent("PLAYER_LOGIN")
vehiclebar:RegisterEvent("UNIT_ENTERED_VEHICLE")
vehiclebar:RegisterEvent("UNIT_ENTERING_VEHICLE")
vehiclebar:RegisterEvent("UNIT_EXITED_VEHICLE")
--The following is to stop taint when getting into and out of a vehicle in combat the issue however is that the hotkeys do not update.
--MainMenuBarArtFrame:UnregisterEvent("UNIT_ENTERING_VEHICLE");
--MainMenuBarArtFrame:UnregisterEvent("UNIT_ENTERED_VEHICLE");
--MainMenuBarArtFrame:UnregisterEvent("UNIT_EXITING_VEHICLE");
--MainMenuBarArtFrame:UnregisterEvent("UNIT_EXITED_VEHICLE");

-- Events that no longer need to be monitored:
--MainMenuBarArtFrame:UnregisterEvent('ACTIONBAR_PAGE_CHANGED')
vehiclebar:SetAlpha(0)
vehiclebar:SetScript("OnEvent", function(self, event, ...)
	local arg1 = ...
	local button
	if event == "PLAYER_LOGIN" then
		exitbutton:ClearAllPoints()
		exitbutton:SetSize(36, 36) -- Adjust values here to adjust the exit button size
		exitbutton:SetPoint("BOTTOMRIGHT", vehicanchor, "TOPRIGHT", 19, -1) 
		exitbutton:SetScript("OnClick", function() VehicleExit() end)
		exitbutton:SetNormalTexture("Interface\\Vehicles\\UI-Vehicles-Button-Exit-Up")
		exitbutton:SetPushedTexture("Interface\\Vehicles\\UI-Vehicles-Button-Exit-Down")
	elseif ((event == "UNIT_ENTERED_VEHICLE") and arg1 == "player") then
		for i = 1, NUM_OVERRIDE_BUTTONS do
			button = _G["OverrideActionBarButton"..i]
			button:ClearAllPoints()
			button:SetSize(30, 30) -- Adjust values here to adjust the vehicle bar buttons size
			--button:SetParent(self)
			previous = _G["OverrideActionBarButton"..i-1]
			if i == 1 then
				button:SetPoint("BOTTOMLEFT", vehicanchor, "TOPLEFT", -4, 4) 
			else
				button:SetPoint("TOPLEFT", previous, "TOPRIGHT", 2, 0) -- You can adjust the X value here to increase or decrease the spacing between the vehicle bar buttons.
			end
		end
		vehiclebar:SetAlpha(1)



	elseif ((event == "UNIT_EXITED_VEHICLE") and arg1 == "player") then
		vehiclebar:SetAlpha(0)
		--for i = 1, NUM_OVERRIDE_BUTTONS do
			--button = _G["OverrideActionBarButton"..i]
			--button:ClearAllPoints()
			--button:SetSize(30, 30)
			--button:SetParent(OverrideActionBar)

		--end
		--OverrideActionBarButton1:SetPoint("BOTTOMLEFT", vehicanchor, "TOPLEFT", -4, 4) 
	elseif event == "UNIT_ENTERING_VEHICLE" then
		--OverrideActionBar:Hide()
	end
end)
--OverrideActionBar:ClearAllPoints()
--OverrideActionBar:SetPoint('TOP', UIParent, 'BOTTOM', 0, -150)
-- MainMenuBarArtFrame Fix

end

addon.RegisterEvent("ActionBars-Initiate", 'PLAYER_LOGIN', function(self, event)
	addon.UnregisterEvent(self, event)
	if addon.settings.InitiateActionBars == true then
		addon:InitiateActionBars()
	end
end)