--- Callback: Ask for sex between two survivors
local ListedPositions = { --- Add more if there will be more context menu options
    "Oral",
    "Intercourse",
    "Consent",
}
-- подключение api самого мода, чтобы видеть всякое типо связывания
local ZomboLewd = require("ZomboLewd/ZomboLewd")

local function onAskForSex(worldObjects, contextMenu, position, requester, target)
  DebugLog.log("[ZomboLewd] /onAskForSex/")
  local opts = PZAPI.ModOptions:getOptions("ZomboLewdFramework")
  local requesterFemale = requester:isFemale()
  local targetFemale    = target:isFemale()
  local sPosition = ListedPositions[position]
  print("Requester isFemale =", requesterFemale, "Target isFemale =", targetFemale)
  print("Requester isFemale =", requester:isZombie(), "Target isFemale =", target: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 WornedItems = {}

  contextMenu.Client.AnimationHandler.Play(
    worldObjects,
    { requester, target },
    selectedAnimation,
    false,
    true,
    {                    
        Start = function(action)                        
            if opts:getOption("box_SUndress"):getValue() then
                DebugLog.log("[Zombolewd] /ZLAskForSex.lua/ Try to undress...")                
                WornedItems = ZomboLewd.ClothHandler.Undress(requester)
            end
        end,
        Perform = function(action)
            if opts:getOption("box_SUndress"):getValue() then
                DebugLog.log("[Zombolewd] /ZLAskForSex.lua/ Try to dressup...")     
                ZomboLewd.ClothHandler.DressUp(requester,WornedItems) 
            end    
        end
    }
  )
end

--- Main export: build context menu
return function(ContextMenuClass, character, clickedPlr, context, worldObjects)
  DebugLog.log("[ZomboLewd] ZLAskForSex function")

  local opts = PZAPI.ModOptions:getOptions("ZomboLewdFramework")
  SexOption = {};
  SexSubMenu = {};
  
  if not opts:getOption("box_Survivor_Sex"):getValue() then
    return
  end
  if character:isFemale() == clickedPlr:isFemale() then
    return
  end

  local sOption = context:addOption(getText("ContextMenu_AskForSex"), 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, onAskForSex, ContextMenuClass, iPosition, character, clickedPlr)
    else
      DebugLog.log(string.format("Missing Text: %s", anim))
    end
  end
end