'use client'

import * as React from 'react'
import { History, Home, Settings, User } from 'lucide-react'

import {
  CommandDialog,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
  CommandSeparator
} from '@/components/ui/command'
import { DialogTitle } from '@radix-ui/react-dialog'
import * as VisuallyHidden from '@radix-ui/react-visually-hidden'
import { useRouter } from 'next/navigation'
import { Client } from './page'
import { io } from 'socket.io-client'

interface CommandMenuProps {
  open: boolean
  setOpen: React.Dispatch<React.SetStateAction<boolean>>
}

export function CommandMenu ({ open, setOpen }: CommandMenuProps) {
  // const [open, setOpen] = React.useState(false)
  const router = useRouter()
  const [clients, setClients] = React.useState<Client[]>([])

  React.useEffect(() => {
    const socketUrl =
      process.env.NEXT_PUBLIC_SOCKET_URL || 'http://localhost:8080'
    const newSocket = io(`${socketUrl}/admin`)
    
    newSocket.on('clientUpdate', updatedClients => {
      setClients(updatedClients)
    })

    return () => {
      newSocket.close()
    }
  }, [])
  React.useEffect(() => {
    const down = (e: KeyboardEvent) => {
      if (e.key === 'k' && (e.metaKey || e.ctrlKey)) {
        e.preventDefault()
        setOpen(open => !open)
      }
    }

    document.addEventListener('keydown', down)
    return () => document.removeEventListener('keydown', down)
  }, [])

  const runCommand = React.useCallback((command: () => unknown) => {
    setOpen(false)
    command()
  }, [])

  return (
    <>
      <CommandDialog open={open} onOpenChange={setOpen}>
        <CommandInput />
        <CommandList>
          <CommandEmpty>No results found.</CommandEmpty>
          <CommandGroup heading='Victims'>
            {clients.map(client => (
              <CommandItem
                key={client.id}
                onSelect={() => {
                  runCommand(() => router.push(`/dashboard/${client.id}`))
                }}
              >
                <User />
                {client.ip}
              </CommandItem>
            ))}
            {clients.length === 0 ? (
              <div className='text-sm text-center w-full text-muted-foreground'>
                No victims at the moment
              </div>
            ) : (
              <></>
            )}
          </CommandGroup>
          <CommandSeparator className={'mt-1'} />
          <CommandGroup heading='Pages'>
            <CommandItem
              onSelect={() => {
                runCommand(() => router.push('/dashboard'))
              }}
            >
              <Home />
              <span>Home</span>
            </CommandItem>
            {/* <CommandItem
              onSelect={() => {
                runCommand(() => router.push('/dashboard/victims'))
              }}
            >
              <User />
              <span>Victims</span>
            </CommandItem> */}
            <CommandItem
              onSelect={() => {
                runCommand(() => router.push('/dashboard/history'))
              }}
            >
              <History />
              <span>History</span>
            </CommandItem>
            <CommandItem
              onSelect={() => {
                runCommand(() => router.push('/dashboard/settings'))
              }}
            >
              <Settings />
              <span>Settings</span>
            </CommandItem>
          </CommandGroup>
        </CommandList>
        <DialogTitle className='hidden'>
          <VisuallyHidden.Root>x</VisuallyHidden.Root>
        </DialogTitle>
      </CommandDialog>
    </>
  )
}
