RockyCoreWoWDeckHelper.ActionBar = {}

-- Action slot IDs are NOT sequential across the bars, and bars 6-8 sit in a
-- high range far past bar 5. `first` is the action slot of that bar's button 1.
RockyCoreWoWDeckHelper.BARS = {
    { bar = 1, first = 1,   binding = "ACTIONBUTTON"          },
    { bar = 2, first = 61,  binding = "MULTIACTIONBAR1BUTTON" },
    { bar = 3, first = 49,  binding = "MULTIACTIONBAR2BUTTON" },
    { bar = 4, first = 25,  binding = "MULTIACTIONBAR3BUTTON" },
    { bar = 5, first = 37,  binding = "MULTIACTIONBAR4BUTTON" },
    { bar = 6, first = 145, binding = "MULTIACTIONBAR5BUTTON" },
    { bar = 7, first = 157, binding = "MULTIACTIONBAR6BUTTON" },
    { bar = 8, first = 169, binding = "MULTIACTIONBAR7BUTTON" },
}

-- The GetItemInfo global is deprecated in favor of C_Item.GetItemInfo;
-- support both so the addon survives its removal
local GetItemInfoCompat = (C_Item and C_Item.GetItemInfo) or GetItemInfo

-- GetActionInfo reports this instead of a mount ID for the random-mount button
local RANDOM_FAVORITE_MOUNT = 268435455

-- Maps action slot number to its resolved data
function RockyCoreWoWDeckHelper.ActionBar:GetSlotData(actionSlot)
    -- HasAction returns nil if slot is empty
    if not HasAction(actionSlot) then
        return { empty = true }
    end

    local actionType, id, subType = GetActionInfo(actionSlot)
    -- actionType: "spell", "item", "macro", "companion", "flyout", "summonmount", etc.

    local icon = GetActionTexture(actionSlot)
    -- Returns the texture path string, e.g. "Interface\\Icons\\Spell_Fire_Flamebolt"
    -- or a numeric FileDataID in newer builds

    local name = ""
    local spellID = nil
    local itemID = nil

    if actionType == "spell" then
		if id then
			local spellInfo = C_Spell.GetSpellInfo(id)
			name = spellInfo and spellInfo.name or ""
			spellID = id
		end
	elseif actionType == "item" then
		if id then
			name = GetItemInfoCompat(id) or ""
			itemID = id
		end
	elseif actionType == "macro" then
		if id then
			name = GetMacroInfo(id) or ""
			-- Resolve the macro's underlying spell/item so the plugin can
			-- fetch a real icon by ID
			spellID = GetMacroSpell(id)
			if not spellID then
				local _, itemLink = GetMacroItem(id)
				if itemLink then
					itemID = tonumber(itemLink:match("item:(%d+)"))
				end
			end
		end
	elseif actionType == "summonmount" then
		if id == RANDOM_FAVORITE_MOUNT then
			-- Sentinel ID: no journal entry exists, so resolve it to the spell
			-- the button actually casts
			name = "Random Favorite Mount"
			spellID = 150544  -- Summon Random Favorite Mount
		elseif id then
			local mountName, mountSpellID = C_MountJournal.GetMountInfoByID(id)
			name = mountName or ""
			spellID = mountSpellID
		end
	end

    -- GetActionCount: returns count for items, charges for spells, 0 otherwise
    local count = GetActionCount(actionSlot)

    -- Cooldown data (not reliably pushable, but useful for initial state)
    local cdStart, cdDuration, cdEnable = GetActionCooldown(actionSlot)

    -- Keybinding
    local binding = self:GetBindingForSlot(actionSlot)

    -- IsUsableAction: returns usable, noMana
    local usable, noMana = IsUsableAction(actionSlot)

    return {
        slot = actionSlot,
        actionType = actionType or "unknown",
        id = id,
        subType = subType,
        spellID = spellID,
        itemID = itemID,
        name = name,
        icon = icon,      -- texture path or FileDataID
        count = count,
        binding = binding,
        usable = usable and 1 or 0,
        cdStart = cdStart,
        cdDuration = cdDuration,
    }
end

function RockyCoreWoWDeckHelper.ActionBar:GetBindingForSlot(actionSlot)
    local actionName
    for _, def in ipairs(RockyCoreWoWDeckHelper.BARS) do
        local offset = actionSlot - def.first
        if offset >= 0 and offset < RockyCoreWoWDeckHelper.SLOTS_PER_BAR then
            actionName = def.binding .. (offset + 1)
            break
        end
    end

    if not actionName then
        return nil  -- paging/stance slot with no binding name of its own
    end

    -- GetBindingKey returns up to 2 bindings; take the first
    local key1, key2 = GetBindingKey(actionName)
    return key1  -- e.g. "1", "SHIFT-F", "CTRL-2", nil if unbound
end

-- Paging and override detection
function RockyCoreWoWDeckHelper.ActionBar:GetEffectiveBarPage()
    -- GetActionBarPage() returns current page for bar 1 (1-6)
    -- When override/vehicle bar is active, bar 1 is replaced
    local page = GetActionBarPage()

    if HasOverrideActionBar() then
        return "override"
    elseif HasVehicleActionBar() then
        return "vehicle"
    elseif HasTempShapeshiftActionBar() then
        -- Druid bear/cat, etc.
        return "shapeshift"
    end

    return page  -- 1-6 for normal paging
end
