/* schematic-kit.jsx — Level 2 · The Language (how to read a schematic).
   Self-contained schematic SYMBOL glyphs + matching physical BREADBOARD part
   glyphs + the interactive pieces that teach reading: SymbolKey (the alphabet),
   ReadTheLoop (trace the current), and SchematicVsBoard (the same circuit shown
   as a diagram AND as a real build, cross-highlighted). All names slg-/Slg-
   prefixed so they never collide with other per-file globals. */

const SLG_INK = "var(--ink-soft)";
const SLG_HOT = "var(--current)";
const SLG_FAINT = "var(--ink-faint)";
const slgGlow = (on) => (on ? "drop-shadow(0 0 5px var(--current))" : "none");

/* ── schematic SYMBOL glyphs ────────────────────────────────────────────────
   Each takes (cx, cy, col, o?) and returns an SVG group of stroked primitives
   in the site's hand-drawn ink style. o.h = horizontal (default true). */
const SlgSym = {
  battery(cx, cy, col) {
    // two stacked cells, vertical orientation (leads exit top/bottom)
    return (
      <g>
        <line x1={cx - 13} y1={cy - 9} x2={cx + 13} y2={cy - 9} stroke={col} strokeWidth="2.2" />
        <line x1={cx - 7} y1={cy - 4} x2={cx + 7} y2={cy - 4} stroke={col} strokeWidth="3.4" />
        <line x1={cx - 13} y1={cy + 4} x2={cx + 13} y2={cy + 4} stroke={col} strokeWidth="2.2" />
        <line x1={cx - 7} y1={cy + 9} x2={cx + 7} y2={cy + 9} stroke={col} strokeWidth="3.4" />
        <text x={cx + 18} y={cy - 6} fontFamily="IBM Plex Mono, monospace" fontSize="11" fill={col}>+</text>
      </g>
    );
  },
  resistor(cx, cy, col, o = {}) {
    const h = o.h !== false;
    return h
      ? <rect x={cx - 17} y={cy - 7} width="34" height="14" rx="2" fill="var(--bg-card)" stroke={col} strokeWidth="2" />
      : <rect x={cx - 7} y={cy - 17} width="14" height="34" rx="2" fill="var(--bg-card)" stroke={col} strokeWidth="2" />;
  },
  led(cx, cy, col, o = {}) {
    const dir = o.dir || "down"; // arrow of travel
    // triangle + cathode bar; vertical (current flows down) by default
    if (dir === "down") {
      return (
        <g>
          <path d={`M${cx - 9} ${cy - 9} L${cx + 9} ${cy - 9} L${cx} ${cy + 7} Z`} fill="var(--bg-card)" stroke={col} strokeWidth="2" />
          <line x1={cx - 9} y1={cy + 7} x2={cx + 9} y2={cy + 7} stroke={col} strokeWidth="2.2" />
          <path d={`M${cx + 11} ${cy - 10} l6 -3`} stroke={col} strokeWidth="1.3" fill="none" />
          <path d={`M${cx + 13} ${cy - 16} l5 1 -1 5`} stroke={col} strokeWidth="1.3" fill="none" />
        </g>
      );
    }
    return ( // horizontal, points right
      <g>
        <path d={`M${cx - 9} ${cy - 9} L${cx + 7} ${cy} L${cx - 9} ${cy + 9} Z`} fill="var(--bg-card)" stroke={col} strokeWidth="2" />
        <line x1={cx + 7} y1={cy - 9} x2={cx + 7} y2={cy + 9} stroke={col} strokeWidth="2.2" />
        <path d={`M${cx + 2} ${cy - 12} l5 -4`} stroke={col} strokeWidth="1.3" fill="none" />
        <path d={`M${cx + 6} ${cy - 18} l5 1 -1 5`} stroke={col} strokeWidth="1.3" fill="none" />
      </g>
    );
  },
  capacitor(cx, cy, col) {
    // vertical branch: two horizontal plates
    return (
      <g>
        <line x1={cx - 11} y1={cy - 3} x2={cx + 11} y2={cy - 3} stroke={col} strokeWidth="2.4" />
        <line x1={cx - 11} y1={cy + 4} x2={cx + 11} y2={cy + 4} stroke={col} strokeWidth="2.4" />
        <text x={cx + 15} y={cy - 4} fontFamily="IBM Plex Mono, monospace" fontSize="10" fill={col}>+</text>
      </g>
    );
  },
  diode(cx, cy, col, o = {}) {
    const h = o.h !== false;
    if (h) return (
      <g>
        <path d={`M${cx - 9} ${cy - 8} L${cx + 7} ${cy} L${cx - 9} ${cy + 8} Z`} fill="var(--bg-card)" stroke={col} strokeWidth="2" />
        <line x1={cx + 7} y1={cy - 8} x2={cx + 7} y2={cy + 8} stroke={col} strokeWidth="2.2" />
      </g>
    );
    return (
      <g>
        <path d={`M${cx - 8} ${cy - 9} L${cx + 8} ${cy - 9} L${cx} ${cy + 7} Z`} fill="var(--bg-card)" stroke={col} strokeWidth="2" />
        <line x1={cx - 8} y1={cy + 7} x2={cx + 8} y2={cy + 7} stroke={col} strokeWidth="2.2" />
      </g>
    );
  },
  ground(cx, cy, col) {
    return (
      <g>
        <line x1={cx} y1={cy - 10} x2={cx} y2={cy} stroke={col} strokeWidth="2" />
        <line x1={cx - 11} y1={cy} x2={cx + 11} y2={cy} stroke={col} strokeWidth="2.4" />
        <line x1={cx - 7} y1={cy + 5} x2={cx + 7} y2={cy + 5} stroke={col} strokeWidth="2.4" />
        <line x1={cx - 3} y1={cy + 10} x2={cx + 3} y2={cy + 10} stroke={col} strokeWidth="2.4" />
      </g>
    );
  },
  switch(cx, cy, col) {
    return (
      <g>
        <circle cx={cx - 12} cy={cy} r="2.6" fill={col} />
        <circle cx={cx + 12} cy={cy} r="2.6" fill={col} />
        <line x1={cx - 12} y1={cy} x2={cx + 9} y2={cy - 11} stroke={col} strokeWidth="2.2" />
      </g>
    );
  },
  transistor(cx, cy, col) {
    return (
      <g>
        <circle cx={cx} cy={cy} r="14" fill="var(--bg-card)" stroke={col} strokeWidth="1.8" />
        <line x1={cx - 6} y1={cy - 9} x2={cx - 6} y2={cy + 9} stroke={col} strokeWidth="3" />
        <line x1={cx - 18} y1={cy} x2={cx - 6} y2={cy} stroke={col} strokeWidth="2" />
        <line x1={cx - 6} y1={cy - 4} x2={cx + 9} y2={cy - 11} stroke={col} strokeWidth="2" />
        <line x1={cx - 6} y1={cy + 4} x2={cx + 9} y2={cy + 11} stroke={col} strokeWidth="2" />
        <path d={`M${cx + 2} ${cy + 6} l7 5 -6 1`} fill={col} stroke="none" />
      </g>
    );
  },
  ic(cx, cy, col) {
    return (
      <g>
        <rect x={cx - 16} y={cy - 13} width="32" height="26" rx="3" fill="var(--bg-card)" stroke={col} strokeWidth="1.9" />
        <path d={`M${cx - 5} ${cy - 13} a5 5 0 0 0 10 0`} fill="none" stroke={col} strokeWidth="1.3" />
        <text x={cx} y={cy + 5} textAnchor="middle" fontFamily="IBM Plex Mono, monospace" fontSize="11" fontWeight="600" fill={col}>555</text>
      </g>
    );
  },
  wire(cx, cy, col) {
    return (
      <g>
        <line x1={cx - 17} y1={cy} x2={cx + 17} y2={cy} stroke={col} strokeWidth="2" />
        <circle cx={cx} cy={cy} r="3.2" fill={col} />
      </g>
    );
  },
};

