// ─── TranslateBook ───────────────────────────────────────────────────────
// Better product experience: Iris doesn't try to paint English onto the
// Arabic letterforms (like a consumer AR app). It gives you ONE calm glass
// card sitting over the book with the full passage translated — same
// treatment as every other scene. The book stays the book; Iris stays a
// quiet lens.
//
// Timing:
//   0.00 – 0.10  book alone, chip says "Arabic · detected"
//   0.10 – 0.22  chip swings to "Translating", card scales up & fades in
//   0.22 – 0.90  card holds, reading time
//   0.90 – 1.00  card and chip fade out, book alone again

function TranslateBook() {
  const [t, setT] = React.useState(0);
  const raf = React.useRef(null);
  const startRef = React.useRef(null);
  const DURATION = 9.0;

  React.useEffect(() => {
    const tick = (ts) => {
      if (startRef.current === null) startRef.current = ts;
      const elapsed = ((ts - startRef.current) / 1000) % DURATION;
      setT(elapsed);
      raf.current = requestAnimationFrame(tick);
    };
    raf.current = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf.current);
  }, []);

  const u = t / DURATION;
  const chipOp = interp(u, [0.02, 0.10, 0.95, 1.00], [0, 1, 1, 0]);

  // Card: fade + lift in, hold, fade out.
  const cardOp = interp(u, [0.12, 0.22, 0.88, 0.96], [0, 1, 1, 0]);
  const cardLift = interp(u, [0.12, 0.22], [8, 0]);

  return (
    <>
      <img src="book-ar.png" alt=""
           style={{
             position:"absolute", inset:0, width:"100%", height:"100%",
             objectFit:"cover", objectPosition:"center", display:"block"
           }}/>

      {/* Very subtle vignette so the glass card has presence */}
      <div style={{
        position:"absolute", inset:0, pointerEvents:"none",
        background:"radial-gradient(ellipse at center, rgba(0,0,0,0) 40%, rgba(0,0,0,0.35) 100%)"
      }}/>

      <TranslationCard opacity={cardOp} lift={cardLift} />

      <TranslateChip opacity={chipOp} state={chipState(u)} />
      <HUDStatus time="21:08" battery={72} context="living room · reading" />
      <BrandCorner />
    </>
  );
}

// ─── The translation card ───────────────────────────────────────────────
// One dark-glass card, centered over the open book. Same visual language
// as HUDGlass: rgba(20,14,10,0.52), warm-white text, amber accent, blur.
// A small Instrument-Serif pull-quote anchors the top; body text runs in
// Instrument Sans. Feels like Iris surfacing the meaning, not OCR paint.
function TranslationCard({ opacity, lift }) {
  if (opacity <= 0.01) return null;
  return (
    <div style={{
      position: "absolute",
      left: "50%", top: "50%",
      transform: `translate(-50%, calc(-50% + ${lift}px))`,
      width: "min(58%, 620px)",
      padding: "22px 26px 20px",
      borderRadius: 16,
      background: "linear-gradient(180deg, rgba(26,18,12,0.88) 0%, rgba(20,14,10,0.92) 100%)",
      backdropFilter: "blur(24px) saturate(125%)",
      WebkitBackdropFilter: "blur(24px) saturate(125%)",
      border: "1px solid rgba(255, 248, 236, 0.22)",
      boxShadow: "0 24px 70px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,248,236,0.18)",
      color: "#FFF8EC",
      fontFamily: "'Instrument Sans', sans-serif",
      textShadow: "0 1px 2px rgba(0,0,0,0.6)",
      opacity,
      pointerEvents: "none",
    }}>
      {/* Eyebrow: page reference — matches HUDGlass eyebrow style */}
      <div style={{
        display: "flex", alignItems: "center", gap: 10,
        fontSize: 11, fontWeight: 500,
        letterSpacing: "0.08em", textTransform: "uppercase",
        opacity: 0.68,
      }}>
        <span style={{position:"relative", width:7, height:7}}>
          <span style={{position:"absolute", inset:0, borderRadius:"50%", background:"#F2B677", opacity:0.35}}/>
          <span style={{position:"absolute", inset:0, borderRadius:"50%", background:"#F2B677"}}/>
        </span>
        <span>Page 112–113 · translated</span>
      </div>

      {/* Pull quote — the one line Iris thinks matters most */}
      <div style={{
        fontFamily: "'Instrument Serif', 'Iowan Old Style', Georgia, serif",
        fontSize: 24, lineHeight: 1.25, letterSpacing: "-0.01em",
        marginTop: 12, marginBottom: 16,
        color: "#FFF8EC",
      }}>
        "The joy we searched for was inside us all along."
      </div>

      {/* Body — full passage, quietly typeset */}
      <div style={{
        fontSize: 13, lineHeight: 1.55, opacity: 0.88,
        display: "grid",
        gridTemplateColumns: "1fr 1fr",
        gap: "0 22px",
      }}>
        <p style={{margin: 0}}>
          There are moments when we realize we've been running after shadows,
          chasing people, dreams, and goals, thinking they hold the key to
          our happiness. When we finally reach them, we find the joy was
          inside us all along.
        </p>
        <p style={{margin: 0}}>
          There will always be challenges, days when nothing goes as planned.
          But these moments are not the end. They are the journey, shaping
          us, preparing us for something greater.
        </p>
      </div>

      {/* Meta footer — thin rule + small tech detail */}
      <div style={{
        marginTop: 16, paddingTop: 10,
        borderTop: "1px solid rgba(255,248,236,0.16)",
        display: "flex", justifyContent: "space-between", alignItems: "center",
        fontSize: 10, letterSpacing: "0.06em", textTransform: "uppercase",
        opacity: 0.55,
      }}>
        <span>Arabic → English</span>
        <span>Look away to dismiss</span>
      </div>
    </div>
  );
}

