--========================================================
-- ItemInspectionUI - Context menu hook (VanillaPlus)
--========================================================

require "InspectUI/UI/Inspect_UI_VanillaPlus"

print("[IIW] Loaded Inspect_Context.lua from 42.12.0")

ItemInspectionUI = ItemInspectionUI or {}

local function firstInventoryItemFromArrayList(list)
    if not list or not instanceof(list, "ArrayList") or not list.size or not list.get then
        return nil
    end

    local okSize, size = pcall(function() return list:size() end)
    if not okSize or not size or size <= 0 then return nil end

    for i = 0, size - 1 do
        local okGet, entry = pcall(function() return list:get(i) end)
        if okGet and entry then
            if instanceof(entry, "InventoryItem") then
                return entry
            end
            if instanceof(entry, "ArrayList") then
                local nested = firstInventoryItemFromArrayList(entry)
                if nested then return nested end
            end
        end
    end

    return nil
end

-- Resuelve el InventoryItem real desde la tabla "items"
local function resolveItemFromContext(items)
    if not items then return nil end

    if instanceof(items, "InventoryItem") then
        return items
    end

    if instanceof(items, "ArrayList") then
        return firstInventoryItemFromArrayList(items)
    end

    if type(items) ~= "table" or #items == 0 then return nil end

    local entry = items[1]

    -- Caso 1: directamente un InventoryItem
    if instanceof(entry, "InventoryItem") then
        return entry
    end

    if instanceof(entry, "ArrayList") then
        return firstInventoryItemFromArrayList(entry)
    end

    -- Caso 2: tabla con campo .items (estándar del contexto de PZ)
    if type(entry) == "table" then
        local nestedItems = rawget(entry, "items")
        if instanceof(nestedItems, "ArrayList") then
            return firstInventoryItemFromArrayList(nestedItems)
        end
        if type(nestedItems) == "table" and #nestedItems > 0 then
            for _, v in ipairs(nestedItems) do
                if instanceof(v, "InventoryItem") then
                    return v
                end
            end
        end
    end

    return nil
end

local function OnFillInventoryObjectContextMenu(playerIndex, context, items)
    local player = getSpecificPlayer(playerIndex)
    if not player then return end

    local item = resolveItemFromContext(items)
    if not item then return end

    if not InspectUI_VanillaPlus or not InspectUI_VanillaPlus.open then
        return
    end

    -- Texto traducible (fallback a texto plano si falta la key)
    local optionText = getText("IGUI_ContextMenu_InspectItem") or "Inspect Item"

    -- Añadimos la opción
    local opt = context:addOption(optionText, item, function()
        local p = getSpecificPlayer(playerIndex)
        InspectUI_VanillaPlus.open(item, p)
    end)

    -- Forzar que nuestra opción quede la PRIMERA del menú
    if opt and context.options then
        local opts = context.options
        for i, o in ipairs(opts) do
            if o == opt then
                table.remove(opts, i)
                table.insert(opts, 1, opt)
                break
            end
        end
    end
end

Events.OnFillInventoryObjectContextMenu.Add(OnFillInventoryObjectContextMenu)
