import { useState } from 'react'
import { Button, Card } from './ui'
import { RecoveryWarning, RecoveryPhraseDisplay, PublicKeyDisplay, ImportKeyForm, ImportStepButtons, ChooseStepButtons } from './CreateAccountModalComponents'
import { t } from '@/lib/i18n'

export function RecoveryStep({
  recoveryPhrase,
  npub,
  onContinue,
}: {
  recoveryPhrase: string[]
  npub: string
  onContinue: () => void
}): React.ReactElement {
  const [copied, setCopied] = useState(false)

  const handleCopy = async (): Promise<void> => {
    if (recoveryPhrase.length > 0) {
      await navigator.clipboard.writeText(recoveryPhrase.join(' '))
      setCopied(true)
      setTimeout(() => setCopied(false), 2000)
    }
  }

  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <Card variant="default" className="bg-cyber-dark max-w-2xl w-full mx-4 max-h-[90vh] overflow-y-auto shadow-glow-cyan">
        <h2 className="text-2xl font-bold mb-4 text-neon-cyan">{t('account.create.recovery.title')}</h2>
        <RecoveryWarning />
        <RecoveryPhraseDisplay recoveryPhrase={recoveryPhrase} copied={copied} onCopy={() => { void handleCopy() }} />
        <PublicKeyDisplay npub={npub} />
        <div className="flex gap-4">
          <Button
            onClick={onContinue}
            variant="primary"
            size="large"
            className="flex-1"
          >
            {t('account.create.recovery.saved')}
          </Button>
        </div>
      </Card>
    </div>
  )
}

export function ImportStep({
  importKey,
  setImportKey,
  loading,
  error,
  onImport,
  onBack,
}: {
  importKey: string
  setImportKey: (key: string) => void
  loading: boolean
  error: string | null
  onImport: () => void
  onBack: () => void
}): React.ReactElement {
  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <Card variant="default" className="bg-cyber-dark max-w-md w-full mx-4 shadow-glow-cyan">
        <h2 className="text-2xl font-bold mb-4 text-neon-cyan">{t('account.import.title')}</h2>
        <ImportKeyForm importKey={importKey} setImportKey={setImportKey} error={error} />
        <ImportStepButtons loading={loading} onImport={onImport} onBack={onBack} />
      </Card>
    </div>
  )
}

export function ChooseStep({
  loading,
  error,
  onGenerate,
  onImport,
  onClose,
}: {
  loading: boolean
  error: string | null
  onGenerate: () => void
  onImport: () => void
  onClose: () => void
}): React.ReactElement {
  return (
    <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
      <Card variant="default" className="bg-cyber-dark max-w-md w-full mx-4 shadow-glow-cyan">
        <h2 className="text-2xl font-bold mb-4 text-neon-cyan">{t('account.create.title')}</h2>
        <p className="text-cyber-accent/70 mb-6">
          {t('account.create.description')}
        </p>
        {error && <p className="text-sm text-red-400 mb-4">{error}</p>}
        <ChooseStepButtons loading={loading} onGenerate={onGenerate} onImport={onImport} onClose={onClose} />
      </Card>
    </div>
  )
}