/* the alphabet, in teaching order */
const SLG_ALPHABET = [
  { type: "battery", name: { adult: "Battery / cell", kids: "Battery" },
    desc: { adult: "The source. Long line is +, short fat line is −. Stacked pairs mean more cells (more volts).",
            kids: "The pusher. The long line is the + side. It gives the push that moves the flow." } },
  { type: "wire", name: { adult: "Wire & junction", kids: "Wire" },
    desc: { adult: "A plain line is a wire — no resistance. A solid dot means wires are joined; crossing with no dot means they pass over, unconnected.",
            kids: "A line is a wire the flow runs along. A dot means two wires are joined together." } },
  { type: "resistor", name: { adult: "Resistor", kids: "Resistor (pinch)" },
    desc: { adult: "Limits current. The rectangle (or zig-zag, in US style) is the same part. Value in Ω sits beside it.",
            kids: "A pinch in the pipe. It slows the flow down so nothing gets too much." } },
  { type: "led", name: { adult: "LED", kids: "LED (light)" },
    desc: { adult: "A diode that emits light. Triangle points the way current flows; the bar is the cathode (−). The two arrows mean 'light out'.",
            kids: "A light. The triangle shows which way the flow goes through it. It only works one way round." } },
  { type: "capacitor", name: { adult: "Capacitor", kids: "Capacitor (bucket)" },
    desc: { adult: "Stores charge. Two plates. A curved/ + -marked plate means it's polarised (electrolytic) — mind the +.",
            kids: "A little bucket. It fills up with charge and empties out — that's what makes things fade and blink." } },
  { type: "diode", name: { adult: "Diode", kids: "Diode (one-way)" },
    desc: { adult: "A one-way valve for current. Triangle is the allowed direction; the bar blocks the reverse.",
            kids: "A one-way door. Flow goes through the pointy way, never backwards." } },
  { type: "switch", name: { adult: "Switch", kids: "Switch" },
    desc: { adult: "Opens or closes the path. Drawn open (lever lifted) — press it and the two dots connect.",
            kids: "A gate. Open = no flow. Closed = flow runs through." } },
  { type: "transistor", name: { adult: "Transistor (NPN)", kids: "Transistor (gate)" },
    desc: { adult: "A current-controlled switch/amplifier. Base (left) controls flow from collector to emitter (the arrow marks the emitter).",
            kids: "A tiny tap a small signal can open. A little push on the side lets a big flow through." } },
  { type: "ic", name: { adult: "IC (e.g. 555)", kids: "Chip" },
    desc: { adult: "An integrated circuit — many parts in one box. Pins are numbered; the notch/dot marks pin 1.",
            kids: "A whole circuit packed into one little box, with legs (pins) to wire it up." } },
  { type: "ground", name: { adult: "Ground (0 V)", kids: "Ground" },
    desc: { adult: "The reference 0 V — usually the − rail. Every ground symbol is the same node, even when not drawn connected.",
            kids: "The bottom — where the flow returns to. Think of it as the floor everything sits on." } },
];

