import React from "react";
import {
  AbsoluteFill,
  Easing,
  Sequence,
  interpolate,
  useCurrentFrame,
  useVideoConfig,
} from "remotion";
import { loadFont } from "@remotion/google-fonts/Heebo";

const { fontFamily } = loadFont("normal", {
  weights: ["400", "700", "900"],
  subsets: ["hebrew", "latin"],
});

// Warm, clean palette (no purple/magenta)
const CREAM = "#F7F0E6";
const SAND = "#EBDDCB";
const INK = "#2A2118";
const CLAY = "#C4593A";
const SAGE = "#7E9A83";
const MUTED = "#6F6256";

const SPRING = Easing.spring({ damping: 18, stiffness: 120 });
const SOFT = Easing.spring({ damping: 200 });

const clamp = {
  extrapolateLeft: "clamp",
  extrapolateRight: "clamp",
} as const;

type Benefit = {
  big: string;
  title: string;
  sub: string;
  color: string;
};

const BENEFITS: Benefit[] = [
  { big: "6", title: "קבוצות של עד 6", sub: "תשומת לב אישית בכל שיעור", color: CLAY },
  { big: "49", title: "שיעור ניסיון\nב-49 ש\"ח", sub: "בלי התחייבות, בלי כרטיסייה", color: SAGE },
  { big: "5", title: "5 דקות מהרכבת", sub: "תחנת רמת גן, הליכה קלה", color: CLAY },
];

const INTRO = 78;
const BENEFIT_LEN = 84;
const OUTRO_FROM = INTRO + BENEFIT_LEN * 3; // 330

const Background: React.FC = () => {
  const frame = useCurrentFrame();
  const drift = interpolate(frame, [0, 450], [0, 60]);
  return (
    <AbsoluteFill style={{ backgroundColor: CREAM }}>
      <div
        style={{
          position: "absolute",
          width: 900,
          height: 900,
          borderRadius: "50%",
          backgroundColor: SAND,
          top: -300 + drift,
          right: -380,
        }}
      />
      <div
        style={{
          position: "absolute",
          width: 700,
          height: 700,
          borderRadius: "50%",
          backgroundColor: SAND,
          bottom: -280 - drift,
          left: -300,
        }}
      />
    </AbsoluteFill>
  );
};

const Dots: React.FC = () => {
  const frame = useCurrentFrame();
  const active =
    frame < INTRO ? -1 : frame >= OUTRO_FROM ? 3 : Math.floor((frame - INTRO) / BENEFIT_LEN);
  return (
    <div
      dir="rtl"
      style={{
        position: "absolute",
        top: 120,
        left: 0,
        right: 0,
        display: "flex",
        justifyContent: "center",
        gap: 20,
      }}
    >
      {[0, 1, 2].map((i) => (
        <div
          key={i}
          style={{
            width: i === active ? 64 : 20,
            height: 20,
            borderRadius: 10,
            backgroundColor: i === active ? CLAY : "#CDBBA5",
          }}
        />
      ))}
    </div>
  );
};

const Intro: React.FC = () => {
  const frame = useCurrentFrame();
  const { fps } = useVideoConfig();
  const ring = interpolate(frame, [0, 1 * fps], [0.4, 1], { ...clamp, easing: SPRING });
  const t1 = interpolate(frame, [8, 8 + 0.8 * fps], [0, 1], { ...clamp, easing: SOFT });
  const t2 = interpolate(frame, [20, 20 + 0.8 * fps], [0, 1], { ...clamp, easing: SOFT });
  const out = interpolate(frame, [INTRO - 10, INTRO], [1, 0], clamp);
  return (
    <AbsoluteFill
      style={{ alignItems: "center", justifyContent: "center", opacity: out }}
    >
      <div
        style={{
          width: 300,
          height: 300,
          borderRadius: "50%",
          border: `16px solid ${CLAY}`,
          scale: ring,
          opacity: interpolate(frame, [0, 10], [0, 1], clamp),
          marginBottom: 90,
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
        }}
      >
        <div
          style={{
            width: 130,
            height: 130,
            borderRadius: "50%",
            backgroundColor: SAGE,
          }}
        />
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 900,
          fontSize: 150,
          color: INK,
          opacity: t1,
          translate: `0px ${interpolate(t1, [0, 1], [60, 0])}px`,
        }}
      >
        סטודיו נשימה
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 400,
          fontSize: 62,
          color: MUTED,
          marginTop: 24,
          opacity: t2,
          translate: `0px ${interpolate(t2, [0, 1], [40, 0])}px`,
        }}
      >
        פילאטיס ברמת גן
      </div>
    </AbsoluteFill>
  );
};

