local activeSexScenes = {}
--- Zombie and bandits nondefeat options
local ListedPositions = { --- Add more if there will be more context menu options
    "Oral",
    "Intercourse",
    "Consent",
}
-- подключение api самого мода, чтобы видеть всякое типо связывания
local ZomboLewd = require("ZomboLewd/ZomboLewd")
--- Callback: Rape a subdued zombie
local function onRapeZombie(worldObjects, contextMenu, position, player, clickedZombie)
    DebugLog.log("[ZomboLewd] /ZLRapeZombie.lua/onRapeZombie")
    local opts = PZAPI.ModOptions:getOptions("ZomboLewdFramework")
    local requesterFemale = player:isFemale()
    local targetFemale    = clickedZombie:isFemale()
    local sPosition = ListedPositions[position]
    local WornedItems = {}
    -- Ensure zombie is ground-level and clear any previous states
    clickedZombie:setAlwaysKnockedDown(false)
    clickedZombie:setKnockedDown(false)
    clickedZombie:update()
    clickedZombie:getEmitter():stopAll()
    print("player isFemale =", requesterFemale, "player isFemale =", targetFemale)
    print("clickedZombie isFemale =", clickedZombie:isZombie(), "clickedZombie isFemale =", clickedZombie:isZombie())
    -- Determine number of male/female actors
    local maleCount, femaleCount
    if requesterFemale and targetFemale then
      maleCount, femaleCount = 0, 2    -- Lesbian
    elseif not requesterFemale and not targetFemale then
      maleCount, femaleCount = 2, 0    -- Gay
    else
      maleCount, femaleCount = 1, 1    -- Straight
    end
  
    -- Fetch random sex animation
    local availableAnimations = contextMenu.Client.AnimationUtils:getAnimations(2, maleCount, femaleCount, {sPosition})
    local randomIndex         = ZombRand(1, #availableAnimations + 1)
    local selectedAnimation   = availableAnimations[randomIndex]    
  
    local function cleanup(player, clickedZombie)
        activeSexScenes[player] = nil
        clickedZombie:setUseless(true)
        clickedZombie:setAlwaysKnockedDown(true)
        clickedZombie:setForceFakeDead(true)
        clickedZombie:setVariable("AnimInProgress", false)
    end

    -- Play with lifecycle callbacks
    contextMenu.Client.AnimationHandler.Play(
        nil,
        { player, clickedZombie },
        selectedAnimation,
        true,
        true,
        {
        Update = function(action)
          activeSexScenes[player].timeOut = 0
        end,
        WaitToStart = function(action)
          action.timeOut = (action.timeOut or 0) + 1
          if action.timeOut > 250 then
          action:forceStop()
          DebugLog.log("Force-stopping action due to timeout")
          end
        end,
        Start = function(action)
          activeSexScenes[player] = nil
          clickedZombie:setVariable("AnimInProgress", true)
          if opts:getOption("box_SUndress"):getValue() then
            DebugLog.log("[Zombolewd] /ZLRapeZombie.lua/ Try to undress...")                
            WornedItems = ZomboLewd.ClothHandler.Undress(player)
            DebugLog.log(string.format("WornedItems count: %d", #WornedItems))
          end
        end,
        Stop = function(action)
          DebugLog.log("Rape zombie stopped")
          cleanup(player, clickedZombie)        
        end,
        Perform = function(action)
          DebugLog.log("Rape zombie performed")
          if opts:getOption("box_SUndress"):getValue() then
            DebugLog.log("[Zombolewd] /ZLRapeZombie.lua/ Try to dressup...")     
            ZomboLewd.ClothHandler.DressUp(player,WornedItems) 
          end                
          cleanup(player, clickedZombie)      
        end
        }
    )

    -- Register active scene for player
    activeSexScenes[player] = {
        target   = zombie,
        timeOut  = 0,
        Callback = cleanup
    }
end

--- Main export: build context menu
return function(ContextMenuClass, character, clickedZombie, context, worldObjects)
    DebugLog.log("[ZomboLewd] ZlRapeZombie function")
    local sOption = context:addOption(getText("ContextMenu_Rape_zombie"), worldobjects)	
    local sSexSubMenu = ISContextMenu:getNew(context)	
    context:addSubMenu(sOption, sSexSubMenu)
    for iPosition = 1, #ListedPositions do	
      local text = getText(string.format("ContextMenu_AskForSex_%s", ListedPositions[iPosition]))
      if text then
        local animationOption = sSexSubMenu:addOption(text, worldobjects, onRapeZombie, ContextMenuClass, iPosition, character, clickedZombie)
      else
        DebugLog.log(string.format("Missing Text: %s", anim))
      end
    end
end