// SVG icons for the /uses page. One per group / subhead.
// Animations: defined in index.html via .gp-* keyframes + classes.

const UsesIcon = ({ size = 56, label, children }) => {
  const T = window.TOKENS;
  return (
    <svg
      width={size} height={size} viewBox="0 0 160 160"
      fill="none" stroke={T.accent} strokeWidth="1.6"
      strokeLinecap="round" strokeLinejoin="round"
      className="uses-icon"
      role="img"
      aria-label={label || ''}
      aria-hidden={label ? undefined : true}
    >
      {children}
    </svg>
  );
};

// === /machines top-level (meta) ===

// All-up icon for the /machines group header — server rack w/ staggered LED activity.
const MachinesMeta = (props) => {
  const T = window.TOKENS;
  const rows = [34, 52, 70, 88, 106];
  return (
    <UsesIcon {...props} label="machines">
      {rows.map((y, i) => (
        <React.Fragment key={i}>
          <rect x="30" y={y} width="92" height="14" />
          <line x1="38" y1={y+4} x2="38" y2={y+10} stroke={T.fgGhost} strokeWidth="2" />
          <line x1="46" y1={y+4} x2="46" y2={y+10} stroke={T.fgGhost} strokeWidth="2" />
          <circle cx="116" cy={y+7} r="2.5" fill={T.accent} stroke="none"
            className="gp-blink"
            style={{ animationDelay: `${i*0.4}s`, animationDuration: '2s' }} />
        </React.Fragment>
      ))}
      <line x1="36"  y1="124" x2="44"  y2="124" strokeWidth="2" />
      <line x1="116" y1="124" x2="124" y2="124" strokeWidth="2" />
    </UsesIcon>
  );
};

// === /machines subheads ===

// main_machine — tower PC + power LED (2s blink)
const MachinesMain = (props) => {
  const T = window.TOKENS;
  return (
    <UsesIcon {...props} label="tower PC">
      <rect x="44" y="28" width="72" height="112" />
      <line x1="44" y1="50" x2="116" y2="50" stroke={T.fgGhost} />
      <rect x="56" y="60" width="48" height="6" stroke={T.fgGhost} />
      <rect x="56" y="72" width="48" height="6" stroke={T.fgGhost} />
      <rect x="56" y="90" width="48" height="36" stroke={T.fgGhost} />
      <line x1="56" y1="98"  x2="104" y2="98"  stroke={T.fgFaint} />
      <line x1="56" y1="106" x2="104" y2="106" stroke={T.fgFaint} />
      <line x1="56" y1="114" x2="104" y2="114" stroke={T.fgFaint} />
      <line x1="56" y1="122" x2="104" y2="122" stroke={T.fgFaint} />
      <circle cx="80" cy="40" r="3" fill={T.accent} stroke="none"
        className="gp-blink" style={{ animationDuration: '2s' }} />
    </UsesIcon>
  );
};

// mobile_machine — top-down open laptop + random key flash
const MachinesMobile = (props) => {
  const T = window.TOKENS;
  const [active, setActive] = React.useState(0);
  React.useEffect(() => {
    const t = setInterval(() => setActive(Math.floor(Math.random() * 30)), 700);
    return () => clearInterval(t);
  }, []);
  const keys = [];
  let idx = 0;
  for (let r = 0; r < 3; r++) {
    for (let c = 0; c < 10; c++) {
      const x = 36 + c * 8;
      const y = 78 + r * 9;
      keys.push(
        <rect key={idx} x={x} y={y} width="6" height="6" rx="1"
          stroke={idx === active ? T.accent : T.fgGhost}
          fill={idx === active ? T.accent : 'none'}
          fillOpacity={idx === active ? 0.4 : 0} />
      );
      idx++;
    }
  }
  return (
    <UsesIcon {...props} label="laptop">
      <rect x="28" y="22" width="104" height="46" />
      <line x1="36" y1="32" x2="124" y2="32" stroke={T.fgGhost} />
      <line x1="36" y1="40" x2="116" y2="40" stroke={T.fgFaint} />
      <line x1="36" y1="48" x2="120" y2="48" stroke={T.fgFaint} />
      <rect x="28" y="74" width="104" height="50" rx="2" />
      {keys}
      <rect x="56" y="106" width="48" height="6" rx="1" stroke={T.fgGhost} />
      <rect x="60" y="116" width="40" height="3" stroke={T.fgFaint} />
    </UsesIcon>
  );
};

// peripherals — headphones + sound waves both sides
const Peripherals = (props) => (
  <UsesIcon {...props} label="peripherals">
    <path d="M40 80 A40 36 0 0 1 120 80" />
    <rect x="28" y="74" width="20" height="32" rx="3" />
    <rect x="112" y="74" width="20" height="32" rx="3" />
    <path d="M22 80 A8 12 0 0 0 22 100"  className="gp-wifi-1" strokeWidth="2" />
    <path d="M14 76 A14 18 0 0 0 14 104" className="gp-wifi-2" strokeWidth="2" />
    <path d="M138 80 A8 12 0 0 1 138 100"  className="gp-wifi-1" strokeWidth="2" />
    <path d="M146 76 A14 18 0 0 1 146 104" className="gp-wifi-2" strokeWidth="2" />
  </UsesIcon>
);

