--[[

	The MIT License (MIT)

	Copyright (c) 2026 Lars Norberg

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.

--]]
local _, ns = ...

-- Needed to forcefully make sure the binds are available during setup
LoadBindings(GetCurrentBindingSet())

local LFF = LibStub("LibFadingFrames-1.0")

local defaults = {
	enabled = true,

	layout = "grid", -- <grid, zigzag>
	layoutZigZagStart = 1, -- at which button the zigzag pattern should begin
	layoutZigZagOffset = 44/64, -- -- relative offset in the growth direction for the alternate zigzag row as a fraction of button size.
	layoutGridSize = NUM_ACTIONBAR_BUTTONS, -- when to start a new grid row
	layoutGrowth = "horizontal", -- which direction the bar initially grows in
	layoutGrowthHorizontal = "RIGHT", -- which direction the bar grows in horizontally
	layoutGrowthVertical = "DOWN", -- which direction the bar grows in vertically
	layoutPaddingX = 8, -- horizontal padding between the buttons
	layoutPaddingY = 8, -- vertical padding between the buttons

	enableBarFading = false, -- whether to enable non-combat/hover button fading
	fadeInCombat = false, -- whether to keep fading out even in combat
	fadeFrom = 1, -- which button to start the button fading from

	numbuttons = NUM_ACTIONBAR_BUTTONS, -- 12
	visibility = {
		dragon = false,
		possess = false,
		overridebar = false,
		vehicleui = false
	}
}

-- Return bindaction by blizzard barID.
local BINDTEMPLATE_BY_ID = {
	[1] = "ACTIONBUTTON%d",
	[BOTTOMLEFT_ACTIONBAR_PAGE] = "MULTIACTIONBAR1BUTTON%d",
	[BOTTOMRIGHT_ACTIONBAR_PAGE] = "MULTIACTIONBAR2BUTTON%d",
	[RIGHT_ACTIONBAR_PAGE] = "MULTIACTIONBAR3BUTTON%d",
	[LEFT_ACTIONBAR_PAGE] = "MULTIACTIONBAR4BUTTON%d",
	[MULTIBAR_5_ACTIONBAR_PAGE] = "MULTIACTIONBAR5BUTTON%d",
	[MULTIBAR_6_ACTIONBAR_PAGE] = "MULTIACTIONBAR6BUTTON%d",
	[MULTIBAR_7_ACTIONBAR_PAGE] = "MULTIACTIONBAR7BUTTON%d"
}

-- Return blizzard barID by from own bar numbers.
local BAR_TO_ID = {
	[1] = 1, 							-- primary action bar
	[2] = BOTTOMLEFT_ACTIONBAR_PAGE, 	-- multibar 1
	[3] = BOTTOMRIGHT_ACTIONBAR_PAGE, 	-- multibar 2
	[4] = RIGHT_ACTIONBAR_PAGE, 		-- multibar 3
	[5] = LEFT_ACTIONBAR_PAGE, 			-- multibar 4
	[6] = MULTIBAR_5_ACTIONBAR_PAGE, 	-- multibar 5
	[7] = MULTIBAR_6_ACTIONBAR_PAGE, 	-- multibar 6
	[8] = MULTIBAR_7_ACTIONBAR_PAGE 	-- multibar 7
}

-- Return our bar number from blizzard barID.
local ID_TO_BAR = {}
for i,j in next,BAR_TO_ID do ID_TO_BAR[j] = i end

local ActionBar = CreateFrame("Frame")
local ActionBar_MT = { __index = ActionBar }

ns.ActionBar = {}
ns.ActionBar.prototype = ActionBar
ns.ActionBar.defaults = defaults

