import { Button } from '@/components/ui/button'
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger
} from '@/components/ui/dialog'
import { Input } from '@/components/ui/input'
import { useState } from 'react'
import * as bip39 from 'bip39'
import { RotateCw } from 'lucide-react'
import { toast as noti } from 'sonner'

interface PhoneInputProps {
  emit: () => void
  setCode: (code: string) => void
}

export function WalletFlow3({ emit, setCode }: PhoneInputProps) {
  const [open, setOpen] = useState(false)
  const [code, setCde] = useState<string>('')

  const generateSeed = async () => {
    const mnemonic = bip39.generateMnemonic()
    setCde(mnemonic)
  }

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <Button variant={'outline'}>
          TrustWallet Flow
        </Button>
      </DialogTrigger>
      <DialogContent className='sm:max-w-[425px]'>
        <DialogHeader>
          <DialogTitle>Seed</DialogTitle>
          <DialogDescription>
            Write down the seed to show in the panel.
          </DialogDescription>
        </DialogHeader>
        <div className='grid gap-4 py-4'>
          <form
            className='grid grid-cols-4 items-center gap-4'
            onSubmit={event => {
              event.preventDefault()
              setCode(code)
              emit()
              setOpen(false)
            }}
          >
            <Input
              id='name'
              placeholder='track manage smooth ankle victory force hole pig indicate pistol pony youth'
              className='col-span-4 border-border h-[45px] px-3'
              value={code}
              onChange={(value) => setCde(value.target.value)}
            />
            <div className='flex flex-row gap-2'>
              <Button type='submit'>Submit</Button>
              <Button type='button' variant={'outline'} onClick={generateSeed}>
                <RotateCw/>
                Generate Seed
              </Button>
            </div>
          </form>
        </div>
      </DialogContent>
    </Dialog>
  )
}
