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
local wasRMBDown = false                   -- Track state of the right mouse button

local textAnimation = "..."
local lastUpdateTime = os.time()
local updateInterval = 0.5

-- Define each menu with items, actions, descriptions, and parent menu
local menus = {
    MainMenu = {
        title = "Main Menu",
        parent = nil,  -- No parent for the 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",
        parent = "MainMenu",  -- Specify the parent menu
        items = {
			{label = "Cam Slot: " .. textAnimation, action = function() printStringNow("Action 1 triggered!", 500) end, description = "Perform action 0"},
			{label = "Preview", action = function() printStringNow("Action 1 triggered!", 500) end, description = "Perform action 0"},
            {label = "Save", action = function() printStringNow("Action 1 triggered!", 500) end, description = "Perform action 1"},
            {label = "Load", action = function() printStringNow("Action 2 triggered!", 500) end, description = "Perform action 2"},
            {label = "", action = function() end, description = ""},
            {label = "Back", action = function() currentMenu = 'MainMenu' printStringNow("Back action 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 isRMBDown = isKeyDown(2)  -- Check if the right 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)

    -- Switch to parent menu if right mouse button is pressed
    if isRMBDown and not wasRMBDown and menu.parent then
        currentMenu = menu.parent
        printStringNow("Switching to parent menu", 500)
    end

    wasLMBDown = isLMBDown  -- Update the LMB state
    wasRMBDown = isRMBDown  -- Update the RMB state
end

function updateText()
    local currentTime = os.time()

    if currentTime - lastUpdateTime >= updateInterval then
        if textAnimation == "..." then
            textAnimation = ".."
        elseif textAnimation == ".." then
            textAnimation = "."
        else
            textAnimation = "..."
        end

        print(textAnimation)

        lastUpdateTime = currentTime
    end
end


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

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







таблица с анимациями объявляется всего один раз, но что мне сделать, чтобы каждый раз когда она рендерится, значение в ней обновлялось?