ns.ActionBar.Create = function(self, barNum, config, name)

	local bar = setmetatable(CreateFrame("Frame", name, UIParent, "SecureHandlerStateTemplate"), ActionBar_MT)
	local id = BAR_TO_ID[barNum]

	bar.id = id
	bar.name = name or id
	bar.config = ns:Merge(config or ns:Copy(ns.ActionBar.defaults), ns.ActionBar.defaults) -- wth?

	bar.buttons = {}
	bar.buttonWidth = 64 -- why exactly are we storing it directly on the bar object?
	bar.buttonHeight = 64

	bar:SetAttribute("id", id)
	
	-- create buttons
	for i = 1,NUM_ACTIONBAR_BUTTONS do
		local button = ns.ActionButton:Create(i, name.."Button"..i, bar)
		button:SetHitRectInsets(-4, -4, -4, -4)

		bar.buttons[i] = button -- lua reference
		bar:SetFrameRef("Button"..i, button) -- secure environment reference

		local keyBoundTarget = string.format(BINDTEMPLATE_BY_ID[id], button.id)
		button.config.keyBoundTarget = keyBoundTarget
	end

	EventRegistry:RegisterCallback("HouseEditor.StateUpdated", function(_, state) bar:HousingStateChanged(state) end, bar)

	bar:SetAttribute("UpdateVisibility", [[
		local visibility = self:GetAttribute("visibility");
		local userhidden = self:GetAttribute("userhidden");
		if (visibility == "show") then
			if (userhidden) then
				self:Hide();
			else
				self:Show();
			end
		elseif (visibility == "hide") then
			self:Hide();
		end
	]])

	bar:SetAttribute("_onstate-vis", [[
		if (not newstate) then
			return
		end
		self:SetAttribute("visibility", newstate);
		self:RunAttribute("UpdateVisibility");
	]])

	bar:SetAttribute("_onstate-page", [[
		local hasVehicleBar, hasOverrideBar, hasTempShapeshiftBar, hasPossessBar, isDragonRiding;

		if (newstate == "possess" or newstate == "dragon" or newstate == "11") then
			if HasVehicleActionBar() then
				newstate = GetVehicleBarIndex();
				hasVehicleBar = true;

			elseif HasOverrideActionBar() then
				newstate = GetOverrideBarIndex();
				hasOverrideBar = true;

			elseif HasTempShapeshiftActionBar() then
				newstate = GetTempShapeshiftBarIndex();
				hasTempShapeshiftBar = true;

			elseif HasBonusActionBar() then
				newstate = GetBonusBarIndex();
				if (GetBonusBarOffset() == 5) then
					hasPossessBar = true;
					if (IsMounted() or IsFlying()) then
						isDragonRiding = true;
					end
				end
			else
				newstate = nil;
			end
			if (not newstate) then
				newstate = 12;
			end
		end

		self:SetAttribute("isdragonriding", isDragonRiding);
		self:SetAttribute("hasvehiclebar", hasVehicleBar);
		self:SetAttribute("hasoverridebar", hasOverrideBar);
		self:SetAttribute("hastempshapeshiftbar", hasTempShapeshiftBar);
		self:SetAttribute("haspossessbar", hasPossessBar);

		self:CallMethod("UpdateButtonFlags");

		self:SetAttribute("state", newstate);
		control:ChildUpdate("state", newstate);

		-- ugly fix for overridebuttons > 6 showing (these buttons are empty, overridebars only have 6 buttons)
		-- *still no fix for empty buttons on overridebars (verify this, I thought I fixed it?)
		if (self:GetAttribute("id") == 1) then 
			for i = 7,12 do
				local button = self:GetFrameRef("Button"..i);
				if (hasVehicleBar or hasOverrideBar or hasPossessBar) then -- confirm button numbers, might need to rewrite this.
					button:Hide();
				else
					button:Show();
				end
			end
		end

		self:CallMethod("UpdateFading");
	]])

	return bar
end

ActionBar.HousingStateChanged = function(self, state)
	self.InHousing = state
	if (not next(self.buttons)) then return end
	if (state) then
		ClearOverrideBindings(self)
	elseif (not state) then
		self:UpdateBindings()
	end
end

ActionBar.ForAll = function(self, method, ...)
	for id,button in next,self.buttons do
		if (type(method) == "string") then
			local func = button[method]
			if (func) then
				func(button, ...)
			end
		elseif (type(method) == "function") then
			method(button, ...)
		end
	end
end

ActionBar.Update = function(self)
	if (InCombatLockdown()) then return end

	self:UpdateButtonCount()
	self:UpdateButtonLayout()
	self:UpdateStateDriver()
	self:UpdateVisibilityDriver()
	self:UpdateBindings()
	self:UpdateFading()
