local TELEPORTS = {
    {name = "Rotworm", level = 8, pos = Position(32479, 32499, 8)},
    {name = "Minotaur", level = 15, pos = Position(32551, 32347, 8)},
    {name = "Cyclops", level = 30, pos = Position(32564, 32569, 8)},
    {name = "Nightmares", level = 80, pos = Position(32044, 32414, 8)},
     {name = "Yielothax", level = 80, pos = Position(32332, 32382, 10)},
}

local function countdownAndTeleport(playerId, name, position, countdown)
    local player = Player(playerId)
    if not player then return end

    if countdown == 0 then
        player:teleportTo(position)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
        player:say(name .. " awaits you!", TALKTYPE_MONSTER_SAY)
        return
    end

    player:say(tostring(countdown), TALKTYPE_MONSTER_SAY)
    addEvent(countdownAndTeleport, 1000, playerId, name, position, countdown - 1)
end

function onModalWindow(player, modalId, buttonId, choiceId)
    if modalId ~= 35477 then return false end

    if buttonId == 2 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "Modal closed.")
        return true
    end

    local data = TELEPORTS[choiceId + 1] -- +1 bo Lua liczy od 1
    if not data then
        player:sendTextMessage(MESSAGE_STATUS_SMALL, "Invalid selection.")
        return true
    end

    if player:getLevel() < data.level then
        player:sendCancelMessage("You need to be at least level " .. data.level .. " to hunt " .. data.name .. "s.")
        return true
    end

    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Preparing teleport to " .. data.name .. "...")
    countdownAndTeleport(player:getId(), data.name, data.pos, 3)

    return true
end

function onLogin(player)
    player:registerEvent("TeleportModal")
    return true
end
