-- подключение api самого мода, чтобы видеть всякое типо связывания
local ZomboLewd = require("ZomboLewd/ZomboLewd")
local function onMastrubate(worldObjects, contextMenu, anim, player)
local opts = PZAPI.ModOptions:getOptions("ZomboLewdFramework")
local WornedItems = {}
    contextMenu.Client.AnimationHandler.Play(
        worldObjects,
        {player},
        anim,
        false,
        true,
        {                    
            Start = function(action)                        
                if opts:getOption("box_SUndress"):getValue() then
                    DebugLog.log("[Zombolewd] Try to undress...")                
                    WornedItems = ZomboLewd.ClothHandler.Undress(player)
                end
            end,
            Perform = function(action)
                if opts:getOption("box_SUndress"):getValue() then
                    DebugLog.log("[Zombolewd] Try to dressup...")     
                    ZomboLewd.ClothHandler.DressUp(player,WornedItems) 
                end    
            end
        }
    )
end

return function(ContextMenu, player, uiContext, worldObjects)
    -- Determine gender-based flags for animation lookup
    local isFemale           = player:isFemale()
    local primaryGenderFlag  = isFemale and 0 or 1
    local secondaryGenderFlag = isFemale and 1 or 0

    -- Retrieve all “Masturbation” animations available for this player
    local animations = ContextMenu.Client.AnimationUtils:getAnimations(
        1,
        primaryGenderFlag,
        secondaryGenderFlag,
        {"Mast"}
    )

    -- Add the main “Masturbate” option and attach a submenu
    local mainOption = uiContext:addOption(
        getText("ContextMenu_Masturbate"),
        worldObjects
    )
    local subMenu = ISContextMenu:getNew(uiContext)
    uiContext:addSubMenu(mainOption, subMenu)

    -- Populate the submenu with each specific animation action
    for _, anim in ipairs(animations) do
        local translationKey = string.format("ContextMenu_%s%s", anim.prefix, anim.id)
        local label          = getText(translationKey)

        if label then
            -- Prepare tooltip for this animation choice
            local tooltip = ISToolTip:new()
            tooltip:setName(label)
            tooltip.description = ""  -- TODO: Add detailed description here
            tooltip:initialise()
            tooltip:setVisible(false)

            -- Add the animation option with its click handler
            local opt = subMenu:addOption(label, worldobjects, onMastrubate, ContextMenu, anim, player)
            opt.toolTip = tooltip
        else
            -- Log missing localization keys for debugging
            DebugLog.log(string.format("Missing translation key: %s", translationKey))
        end
    end
end