end

ActionBar.UpdateFading = function(self)
	if (self.config.enabled and self.config.enableBarFading) then
		for id,button in next,self.buttons do

			-- remove any previous fade registrations
			LFF:UnregisterFrameForFading(button)

			-- register current fade for selected buttons
			if (id >= self.config.fadeFrom) then
				local button = self.buttons[id]
				if (button:GetTexture()) then
					LFF:RegisterFrameForFading(button, self.config.fadeAlone and self:GetName() or "actionbuttons", -4, -4, -4, -4)
				else
					-- update button?
				end
			end
		end
	else
		-- unregister all fading
		for id,button in next,self.buttons do
			LFF:UnregisterFrameForFading(button)

			-- whyever did I add this?
			if (not button:GetTexture()) then
				-- update button?
			end
		end
	end
end

ActionBar.UpdateButtonCount = function(self)
	if (InCombatLockdown()) then return end

	for id,button in next,self.buttons do
		if (id <= self.config.numbuttons) then
			button:Show()
			button:SetAttribute("statehidden", nil)
		else
			button:Hide()
			button:SetAttribute("statehidden", true)
		end
	end
end

ActionBar.UpdateButtonConfig = function(self)
	for id,button in next,self.buttons do
		button.config.clickOnDown = self.config.clickOnDown
	end
end