// ─── Chip ────────────────────────────────────────────────────────────────
function TranslateChip({ opacity, state }) {
  if (opacity <= 0.01) return null;
  const label = state === "idle"        ? "Arabic · detected" :
                state === "translating" ? "Translating"       :
                                          "Arabic → English";
  return (
    <div style={{
      position:"absolute", left:"50%", top:"4%",
      transform:"translateX(-50%)",
      opacity,
      display:"inline-flex", alignItems:"center", gap:12,
      padding:"10px 18px",
      background:"rgba(20,14,10,0.48)",
      backdropFilter:"blur(16px) saturate(120%)",
      WebkitBackdropFilter:"blur(16px) saturate(120%)",
      border:"1px solid rgba(255,248,236,0.22)",
      borderRadius:999,
      color:"#FFF8EC",
      fontFamily:"'Instrument Sans', sans-serif",
      fontSize:13, fontWeight:500,
      textShadow:"0 1px 4px rgba(0,0,0,0.8)",
      boxShadow:"0 8px 32px rgba(0,0,0,0.4)",
    }}>
      <span style={{position:"relative", width:10, height:10}}>
        <span style={{position:"absolute", inset:0, borderRadius:"50%", background:"#F2B677", opacity:0.4}}/>
        <span style={{position:"absolute", inset:0, borderRadius:"50%", background:"#F2B677"}}/>
      </span>
      <span style={{fontFamily:"'JetBrains Mono', monospace", fontSize:10, letterSpacing:"0.08em", textTransform:"uppercase", opacity:0.7}}>
        ar → en
      </span>
      <span style={{width:1, height:14, background:"rgba(255,248,236,0.22)"}}/>
      <span>{label}</span>
    </div>
  );
}

function BrandCorner() {
  return (
    <div style={{
      position:"absolute", left:26, bottom:24,
      fontFamily:"'Instrument Sans', sans-serif",
      textShadow:"0 1px 4px rgba(0,0,0,0.9)",
    }}>
      <svg viewBox="0 0 240 120" height={18} style={{overflow:"visible"}}>
        <text x="0" y="88" fontFamily="Inter, sans-serif" fontSize="100" fontStyle="italic" fontWeight="500" fill="#FFF8EC" letterSpacing="-0.09em">Iris</text>
        <g transform="translate(218 83)">
          <circle cx="0" cy="0" r="7" fill="#F2B677" opacity="0.4"/>
          <circle cx="0" cy="0" r="7" fill="#F2B677"/>
        </g>
      </svg>
    </div>
  );
}

function chipState(u){
  if (u < 0.12) return "idle";
  if (u < 0.30) return "translating";
  return "done";
}

function interp(t, xs, ys){
  if (t <= xs[0]) return ys[0];
  for (let i = 0; i < xs.length - 1; i++){
    if (t >= xs[i] && t <= xs[i+1]){
      const p = (t - xs[i]) / (xs[i+1] - xs[i]);
      return ys[i] + p * (ys[i+1] - ys[i]);
    }
  }
  return ys[ys.length - 1];
}

window.TranslateBook = TranslateBook;