function SlgSymbolKey({ kids }) {
  const [open, setOpen] = React.useState("battery");
  return (
    <div className="slg-key">
      <div className="slg-key-grid">
        {SLG_ALPHABET.map(s => {
          const on = open === s.type;
          return (
            <button key={s.type} className={`slg-key-card ${on ? "on" : ""}`} onClick={() => setOpen(s.type)}>
              <svg viewBox="0 0 96 64" className="slg-key-svg">
                <g style={{ filter: slgGlow(on) }}>{SlgSym[s.type](48, 32, on ? SLG_HOT : SLG_INK)}</g>
              </svg>
              <span className="slg-key-name">{kids ? s.name.kids : s.name.adult}</span>
            </button>
          );
        })}
      </div>
      <div className="slg-key-detail">
        {(() => {
          const s = SLG_ALPHABET.find(x => x.type === open);
          return (
            <>
              <svg viewBox="0 0 120 80" className="slg-key-detail-svg">
                <g style={{ filter: slgGlow(true) }}>{SlgSym[s.type](60, 40, SLG_HOT)}</g>
              </svg>
              <div>
                <div className="slg-key-detail-name">{kids ? s.name.kids : s.name.adult}</div>
                <p className="slg-key-detail-desc">{kids ? s.desc.kids : s.desc.adult}</p>
              </div>
            </>
          );
        })()}
      </div>
    </div>
  );
}

Object.assign(window, { SlgSym, SLG_ALPHABET, SlgSymbolKey, SLG_INK, SLG_HOT, SLG_FAINT, slgGlow });
