/* unitcirclelab.jsx — UnitCircleTutor: the unit-circle bench. Built on window.BenchKit
   (harness) + window.BenchFields (shared field math), so this file is just the FIGURE.
   computeFields = window.BenchFields.unitCircle — the SAME function server/benches/unitcircle.js
   calls, so client and server fields are identical by construction. Mirrors gaslawslab.jsx:
   sliders → BenchFields.fn(state) → report + meters. */
(function () {
  const { useState, useMemo, useEffect } = React;
  const K = window.BenchKit, C = K.C, SVG_BG = K.SVG_BG, fmt = K.fmt;
  const TaskStrip = K.TaskStrip, StatMeter = K.StatMeter;
  const compute = (s) => window.BenchFields.unitCircle(s);

  function Slider({ label, value, set, min, max, step, unit, color, onUp }) {
    return (
      <div style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 9 }}>
        <span style={{ fontSize: 12, color: C.mute, width: 116 }}>{label}</span>
        <input type="range" min={min} max={max} step={step} value={value} onChange={(e) => set(parseFloat(e.target.value))} onPointerUp={onUp} style={{ flex: 1 }} />
        <span style={{ color: color || C.amber, width: 64, textAlign: "right" }}>{value}{unit ? ` ${unit}` : ""}</span>
      </div>
    );
  }

  function UnitCircleFig({ task, setTask, tasks, report, event, done }) {
    const [angleDeg, setAngleDeg] = useState(135);
    const fld = useMemo(() => compute({ angleDeg }), [angleDeg]);
    useEffect(() => { report(fld); }, [angleDeg]); // eslint-disable-line

    // geometry: circle centered at (CX, CY), radius R; SVG y-axis points down so negate sinθ.
    const CX = 200, CY = 150, R = 110;
    const th = (angleDeg * Math.PI) / 180;
    const cosT = Math.cos(th), sinT = Math.sin(th);
    const px = CX + R * cosT, py = CY - R * sinT;
    // arc from positive x-axis to the point (counter-clockwise → SVG sweep-flag 0).
    const aR = 34;
    const ax0 = CX + aR, ay0 = CY;
    const ax1 = CX + aR * cosT, ay1 = CY - aR * sinT;
    const largeArc = angleDeg > 180 ? 1 : 0;
    const t = tasks.find((x) => x.id === task) || tasks[0];

    return (
      <>
        <TaskStrip tasks={tasks} cur={task} setCur={setTask} done={done} goal={t && t.goal} />
        <svg viewBox="0 0 600 300" style={{ width: "100%", background: SVG_BG, border: `1px solid ${C.line}`, borderRadius: 8 }}>
          {/* axes */}
          <line x1={CX - R - 24} y1={CY} x2={CX + R + 24} y2={CY} stroke={C.faint} strokeWidth="1.5" />
          <line x1={CX} y1={CY - R - 24} x2={CX} y2={CY + R + 24} stroke={C.faint} strokeWidth="1.5" />
          {/* unit circle */}
          <circle cx={CX} cy={CY} r={R} fill="none" stroke={C.teal} strokeWidth="1.75" opacity="0.8" />
          {/* angle arc */}
          <path d={`M ${ax0} ${ay0} A ${aR} ${aR} 0 ${largeArc} 0 ${ax1} ${ay1}`} fill="none" stroke={C.gold} strokeWidth="2" />
          <text x={CX + aR + 6} y={CY - 6} fill={C.gold} fontSize="11" fontFamily="monospace">θ = {Math.round(angleDeg)}°</text>
          {/* dashed drops: to x-axis (cos) and to y-axis (sin) */}
          <line x1={px} y1={py} x2={px} y2={CY} stroke={C.blue} strokeWidth="1.5" strokeDasharray="4 3" />
          <line x1={px} y1={py} x2={CX} y2={py} stroke={C.crimson} strokeWidth="1.5" strokeDasharray="4 3" />
          {/* radius to the point */}
          <line x1={CX} y1={CY} x2={px} y2={py} stroke={C.ink} strokeWidth="2" />
          {/* the point */}
          <circle cx={px} cy={py} r={5} fill={C.amber} stroke={C.ink} strokeWidth="1" />
          <text x={px + (cosT >= 0 ? 9 : -9)} y={py + (sinT >= 0 ? -9 : 16)} textAnchor={cosT >= 0 ? "start" : "end"} fill={C.ink} fontSize="12" fontFamily="monospace">
            ({fmt(fld.cosVal)}, {fmt(fld.sinVal)})
          </text>
          <text x={CX + R + 8} y={CY + 16} fill={C.blue} fontSize="11" fontFamily="monospace">x = cosθ</text>
          <text x={CX + 6} y={CY - R - 10} fill={C.crimson} fontSize="11" fontFamily="monospace">y = sinθ</text>
        </svg>
        <div style={{ marginTop: 10, padding: "8px 12px", borderRadius: 6, background: C.panel, border: `1px solid ${C.line}`, color: C.mute, fontSize: 12.5 }}>
          point = (cosθ, sinθ) = ({fmt(fld.cosVal)}, {fmt(fld.sinVal)}) · radians = {fmt(fld.radians)} · Quadrant {fld.quadrant}{fld.isSpecialAngle ? " · special angle" : ""}.
        </div>
        <Slider label="angle θ" value={angleDeg} set={setAngleDeg} min={0} max={360} step={1} unit="°" color={C.gold} onUp={() => event("adjusted", `Set θ = ${Math.round(angleDeg)}°`, { response: `(${fmt(fld.cosVal)}, ${fmt(fld.sinVal)})` }, C.gold)} />
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 8, marginTop: 12 }}>
          <StatMeter label="cosθ (x)" v={fld.cosVal} d={3} color={C.blue} />
          <StatMeter label="sinθ (y)" v={fld.sinVal} d={3} color={C.crimson} />
          <StatMeter label="tanθ" v={fld.tanVal} d={2} color={C.amber} />
          <StatMeter label="quadrant" v={fld.quadrant} d={0} color={C.teal} />
        </div>
      </>
    );
  }

  window.UnitCircleTutor = K.makeTutor(UnitCircleFig, { moduleLabel: "Unit circle bench", benchId: "unitcircle" });
})();
