'use client'

import { useState } from 'react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from '@/components/ui/dialog'

interface TextDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  onConfirm: (text: string, fontSize: number) => void
  selectedColor: string
}

export function TextDialog({ open, onOpenChange, onConfirm, selectedColor }: TextDialogProps) {
  const [text, setText] = useState('')
  const [fontSize, setFontSize] = useState(16)

  const handleConfirm = () => {
    if (text.trim()) {
      onConfirm(text.trim(), fontSize)
      setText('')
      setFontSize(16)
    }
  }

  const handleKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === 'Enter') {
      handleConfirm()
    }
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-md top-[12%] translate-y-0">
        <DialogHeader>
          <DialogTitle>Text platzieren</DialogTitle>
        </DialogHeader>
        <div className="space-y-4 py-4">
          <div className="space-y-2">
            <Label htmlFor="text-input">Text</Label>
            <Input
              id="text-input"
              value={text}
              onChange={(e) => setText(e.target.value)}
              onKeyDown={handleKeyDown}
              placeholder="z.B. Zugang Süd, Brandstelle, etc."
              autoFocus
            />
          </div>
          <div className="space-y-2">
            <Label htmlFor="font-size">Schriftgrösse: {fontSize}px</Label>
            <input
              id="font-size"
              type="range"
              min="10"
              max="32"
              value={fontSize}
              onChange={(e) => setFontSize(Number(e.target.value))}
              className="w-full"
            />
          </div>
          <div className="flex items-center gap-2">
            <Label>Vorschau:</Label>
            <span
              style={{ fontSize: `${fontSize}px`, color: selectedColor, fontWeight: 'bold' }}
            >
              {text || 'Beispieltext'}
            </span>
          </div>
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            Abbrechen
          </Button>
          <Button onClick={handleConfirm} disabled={!text.trim()}>
            Platzieren
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}
