local addonName, addon = ...

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['VehicleMenuBar'] = 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['UIParent'] = nil
--table.wipe(UIPARENT_MANAGED_FRAME_POSITIONS);

--Action Bars
local playerClass, englishClass = UnitClass("player");

if englishClass == 'DEATHKNIGHT' then
	MainMenuBar:SetScale(0.77)
	MainMenuBar:ClearAllPoints()
	MainMenuBar:SetPoint('CENTER', UIParent, 'CENTER', -145, 0)
	MainMenuBar:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 58)
	
else
	MainMenuBar:SetScale(0.80)
	MainMenuBar:ClearAllPoints()
	MainMenuBar:SetPoint('CENTER', UIParent, 'CENTER', -122, 0)
	MainMenuBar:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 58)
	
end
addon:LockFrame(MainMenuBar)


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)
	ShapeshiftBarFrame:ClearAllPoints()
	ShapeshiftBarFrame:SetPoint('CENTER', UIParent, 'CENTER', -630, 0)
	ShapeshiftBarFrame:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 120)
	addon:LockFrame(PetActionBarFrame)
	addon:LockFrame(ShapeshiftBarFrame)
else
	PetActionBarFrame:ClearAllPoints()
	PetActionBarFrame:SetPoint('CENTER', UIParent, 'CENTER', -400, 0)
	PetActionBarFrame:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 120)
	addon:LockFrame(PetActionBarFrame)
	addon:LockFrame(ShapeshiftBarFrame)
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)
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)

UIPARENT_MANAGED_FRAME_POSITIONS['VehicleMenuBar'] = nil
VehicleMenuBarArtFrame:Hide()
local buttonborder, buttontexture
for i = 1, VEHICLE_MAX_ACTIONBUTTONS do
		buttonborder = _G["VehicleMenuBarActionButton"..i.."Border"]
		buttontexture = _G["VehicleMenuBarActionButton"..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_ENTERING_WORLD")
vehiclebar:RegisterEvent("UNIT_ENTERED_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:SetScript("OnEvent", function(self, event, ...)
	local arg1 = ...
	if event == "PLAYER_ENTERING_WORLD" then
		local button
		for i = 1, VEHICLE_MAX_ACTIONBUTTONS do
			button = _G["VehicleMenuBarActionButton"..i]
			button:ClearAllPoints()
			button:SetSize(30, 30) -- Adjust values here to adjust the vehicle bar buttons size
			button:SetParent(self)
			previous = _G["VehicleMenuBarActionButton"..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
		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")
		vehiclebar:SetAlpha(0)
	elseif ((event == "UNIT_ENTERED_VEHICLE") and arg1 == "player") then
		vehiclebar:SetAlpha(1)
	elseif ((event == "UNIT_EXITED_VEHICLE") and arg1 == "player") then
		vehiclebar:SetAlpha(0)
	end
end)