// === Top-level groups ===

const TerminalAndShell = (props) => {
  const T = window.TOKENS;
  return (
    <UsesIcon {...props} label="terminal">
      <rect x="20" y="32" width="120" height="96" rx="3" />
      <line x1="20" y1="48" x2="140" y2="48" />
      <circle cx="32" cy="40" r="2" fill={T.accent}  stroke="none" />
      <circle cx="42" cy="40" r="2" fill={T.fgGhost} stroke="none" />
      <circle cx="52" cy="40" r="2" fill={T.fgGhost} stroke="none" />
      <text x="34" y="78" fontFamily="JetBrains Mono, monospace" fontSize="14"
        fill={T.fgMid} stroke="none">$ zsh</text>
      <text x="34" y="104" fontFamily="JetBrains Mono, monospace" fontSize="14"
        fill={T.accent} stroke="none">›</text>
      <rect x="48" y="94" width="9" height="13"
        fill={T.accent} stroke="none" className="gp-caret" />
    </UsesIcon>
  );
};

const CliTools = (props) => {
  const T = window.TOKENS;
  return (
    <UsesIcon {...props} label="cli tools">
      <text x="20" y="48" fontFamily="JetBrains Mono, monospace" fontSize="11"
        fill={T.fgMid} stroke="none">$ rg "foo"</text>
      <text x="20" y="76" fontFamily="JetBrains Mono, monospace" fontSize="10"
        fill={T.fgGhost} stroke="none">main.py:42  match</text>
      <text x="20" y="96" fontFamily="JetBrains Mono, monospace" fontSize="10"
        fill={T.fgGhost} stroke="none">utils.py:9  match</text>
      <text x="20" y="116" fontFamily="JetBrains Mono, monospace" fontSize="10"
        fill={T.fgGhost} stroke="none">api.py:81   match</text>
      <rect x="18" y="64" width="124" height="14"
        fill={T.accent} fillOpacity="0.25" stroke="none" className="gp-hl" />
    </UsesIcon>
  );
};

const DevStack = (props) => {
  const T = window.TOKENS;
  return (
    <UsesIcon {...props} label="dev stack">
      <line x1="40" y1="32" x2="40" y2="128" stroke={T.fgGhost} strokeWidth="2" />
      <path d="M40 60 C40 60, 80 60, 80 80 L80 100 C80 120, 40 120, 40 120"
        strokeDasharray="80" className="gp-branch" />
      <circle cx="40" cy="40"  r="4" fill={T.bg} stroke={T.fg} />
      <circle cx="40" cy="60"  r="4" fill={T.bg} stroke={T.fg} />
      <circle cx="80" cy="80"  r="4" fill={T.bg} stroke={T.accent} />
      <circle cx="80" cy="100" r="4" fill={T.bg} stroke={T.accent} />
      <circle cx="40" cy="120" r="4" fill={T.bg} stroke={T.fg} />
    </UsesIcon>
  );
};

const AiStack = (props) => {
  const T = window.TOKENS;
  const L1 = [40, 80, 120];
  const L2 = [36, 64, 96, 124];
  const L3 = [60, 100];
  return (
    <UsesIcon {...props} label="ai stack">
      {L1.flatMap(y1 => L2.map(y2 =>
        <line key={`a-${y1}-${y2}`} x1="32" y1={y1} x2="80" y2={y2} stroke={T.fgFaint} />
      ))}
      {L2.flatMap(y1 => L3.map(y2 =>
        <line key={`b-${y1}-${y2}`} x1="80" y1={y1} x2="128" y2={y2} stroke={T.fgFaint} />
      ))}
      {L1.map(y => <circle key={'l1'+y} cx="32" cy={y} r="4" fill={T.bg} stroke={T.fgGhost} />)}
      {L2.map(y => <circle key={'l2'+y} cx="80" cy={y} r="4" fill={T.bg} stroke={T.fgGhost} />)}
      {L3.map(y => <circle key={'l3'+y} cx="128" cy={y} r="4" fill={T.bg} stroke={T.accent} />)}
      <circle r="3" fill={T.cyan} stroke="none">
        <animate attributeName="cx"
          values="32;80;128;80;32;80;128;80;32"
          dur="6.4s" repeatCount="indefinite" />
        <animate attributeName="cy"
          values="40;36;60;124;120;124;100;64;40"
          dur="6.4s" repeatCount="indefinite" />
      </circle>
    </UsesIcon>
  );
};

const Miscellaneous = (props) => {
  const T = window.TOKENS;
  return (
    <UsesIcon {...props} label="miscellaneous">
      <circle cx="50"  cy="80" r="6" fill={T.accent} stroke="none" className="gp-type-1" />
      <circle cx="80"  cy="80" r="6" fill={T.accent} stroke="none" className="gp-type-2" />
      <circle cx="110" cy="80" r="6" fill={T.accent} stroke="none" className="gp-type-3" />
    </UsesIcon>
  );
};

window.UsesIcons = {
  machinesMeta: MachinesMeta,
  machinesMain: MachinesMain,
  machinesMobile: MachinesMobile,
  peripherals: Peripherals,
  terminalAndShell: TerminalAndShell,
  cliTools: CliTools,
  devStack: DevStack,
  aiStack: AiStack,
  miscellaneous: Miscellaneous,
};
