local mad = require "MoonAdditions"

-- Constants
local MENU_TITLE_SIZE = 1.40
local MENU_ITEM_HEIGHT = 17
local MENU_ITEM_WIDTH = 125
local MENU_WIDTH = 190
local MENU_ITEM_SPACING = 1  -- Additional space between items
local MENU_COLOR = {r = 153, g = 238, b = 153, a = 255}
local TEXT_COLOR = {r = 255, g = 255, b = 255, a = 255}
local CURSOR_HOVER_COLOR = {r = 255, g = 170, b = 255, a = 255}
local DESCRIPTION_COLOR = {r = 255, g = 230, b = 100, a = 255} -- Yellow color for descriptions
local DESCRIPTION_FONT_SIZE = 0.75  -- Smaller font size for descriptions

-- Static menu coordinates
local MENU_X = 45
local MENU_Y = 240

local currentMenu = "MainMenu"			-- Current active menu
local wasLMBDown = false				-- Track state of the left mouse button

-- Define each menu with items, actions, and descriptions
local menus = {
    MainMenu = {
        title = "Main Menu",
        items = {
            {label = "Positions", action = function() currentMenu = 'PosMenu'  printStringNow("Positions action triggered!", 500) end, description = "Configure camera positions"},
            {label = "Playback", action = function() printStringNow("Playback action triggered!", 500) end, description = "Play recorded audio"},
            {label = "Pause", action = function() printStringNow("Pause action triggered!", 500) end, description = "Pause current action"},
            {label = "Car slot", action = function() printStringNow("Car slot action triggered!", 500) end, description = "Manage car storage"},
            {label = "Save/Load", action = function() printStringNow("Save/Load action triggered!", 500) end, description = "Save or load settings"},
            {label = "Triggers", action = function() printStringNow("Triggers action triggered!", 500) end, description = "Configure trigger actions"},
            {label = "Options", action = function() printStringNow("Options action triggered!", 500) end, description = "Adjust settings"},
        }
    },
    PosMenu = {
        title = "Positions",
        items = {
            {label = "Action 1", action = function() printStringNow("Action 1 triggered!", 500) end, description = "Perform action 1"},
            {label = "Action 2", action = function() printStringNow("Action 2 triggered!", 500) end, description = "Perform action 2"},
			{label = "", action = function() end, description = ""},
			{label = "Back", action = function() currentMenu = 'MainMenu' printStringNow("Action 2 triggered!", 500) end, description = "go back"},

        }
    }
}


-- Draw a text with given attributes
function drawText(text, x, y, size, color, isHovered)
    local style = mad.font_style.FONT_SUBTITLES
    local width = size * 0.58
    local height = size * 1.02
    local align = mad.font_align.LEFT
    local wrap = MENU_WIDTH
    local proportional = true
    local justify = false  

    local textColor = isHovered and CURSOR_HOVER_COLOR or color
    mad.draw_text(text, x, y, style, width, height, align, wrap, proportional, justify, 
                  textColor.r, textColor.g, textColor.b, textColor.a, 
                  1, 0, 0, 0, 0, 255, 0, 0, 0, 0)
end

-- Check if a point is within a rectangular area
function isPointInArea(pointX, pointY, rectX, rectY, rectWidth, rectHeight)
    return pointX >= rectX and pointX <= rectX + rectWidth and
           pointY >= rectY and pointY <= rectY + rectHeight
end

-- Display and manage the current menu
function displayMenu(menu)
    local cursorX, cursorY = getCursorPos()
    local isLMBDown = isKeyDown(1)  -- Check if the left mouse button is pressed
    local currentDescription = ""

    drawText(menu.title, MENU_X, MENU_Y, MENU_TITLE_SIZE, MENU_COLOR, false)

    for i, item in ipairs(menu.items) do
        local itemY = MENU_Y + 10 + (MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * i
        local isHovered = isPointInArea(cursorX, cursorY, MENU_X, itemY, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT)

        -- Draw the menu item
        drawText(item.label, MENU_X, itemY, 1, TEXT_COLOR, isHovered)

        -- Update the current description based on the hovered item
        if isHovered then
            currentDescription = item.description
        end

        if isHovered and isLMBDown and not wasLMBDown then
            item.action()
        end
    end

    -- Draw the current description under the last menu item with smaller size
    local descriptionY = MENU_Y + 15 + (MENU_ITEM_HEIGHT + MENU_ITEM_SPACING) * (#menu.items + 1)
    drawText(currentDescription, MENU_X, descriptionY, DESCRIPTION_FONT_SIZE, DESCRIPTION_COLOR, false)

    wasLMBDown = isLMBDown  -- Update the LMB state
end

-- Main function
function main()
    while not isPlayerPlaying(PLAYER_HANDLE) do wait(100) end
    wait(100)

    while true do
        wait(0)
print(currentMenu)
        -- Display the current active menu
        local menu = menus[currentMenu]
        if menu then
            displayMenu(menu)
        end
		-- print(currentMenu)
    end
end

добавь в таблицу menus поле таблицам с разными меню помимо title и items что-то вроде parent,  чтобы при нажатии клавиши 2 (пкм) происходило переключении из текущего меню на parent меню текущего меню