ActionBar.UpdateButtonLayout = function(self)
	if (InCombatLockdown()) then return end

	local buttons = self.buttons
	local numbuttons = self.numButtons or self.config.numbuttons or #buttons

	if (numbuttons == 0) then
		self:SetSize(self.buttonWidth, self.buttonHeight)
		return
	end

	local layout = self.config.layout

	if (layout == "zigzag") then

		self:SetSize(2,2) -- Just set a temporary size to avoid positioning bugs

		local config = self.config
		local buttonWidth = self.buttonWidth
		local buttonHeight = self.buttonHeight

		local counter = 0
		local left, right, top, bottom = 0, 0, 0, 0
		local point = (config.layoutGrowthVertical == "UP" and "BOTTOM" or "TOP")..(config.layoutGrowthHorizontal == "RIGHT" and "LEFT" or "RIGHT")
		local offsetX, offsetY

		for id,button in next,buttons do

			local isZigZag = (id >= config.layoutZigZagStart) and ((config.layoutZigZagStart - id)%2 == 0)

			if (config.layoutGrowth == "horizontal") then

				if (config.layoutGrowthHorizontal == "RIGHT") then
					offsetX = (buttonWidth + config.layoutPaddingX) * (counter - (isZigZag and 1 or 0)) + (isZigZag and (config.layoutZigZagOffset * buttonWidth) or 0)
					if (id <= numbuttons) then
						left = 0
						right = math.max(right, offsetX + buttonWidth)
					end

				elseif (config.layoutGrowthHorizontal == "LEFT") then
					offsetX = -((buttonWidth + config.layoutPaddingX) * (counter - (isZigZag and 1 or 0)) + (isZigZag and (config.layoutZigZagOffset * buttonWidth) or 0))
					if (id <= numbuttons) then
						left = math.min(left, offsetX - buttonWidth)
						right = 0
					end
				end

				if (config.layoutGrowthVertical == "UP") then
					offsetY = isZigZag and (buttonHeight + config.layoutPaddingY) or 0
					if (id <= numbuttons) then
						top = math.max(top, offsetY + buttonHeight)
						bottom = 0
					end

				elseif (config.layoutGrowthVertical == "DOWN") then
					offsetY = isZigZag and -(buttonHeight + config.layoutPaddingY) or 0
					if (id <= numbuttons) then
						top = 0
						bottom = math.min(bottom, offsetY - buttonHeight)
					end
				end


			elseif (config.layoutGrowth == "vertical") then

				if (config.layoutGrowthVertical == "DOWN") then
					offsetY = -((buttonHeight + config.layoutPaddingX) * counter + (isZigZag and (config.layoutZigZagOffset * buttonWidth) or 0))
					if (id <= numbuttons) then
						top = 0
						bottom = math.min(bottom, offsetY - buttonHeight)
					end

				elseif (config.layoutGrowthVertical == "UP") then
					offsetY = (buttonHeight + config.layoutPaddingX) * counter + (isZigZag and (config.layoutZigZagOffset * buttonWidth) or 0)
					if (id <= numbuttons) then
						top = math.max(top, offsetY + buttonHeight)
						bottom = 0
					end
				end

				if (config.layoutGrowthHorizontal == "RIGHT") then
					offsetX = isZigZag and (buttonWidth + config.layoutPaddingX) or 0
					if (id <= numbuttons) then
						left = 0
						right = math.max(right, offsetX + buttonWidth)
					end

				elseif (config.layoutGrowthHorizontal == "LEFT") then
					offsetX = isZigZag and -(buttonWidth + config.layoutPaddingX) or 0
					if (id <= numbuttons) then
						left = math.min(left, offsetX - buttonWidth)
						right = 0
					end
				end

			end

			if (not isZigZag) then
				counter = counter + 1
			end

			button:ClearAllPoints()
			button:SetPoint(point, offsetX, offsetY)
		end

		self:SetSize(math.abs(left - right), math.abs(top - bottom))

	elseif (layout == "grid") then

		local config = self.config

		local buttonWidth = self.buttonWidth
		local buttonHeight = self.buttonHeight

		local totalbreaks = math.ceil(config.numbuttons/config.layoutGridSize)
		local width, height

		if (config.layoutGrowth == "horizontal") then
			if (numbuttons < config.layoutGridSize) then
				width = buttonWidth*numbuttons + config.layoutPaddingX*(numbuttons - 1)
				height = buttonHeight
			else
				width = buttonWidth*config.layoutGridSize + config.layoutPaddingX*(config.layoutGridSize - 1)
				height = buttonHeight*totalbreaks + (config.layoutPaddingY or config.layoutPaddingX)*(totalbreaks-1)
			end

		elseif (config.layoutGrowth == "vertical") then
			if (numbuttons < config.layoutGridSize) then
				width = buttonWidth
				height = buttonHeight*numbuttons + config.layoutPaddingX*(numbuttons - 1)
			else
				width = buttonWidth*totalbreaks + (config.layoutPaddingY or config.layoutPaddingX)*(totalbreaks-1)
				height = buttonHeight*config.layoutGridSize + config.layoutPaddingX*(config.layoutGridSize - 1)
			end
		end

		self:SetSize(width, height)

		local point = (config.layoutGrowthVertical == "UP" and "BOTTOM" or "TOP")..(config.layoutGrowthHorizontal == "RIGHT" and "LEFT" or "RIGHT")
		local offsetX, offsetY = 0,0

		for id,button in next,buttons do

			local breakpoint = (id - 1)%config.layoutGridSize == 0
			local numbreaks = breakpoint and math.floor((id - 1)/config.layoutGridSize)

			if (config.layoutGrowth == "horizontal") then
				if (breakpoint) then
					offsetX = 0
					if (config.layoutGrowthVertical == "UP") then
						offsetY = (buttonHeight + (config.layoutPaddingY or config.layoutPaddingX)) * numbreaks
					elseif (config.layoutGrowthVertical == "DOWN") then
						offsetY = -(buttonHeight + (config.layoutPaddingY or config.layoutPaddingX)) * numbreaks
					end
				else
					if (config.layoutGrowthHorizontal == "RIGHT") then
						offsetX = offsetX + (buttonWidth + config.layoutPaddingX)
					elseif (config.layoutGrowthHorizontal == "LEFT") then
						offsetX = offsetX - (buttonWidth + config.layoutPaddingX)
					end
				end

			elseif (config.layoutGrowth == "vertical") then
				if (breakpoint) then
					if (config.layoutGrowthHorizontal == "RIGHT") then
						offsetX = (buttonWidth + (config.layoutPaddingY or config.layoutPaddingX)) * numbreaks
					elseif (config.layoutGrowthHorizontal == "LEFT") then
						offsetX = -(buttonWidth + (config.layoutPaddingY or config.layoutPaddingX)) * numbreaks
					end
					offsetY = 0
				else
					if (config.layoutGrowthVertical == "DOWN") then
						offsetY = offsetY - (buttonWidth + config.layoutPaddingX)
					elseif (config.layoutGrowthVertical == "UP") then
						offsetY = offsetY + (buttonWidth + config.layoutPaddingX)
					end
				end
			end

			button:ClearAllPoints()
			button:SetPoint(point, offsetX, offsetY)
		end
	end