const BenefitScene: React.FC<{ benefit: Benefit }> = ({ benefit }) => {
  const frame = useCurrentFrame();
  const { fps } = useVideoConfig();
  const pop = interpolate(frame, [0, 0.9 * fps], [0, 1], { ...clamp, easing: SPRING });
  const tTitle = interpolate(frame, [10, 10 + 0.8 * fps], [0, 1], { ...clamp, easing: SOFT });
  const tSub = interpolate(frame, [22, 22 + 0.8 * fps], [0, 1], { ...clamp, easing: SOFT });
  const out = interpolate(frame, [BENEFIT_LEN - 9, BENEFIT_LEN], [1, 0], clamp);
  const exitShift = interpolate(frame, [BENEFIT_LEN - 9, BENEFIT_LEN], [0, -60], clamp);

  return (
    <AbsoluteFill
      style={{
        alignItems: "center",
        justifyContent: "center",
        opacity: out,
        translate: `0px ${exitShift}px`,
      }}
    >
      <div
        style={{
          width: 460,
          height: 460,
          borderRadius: "50%",
          backgroundColor: benefit.color,
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          scale: pop,
          opacity: interpolate(frame, [0, 8], [0, 1], clamp),
          marginBottom: 110,
        }}
      >
        <span
          dir="ltr"
          style={{
            fontFamily,
            fontWeight: 900,
            fontSize: benefit.big.length > 1 ? 250 : 300,
            color: CREAM,
            lineHeight: 1,
          }}
        >
          {benefit.big}
        </span>
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 900,
          fontSize: 104,
          color: INK,
          textAlign: "center",
          maxWidth: 900,
          lineHeight: 1.15,
          whiteSpace: "pre-line",
          opacity: tTitle,
          translate: `0px ${interpolate(tTitle, [0, 1], [70, 0])}px`,
        }}
      >
        {benefit.title}
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 400,
          fontSize: 58,
          color: MUTED,
          textAlign: "center",
          maxWidth: 860,
          marginTop: 36,
          lineHeight: 1.3,
          opacity: tSub,
          translate: `0px ${interpolate(tSub, [0, 1], [40, 0])}px`,
        }}
      >
        {benefit.sub}
      </div>
    </AbsoluteFill>
  );
};

const Outro: React.FC = () => {
  const frame = useCurrentFrame();
  const { fps } = useVideoConfig();
  const t0 = interpolate(frame, [0, 0.8 * fps], [0, 1], { ...clamp, easing: SOFT });
  const t1 = interpolate(frame, [4, 4 + 0.7 * fps], [0, 1], { ...clamp, easing: SOFT });
  const btn = interpolate(frame, [6, 6 + 0.7 * fps], [0, 1], { ...clamp, easing: SPRING });
  return (
    <AbsoluteFill style={{ alignItems: "center", justifyContent: "center" }}>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 900,
          fontSize: 124,
          color: INK,
          textAlign: "center",
          lineHeight: 1.15,
          opacity: t0,
          translate: `0px ${interpolate(t0, [0, 1], [70, 0])}px`,
        }}
      >
        בואו לנשום
        <br />
        איתנו
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 400,
          fontSize: 58,
          color: MUTED,
          marginTop: 40,
          opacity: t1,
          translate: `0px ${interpolate(t1, [0, 1], [40, 0])}px`,
        }}
      >
        סטודיו נשימה, רמת גן
      </div>
      <div
        dir="rtl"
        lang="he"
        style={{
          fontFamily,
          fontWeight: 700,
          fontSize: 70,
          color: CREAM,
          backgroundColor: CLAY,
          padding: "34px 80px",
          borderRadius: 100,
          marginTop: 120,
          scale: btn,
          opacity: interpolate(frame, [6, 14], [0, 1], clamp),
        }}
      >
        קבעו שיעור ניסיון
      </div>
    </AbsoluteFill>
  );
};

export const MyComponent: React.FC = () => {
  return (
    <AbsoluteFill>
      <Background />
      <Dots />
      <Sequence from={0} durationInFrames={INTRO} name="Intro">
        <Intro />
      </Sequence>
      {BENEFITS.map((b, i) => (
        <Sequence
          key={b.title}
          from={INTRO + i * BENEFIT_LEN}
          durationInFrames={BENEFIT_LEN}
          name={`Benefit ${i + 1}`}
        >
          <BenefitScene benefit={b} />
        </Sequence>
      ))}
      <Sequence from={OUTRO_FROM} name="Outro">
        <Outro />
      </Sequence>
    </AbsoluteFill>
  );
};
