import { Group, Circle, Line, Arc } from 'react-konva';

interface SwitchSymbolProps {
  readonly x: number;
  readonly y: number;
  readonly rotation: number;
  readonly color: string;
  readonly scale: number;
}

/**
 * IEC 60617 switch symbols.
 * Base: circle with a line at angle (toggle arm).
 */

/** Single switch: circle with angled line. */
export function SingleSwitchSymbol({ x, y, rotation, color, scale }: SwitchSymbolProps) {
  const r = 6 * scale;
  const armLen = 14 * scale;

  return (
    <Group x={x} y={y} rotation={rotation}>
      <Circle radius={r} stroke={color} strokeWidth={1.5} fill="transparent" listening={false} />
      {/* Toggle arm at 45 degrees from bottom of circle */}
      <Line
        points={[0, r, -armLen * Math.sin(Math.PI / 4), r + armLen * Math.cos(Math.PI / 4)]}
        stroke={color}
        strokeWidth={1.5}
        listening={false}
      />
    </Group>
  );
}

/** Double switch: circle with two toggle marks. */
export function DoubleSwitchSymbol({ x, y, rotation, color, scale }: SwitchSymbolProps) {
  const r = 6 * scale;
  const armLen = 14 * scale;
  const tickLen = 3 * scale;

  return (
    <Group x={x} y={y} rotation={rotation}>
      <Circle radius={r} stroke={color} strokeWidth={1.5} fill="transparent" listening={false} />
      {/* Toggle arm */}
      <Line
        points={[0, r, -armLen * Math.sin(Math.PI / 4), r + armLen * Math.cos(Math.PI / 4)]}
        stroke={color}
        strokeWidth={1.5}
        listening={false}
      />
      {/* Two crossing ticks on the arm to indicate double */}
      <Line
        points={[
          -armLen * 0.4 * Math.sin(Math.PI / 4) - tickLen * Math.cos(Math.PI / 4),
          r + armLen * 0.4 * Math.cos(Math.PI / 4) - tickLen * Math.sin(Math.PI / 4),
          -armLen * 0.4 * Math.sin(Math.PI / 4) + tickLen * Math.cos(Math.PI / 4),
          r + armLen * 0.4 * Math.cos(Math.PI / 4) + tickLen * Math.sin(Math.PI / 4),
        ]}
        stroke={color}
        strokeWidth={1.5}
        listening={false}
      />
      <Line
        points={[
          -armLen * 0.6 * Math.sin(Math.PI / 4) - tickLen * Math.cos(Math.PI / 4),
          r + armLen * 0.6 * Math.cos(Math.PI / 4) - tickLen * Math.sin(Math.PI / 4),
          -armLen * 0.6 * Math.sin(Math.PI / 4) + tickLen * Math.cos(Math.PI / 4),
          r + armLen * 0.6 * Math.cos(Math.PI / 4) + tickLen * Math.sin(Math.PI / 4),
        ]}
        stroke={color}
        strokeWidth={1.5}
        listening={false}
      />
    </Group>
  );
}

/** Dimmer switch: circle with an arc symbol. */
export function DimmerSwitchSymbol({ x, y, rotation, color, scale }: SwitchSymbolProps) {
  const r = 6 * scale;
  const armLen = 14 * scale;

  return (
    <Group x={x} y={y} rotation={rotation}>
      <Circle radius={r} stroke={color} strokeWidth={1.5} fill="transparent" listening={false} />
      {/* Toggle arm */}
      <Line
        points={[0, r, -armLen * Math.sin(Math.PI / 4), r + armLen * Math.cos(Math.PI / 4)]}
        stroke={color}
        strokeWidth={1.5}
        listening={false}
      />
      {/* Dimmer arc inside circle */}
      <Arc
        innerRadius={0}
        outerRadius={r * 0.6}
        angle={180}
        rotation={-90}
        stroke={color}
        strokeWidth={1.2}
        listening={false}
      />
    </Group>
  );
}