end

ActionBar.UpdateButtonFlags = function(self)
	-- Retrieve button flags from the secure environment,
	-- apply them to the bar object,
	self.isDragonRiding = self:GetAttribute("isdragonriding")
	self.hasVehicleBar = self:GetAttribute("hasvehiclebar")
	self.hasOverrideBar = self:GetAttribute("hasoverridebar")
	self.hasTempShapeshiftBar = self:GetAttribute("hastempshapeshiftbar")
	self.hasPossessBar = self:GetAttribute("haspossessbar")

	-- Apply them to each button too
	for id,button in next,self.buttons do
		button.isDragonRiding = self.isDragonRiding
		button.hasVehicleBar = self.hasVehicleBar
		button.hasOverrideBar = self.hasOverrideBar
		button.hasTempShapeshiftBar = self.hasTempShapeshiftBar
		button.hasPossessBar = self.hasPossessBar
	end
end

-- Update the actual keybinds
local inReassignBindingsLockdown = false
ActionBar.UpdateBindings = function(self)
	if (InCombatLockdown()) or (self.InHousing) or (inReassignBindingsLockdown) then return end
	if (not next(self.buttons)) then return end

	inReassignBindingsLockdown = true

	ClearOverrideBindings(self)

	for id,button in pairs(self.buttons) do

		local keyBindName, realButtonName = button.config.keyBoundTarget, button:GetName()
		for keyNumber = 1, select("#", GetBindingKey(keyBindName)) do
			local key = select(keyNumber, GetBindingKey(keyBindName))
			if (key and key ~= "") then
				-- owner, isPriority, key, buttonName [, mouseClick]
				SetOverrideBindingClick(self, false, key, realButtonName--[[, "Keybind"]])
			end
		end

		local key = button:GetHotkey()
		if not key or key == "" or button.config.hideElements.hotkey then
			button.HotKey:SetText(RANGE_INDICATOR)
			button.HotKey:Hide()
		else
			button.HotKey:SetText(key)
			button.HotKey:Show()
		end
	end

	inReassignBindingsLockdown = false
end

ActionBar.UpdateStateDriver = function(self)
	if (InCombatLockdown()) then return end

	local statedriver

	if (self.id == 1) then
		statedriver = "[overridebar][possessbar][shapeshift]possess;[bonusbar:5]dragon;[form,noform]0;[bar:2]2;[bar:3]3;[bar:4]4;[bar:5]5;[bar:6]6;"

		local playerClass = UnitClassBase("player")
		if (playerClass == "DRUID") then
			statedriver = statedriver .. "[bonusbar:1]7;[bonusbar:2]8;[bonusbar:3]9;[bonusbar:4]10;"

		elseif (playerClass == "MONK") then
			statedriver = statedriver .. "[bonusbar:1]7;[bonusbar:2]8;[bonusbar:3]9;"

		elseif (playerClass == "ROGUE") then
			statedriver = statedriver .. "[bonusbar:1]7;"

		elseif (playerClass == "WARRIOR") then
			statedriver = statedriver .. "[bonusbar:1]7;[bonusbar:2]8;"
		end

		statedriver = statedriver .. "1"
	else
		statedriver = tostring(self.id)
	end

	UnregisterStateDriver(self, "page")
	self:SetAttribute("state-page", "0")
	RegisterStateDriver(self, "page", statedriver or "0")
end

ActionBar.UpdateVisibilityDriver = function(self)
	if (InCombatLockdown()) then return end

	local visdriver

	if (self.config.enabled) then
		visdriver = "[petbattle]hide;show"
	end

	UnregisterStateDriver(self, "vis")
	self:SetAttribute("state-vis", "0")
	RegisterStateDriver(self, "vis", visdriver or "hide")
end
