/* eslint-disable react/jsx-no-undef */
// GENER8 Promo Video — Scene components
// 25s @ 1920x1080. Five scenes:
//   0.0 – 3.0  : Hook
//   3.0 – 11.5 : Brief composer
//  11.5 – 19.5 : Five cards fan out and write
//  19.5 – 22.5 : Engagement bursts on each card
//  22.5 – 25.0 : Logo + CTA

// ─────────────────────────────────────────────────────────────────────────
// Shared bits
// ─────────────────────────────────────────────────────────────────────────

const PLAT = {
  instagram: {
    name: "Instagram",
    accent: "#E1306C",
    bg: "rgba(225,48,108,0.12)",
    border: "rgba(225,48,108,0.4)",
    dotBg: "linear-gradient(135deg,#FEDA75,#FA7E1E 30%,#D62976 60%,#962FBF 80%,#4F5BD5)",
    text: "The moment you stop waiting for the right time is the moment things start moving.\n\nNew drop. Link in bio.",
    icon: (s=18) => <svg width={s} height={s} viewBox="0 0 22 22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="2" y="2" width="18" height="18" rx="5"/><circle cx="11" cy="11" r="4"/><circle cx="16.5" cy="5.5" r="1" fill="currentColor" strokeWidth="0"/></svg>,
  },
  tiktok: {
    name: "TikTok",
    accent: "#fff",
    bg: "rgba(255,255,255,0.07)",
    border: "rgba(255,255,255,0.22)",
    dotBg: "#010101",
    text: "POV: you just stopped procrastinating and hit launch\n\nno permission needed.\njust go.",
    icon: (s=18) => <svg width={s} height={s} viewBox="0 0 22 22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M15 2c.3 2.5 1.7 4.2 4 4.7v3.3c-1.5 0-3-.5-4.2-1.4V15a5.8 5.8 0 1 1-5.8-5.8V12.5a2.5 2.5 0 1 0 2.5 2.5V2H15Z"/></svg>,
  },
  facebook: {
    name: "Facebook",
    accent: "#1877F2",
    bg: "rgba(24,119,242,0.12)",
    border: "rgba(24,119,242,0.4)",
    dotBg: "#1877F2",
    text: "6 months ago I had an idea. Today we launched.\n\nWhat's one thing you wish you'd started sooner?",
    icon: (s=18) => <svg width={s} height={s} viewBox="0 0 22 22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M13.5 4H16V2h-2.5A5 5 0 0 0 8.5 7v2H6v3h2.5v8h3V12H14l.5-3h-3V7A2.5 2.5 0 0 1 13.5 4Z"/></svg>,
  },
  youtube: {
    name: "YouTube",
    accent: "#FF4444",
    bg: "rgba(255,68,68,0.1)",
    border: "rgba(255,68,68,0.38)",
    dotBg: "#FF0000",
    text: "[HOOK 0:00 — 0:10]\n\"The product everyone's been waiting for is finally here.\"\n\n[CTA] Link in description.",
    icon: (s=18) => <svg width={s} height={s} viewBox="0 0 22 22" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><rect x="2" y="5" width="18" height="14" rx="3"/><polygon points="9,9 9,15 15,12" fill="currentColor" stroke="none"/></svg>,
  },
  email: {
    name: "Email",
    accent: "#6366F1",
    bg: "rgba(99,102,241,0.12)",
    border: "rgba(99,102,241,0.4)",
    dotBg: "#6366F1",
    text: "SUBJECT: Something new just dropped\n\nHey — this one's been a long time coming. Three years of work. One quiet launch.",
    icon: (s=18) => <svg width={s} height={s} viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><rect x="1" y="2.5" width="12" height="9" rx="1.5"/><path d="M1 4l6 4.5L13 4"/></svg>,
  },
};

const PLAT_ORDER = ["instagram", "tiktok", "facebook", "youtube", "email"];

