'use client'

import { Button } from '@/components/ui/button'
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from '@/components/ui/tooltip'
import {
  MousePointer2,
  Circle,
  CircleDot,
  Square,
  Minus,
  Pentagon,
  Type,
  Pencil,
  Undo2,
  Redo2,
  MoveRight,
  Ruler,
  Eraser,
} from 'lucide-react'
import type { DrawMode } from '@/types'
import { cn } from '@/lib/utils'

interface LeftToolbarProps {
  drawMode: DrawMode
  onDrawModeChange: (mode: DrawMode) => void
  selectedColor: string
  onColorChange: (color: string) => void
  selectedWidth: number
  onWidthChange: (width: number) => void
  onUndo: () => void
  onRedo: () => void
  canEdit: boolean
}

const colors = [
  { value: '#ef4444', name: 'Rot' },
  { value: '#f97316', name: 'Orange' },
  { value: '#eab308', name: 'Gelb' },
  { value: '#22c55e', name: 'Grün' },
  { value: '#3b82f6', name: 'Blau' },
  { value: '#8b5cf6', name: 'Violett' },
  { value: '#000000', name: 'Schwarz' },
  { value: '#ffffff', name: 'Weiss' },
]

const drawTools: { mode: DrawMode; icon: typeof MousePointer2; label: string; shortcut: string }[] = [
  { mode: 'select', icon: MousePointer2, label: 'Auswählen', shortcut: 'V' },
  { mode: 'point', icon: CircleDot, label: 'Punkt', shortcut: 'P' },
  { mode: 'linestring', icon: Minus, label: 'Linie', shortcut: 'L' },
  { mode: 'polygon', icon: Pentagon, label: 'Polygon', shortcut: 'G' },
  { mode: 'rectangle', icon: Square, label: 'Rechteck', shortcut: 'R' },
  { mode: 'circle', icon: Circle, label: 'Kreis', shortcut: 'C' },
  { mode: 'freehand', icon: Pencil, label: 'Freihand', shortcut: 'F' },
  { mode: 'arrow', icon: MoveRight, label: 'Pfeil / Route', shortcut: 'A' },
  { mode: 'text', icon: Type, label: 'Text', shortcut: 'T' },
  { mode: 'eraser', icon: Eraser, label: 'Radiergummi', shortcut: 'E' },
  { mode: 'measure', icon: Ruler, label: 'Messen', shortcut: 'M' },
]

export function LeftToolbar({
  drawMode,
  onDrawModeChange,
  selectedColor,
  onColorChange,
  selectedWidth,
  onWidthChange,
  onUndo,
  onRedo,
  canEdit,
}: LeftToolbarProps) {
  return (
    <TooltipProvider delayDuration={300}>
      <aside className="w-14 lg:w-20 border-r border-border bg-card flex flex-col items-center py-1.5 lg:py-2 shrink-0 overflow-y-auto overflow-x-hidden z-10">
        {/* Draw Tools — Touch-Ziele mind. 44px auf Mobil/Tablet */}
        <div className="flex flex-col gap-0.5">
          {drawTools.map((tool) => (
            <Tooltip key={tool.mode}>
              <TooltipTrigger asChild>
                <Button
                  variant={drawMode === tool.mode ? 'default' : 'ghost'}
                  size="icon"
                  className="w-11 h-11 lg:w-12 lg:h-12"
                  onClick={() => onDrawModeChange(tool.mode)}
                  disabled={!canEdit && tool.mode !== 'select'}
                >
                  <tool.icon className="w-5 h-5 lg:w-6 lg:h-6" />
                </Button>
              </TooltipTrigger>
              <TooltipContent side="right">
                <p>{tool.label} <kbd className="ml-1.5 text-[10px] px-1 py-0.5 bg-muted rounded border border-border font-mono">{tool.shortcut}</kbd></p>
              </TooltipContent>
            </Tooltip>
          ))}
        </div>

        <div className="w-8 lg:w-12 h-px bg-border my-1" />

        {/* Undo/Redo — mobil gestapelt (44px), Desktop 2-spaltig */}
        <div className="grid grid-cols-1 lg:grid-cols-2 gap-0.5">
          <Tooltip>
            <TooltipTrigger asChild>
              <Button
                variant="ghost"
                size="icon"
                className="w-11 h-11 lg:w-8 lg:h-8"
                onClick={onUndo}
                disabled={!canEdit}
              >
                <Undo2 className="w-5 h-5 lg:w-5 lg:h-5" />
              </Button>
            </TooltipTrigger>
            <TooltipContent side="right">
              <p>Rückgängig</p>
            </TooltipContent>
          </Tooltip>

          <Tooltip>
            <TooltipTrigger asChild>
              <Button
                variant="ghost"
                size="icon"
                className="w-11 h-11 lg:w-8 lg:h-8"
                onClick={onRedo}
                disabled={!canEdit}
              >
                <Redo2 className="w-5 h-5 lg:w-5 lg:h-5" />
              </Button>
            </TooltipTrigger>
            <TooltipContent side="right">
              <p>Wiederholen</p>
            </TooltipContent>
          </Tooltip>

        </div>

        <div className="w-8 lg:w-12 h-px bg-border my-1" />

        {/* Color Picker - 2-col grid, grössere Swatches für Touch */}
        <div className="grid grid-cols-2 gap-1">
          {colors.map((color) => (
            <button
              key={color.value}
              className={cn(
                'w-6 h-6 lg:w-7 lg:h-7 rounded border-2 transition-all',
                selectedColor === color.value
                  ? 'border-primary ring-1 ring-primary ring-offset-1 scale-110'
                  : 'border-muted hover:border-muted-foreground'
              )}
              style={{ backgroundColor: color.value }}
              onClick={() => onColorChange(color.value)}
              disabled={!canEdit}
              title={color.name}
            />
          ))}
        </div>
      </aside>
    </TooltipProvider>
  )
}