// Ambient drifting orbs that float behind every scene
function AmbientOrbs() {
  const t = useTime();
  const x1 = Math.sin(t * 0.18) * 50;
  const y1 = Math.cos(t * 0.22) * 40;
  const x2 = Math.cos(t * 0.16) * 60;
  const y2 = Math.sin(t * 0.20) * 50;
  return (
    <>
      <div style={{
        position: "absolute", top: "5%", right: "-8%",
        width: 900, height: 900, borderRadius: "50%",
        background: "radial-gradient(circle, oklch(60% 0.19 25 / 0.08) 0%, transparent 62%)",
        transform: `translate(${x1}px, ${y1}px)`,
        pointerEvents: "none",
      }} />
      <div style={{
        position: "absolute", bottom: "0%", left: "-10%",
        width: 760, height: 760, borderRadius: "50%",
        background: "radial-gradient(circle, oklch(55% 0.12 250 / 0.06) 0%, transparent 62%)",
        transform: `translate(${x2}px, ${y2}px)`,
        pointerEvents: "none",
      }} />
    </>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// SCENE 1 — Hook (0.0 – 3.0)
//   "Sunday. Five platforms." with italic accent on "Five platforms."
//   Five platform glyphs drift in soft orbit around the headline.
// ─────────────────────────────────────────────────────────────────────────

function SceneHook() {
  return (
    <Sprite start={0} end={3.0}>
      {({ localTime }) => {
        // Headline reveal: 0.3s line 1, 1.0s line 2, exit at 2.5s
        const line1Op = clamp((localTime - 0.2) / 0.6, 0, 1);
        const line1Y  = (1 - Easing.easeOutCubic(line1Op)) * 30;
        const line2Op = clamp((localTime - 0.9) / 0.6, 0, 1);
        const line2Y  = (1 - Easing.easeOutCubic(line2Op)) * 30;

        // Exit
        const exit = clamp((localTime - 2.5) / 0.5, 0, 1);
        const containerOp = 1 - exit;
        const containerY  = -exit * 24;

        return (
          <div style={{
            position: "absolute", inset: 0,
            display: "flex", alignItems: "center", justifyContent: "center",
            flexDirection: "column",
            opacity: containerOp,
            transform: `translateY(${containerY}px)`,
          }}>
            {/* Orbiting platform glyphs */}
            {PLAT_ORDER.map((id, i) => {
              const p = PLAT[id];
              const angle = (i / 5) * Math.PI * 2 - Math.PI / 2 + localTime * 0.18;
              const r = 420;
              const cx = 960 + Math.cos(angle) * r;
              const cy = 540 + Math.sin(angle) * r * 0.55;
              const op = clamp((localTime - 0.1 - i * 0.06) / 0.5, 0, 1) * 0.6 * (1 - exit);
              return (
                <div key={id} style={{
                  position: "absolute",
                  left: cx - 36, top: cy - 36,
                  width: 72, height: 72, borderRadius: "50%",
                  background: p.bg, border: `1px solid ${p.border}`,
                  display: "grid", placeItems: "center",
                  color: p.accent,
                  opacity: op,
                  boxShadow: `0 0 24px ${p.bg}`,
                }}>
                  {p.icon(28)}
                </div>
              );
            })}

            {/* Headline */}
            <div style={{
              position: "relative",
              fontFamily: "var(--font-serif)",
              fontWeight: 500,
              fontSize: 116,
              letterSpacing: "-0.03em",
              lineHeight: 1.05,
              color: "var(--ink)",
              textAlign: "center",
            }}>
              <div style={{ opacity: line1Op, transform: `translateY(${line1Y}px)` }}>
                Sunday morning.
              </div>
              <div style={{
                opacity: line2Op, transform: `translateY(${line2Y}px)`,
                fontStyle: "italic", color: "var(--accent)",
                marginTop: 4,
              }}>
                Five posts to write.
              </div>
            </div>
          </div>
        );
      }}
    </Sprite>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// SCENE 2 — Brief composer (3.0 – 11.5)
//   Compose window slides up center, brief types out, Generate clicked,
//   spinner pulses, then transition.
// ─────────────────────────────────────────────────────────────────────────

const BRIEF_TEXT = "Just dropped our new product line. Big moment for the brand. Want to capture the energy without sounding corporate.";

function SceneBrief() {
  return (
    <Sprite start={3.0} end={11.5}>
      {({ localTime }) => {
        // Window entrance (0.0–0.6)
        const inT = clamp(localTime / 0.6, 0, 1);
        const winY  = (1 - Easing.easeOutCubic(inT)) * 60;
        const winOp = inT;
        const winScale = 0.92 + 0.08 * Easing.easeOutCubic(inT);

        // Brief typing: starts 0.8s, 35ms per char (~3.7s for the whole thing)
        const typeStart = 0.8;
        const charsPerSec = 28;
        const chars = Math.floor(Math.max(0, (localTime - typeStart) * charsPerSec));
        const typedText = BRIEF_TEXT.slice(0, Math.min(chars, BRIEF_TEXT.length));
        const typingDone = chars >= BRIEF_TEXT.length;

        // Generate click (typingDone + 0.6s)
        const clickAt = typeStart + BRIEF_TEXT.length / charsPerSec + 0.4; // ~5.6s
        const clickActive = localTime >= clickAt && localTime < clickAt + 0.25;
        const btnScale = clickActive ? 0.94 : 1;

        // Spinner phase
        const spinnerStart = clickAt + 0.2;
        const spinning = localTime >= spinnerStart && localTime < 8.3;

        // Exit (window scales up and accelerates away after 8.3s)
        const exitT = clamp((localTime - 8.3) / 0.4, 0, 1);
        const exitScale = 1 + exitT * 0.06;
        const exitOp = 1 - exitT;

        return (
          <div style={{
            position: "absolute", inset: 0,
            display: "flex", alignItems: "center", justifyContent: "center",
            opacity: winOp * exitOp,
          }}>
            <div style={{
              transform: `translateY(${winY}px) scale(${winScale * exitScale})`,
              transformOrigin: "center",
            }}>
              <ComposeWindow
                brief={typedText}
                cursor={!typingDone}
                generating={spinning}
                btnScale={btnScale}
              />
            </div>
          </div>
        );
      }}
    </Sprite>
  );
}

function ComposeWindow({ brief, cursor, generating, btnScale }) {
  return (
    <div style={{
      width: 820, background: "oklch(11% 0.006 28)",
      border: "1px solid var(--line-soft)",
      borderRadius: 22, overflow: "hidden",
      boxShadow: "0 60px 120px oklch(0% 0 0 / 0.7), 0 0 0 1px oklch(93% 0.005 60 / 0.04)",
    }}>
      {/* Title bar */}
      <div style={{
        padding: "16px 22px", borderBottom: "1px solid var(--line-soft)",
        display: "flex", alignItems: "center", gap: 8,
      }}>
        <div style={{ display: "flex", gap: 6 }}>
          <div style={{ width: 13, height: 13, borderRadius: "50%", background: "#FF5F56" }} />
          <div style={{ width: 13, height: 13, borderRadius: "50%", background: "#FFBD2E" }} />
          <div style={{ width: 13, height: 13, borderRadius: "50%", background: "#27C93F" }} />
        </div>
        <div style={{ flex: 1, textAlign: "center", fontSize: 15, color: "var(--ink-4)" }}>
          GENER8 · Compose
        </div>
      </div>

      {/* Brief field */}
      <div style={{ padding: "22px 24px 16px" }}>
        <div style={{
          fontSize: 12, fontWeight: 600, letterSpacing: "0.08em",
          textTransform: "uppercase", color: "var(--ink-4)", marginBottom: 12,
        }}>Brief</div>
        <div style={{
          background: "var(--bg)",
          border: "1px solid var(--line-soft)",
          borderRadius: 10, padding: "18px 20px",
          fontSize: 21, color: "var(--ink-2)", lineHeight: 1.5,
          minHeight: 96,
          fontFamily: "var(--font-sans)",
        }}>
          {brief}
          {cursor && <span style={{
            display: "inline-block", width: 2, height: 22,
            background: "var(--accent)", marginLeft: 2, verticalAlign: "-4px",
            animation: "g8v-blink 0.9s steps(2) infinite",
          }} />}
        </div>
      </div>

      {/* Footer: Generate button */}
      <div style={{
        padding: "8px 24px 22px",
        display: "flex", justifyContent: "flex-end", alignItems: "center",
      }}>
        <div style={{
          padding: "12px 28px", borderRadius: 10,
          fontSize: 16, fontWeight: 700,
          background: "var(--accent)", color: "#fff",
          display: "inline-flex", alignItems: "center", gap: 8,
          transform: `scale(${btnScale})`,
          transition: "transform 100ms var(--ease-out)",
          boxShadow: generating ? "none" : "0 6px 22px oklch(60% 0.19 25 / 0.35)",
        }}>
          {generating ? (
            <>
              <span style={{
                display: "inline-block", width: 14, height: 14,
                border: "2px solid rgba(255,255,255,0.35)",
                borderTopColor: "#fff", borderRadius: "50%",
                animation: "g8v-spin 0.7s linear infinite",
              }} />
              Writing your posts…
            </>
          ) : (
            <>Generate →</>
          )}
        </div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────
// CAPTIONS — lower-third explainer that narrates the real-world value
//   phase by phase, so a first-time viewer understands what's happening
//   and how GENER8 fits a real situation (not just a flashy animation).
//   Lives in the empty bottom band (clears the cards above and the trust
//   pill that arrives at ~20.4s).
// ─────────────────────────────────────────────────────────────────────────

const VIDEO_CAPTIONS = [
  // Hook: the real, recurring situation — and why it's painful (each platform differs).
  { start: 0.4,  end: 3.0,  text: "Launch day — and every platform needs its own kind of post." },
  // Brief: how little effort it takes from the user.
  { start: 3.5,  end: 9.0,  text: "You write one plain brief — just say what happened, like texting a coworker." },
  // Reveal: the actual transformation, the core value.
  { start: 9.5,  end: 18.6, text: "GENER8 rewrites it natively for each platform — matching tone, length, and format." },
  // CTA payoff: restate the outcome while the bottom band is clear.
  { start: 23.0, end: 24.7, text: "Five platform-ready posts from one brief — in seconds." },
];

function SceneCaptions() {
  const t = useTime();
  return (
    <div style={{
      position: "absolute", left: 0, right: 0, bottom: 72,
      display: "flex", justifyContent: "center",
      pointerEvents: "none",
    }}>
      {VIDEO_CAPTIONS.map((c, i) => {
        const fadeIn  = clamp((t - c.start) / 0.5, 0, 1);
        const fadeOut = clamp((c.end - t) / 0.5, 0, 1);
        const op = Math.min(fadeIn, fadeOut);
        if (op <= 0) return null;
        const y = (1 - Easing.easeOutCubic(fadeIn)) * 14;
        return (
          <div key={i} style={{
            position: "absolute",
            opacity: op,
            transform: `translateY(${y}px)`,
            padding: "13px 30px",
            background: "oklch(11% 0.006 28 / 0.72)",
            border: "1px solid var(--line-soft)",
            borderRadius: 999,
            backdropFilter: "blur(8px)",
            WebkitBackdropFilter: "blur(8px)",
            fontFamily: "var(--font-sans)",
            fontSize: 27,
            fontWeight: 500,
            color: "var(--ink-2)",
            whiteSpace: "nowrap",
            letterSpacing: "-0.01em",
          }}>
            {c.text}
          </div>
        );
      })}
    </div>
  );
}

Object.assign(window, {
  PLAT, PLAT_ORDER, AmbientOrbs, SceneHook, SceneBrief, ComposeWindow, BRIEF_TEXT,
  SceneCaptions, VIDEO_CAPTIONS,
});
