/* NIA — Character animation for pain-point questions */

const { useState, useEffect, useRef } = React;

/* Little stick-figure character */
function Character({ variant = 0, talking = false, thinking = false }) {
  // variants: 0 = standing, 1 = scratching head, 2 = arms up, 3 = sitting at desk, 4 = shrugging
  const bodyColor = "#0A0B14";
  const accent = "#D4FF3D";
  const skin = "#F2EEE3";

  return (
    <svg viewBox="0 0 200 240" style={{ width: "100%", height: "100%" }}>
      {/* Shadow */}
      <ellipse cx="100" cy="232" rx="40" ry="4" fill="rgba(10,11,20,0.12)" />

      {/* Thinking bubble */}
      {thinking && (
        <g style={{ transformOrigin: "150px 50px", animation: "thinkPulse 2s ease-in-out infinite" }}>
          <circle cx="140" cy="50" r="8" fill="none" stroke={bodyColor} strokeWidth="2" />
          <circle cx="160" cy="40" r="5" fill="none" stroke={bodyColor} strokeWidth="2" />
          <circle cx="175" cy="28" r="3" fill={bodyColor} />
        </g>
      )}

      {/* Talking dots */}
      {talking && (
        <g>
          <circle cx="150" cy="60" r="3" fill={accent}>
            <animate attributeName="opacity" values="0;1;0" dur="1.2s" repeatCount="indefinite" begin="0s" />
          </circle>
          <circle cx="162" cy="60" r="3" fill={accent}>
            <animate attributeName="opacity" values="0;1;0" dur="1.2s" repeatCount="indefinite" begin="0.2s" />
          </circle>
          <circle cx="174" cy="60" r="3" fill={accent}>
            <animate attributeName="opacity" values="0;1;0" dur="1.2s" repeatCount="indefinite" begin="0.4s" />
          </circle>
        </g>
      )}

      {/* V0 — standing looking worried */}
      {variant === 0 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          {/* eyes */}
          <circle cx="90" cy="60" r="2.5" fill={bodyColor} />
          <circle cx="110" cy="60" r="2.5" fill={bodyColor} />
          {/* frown */}
          <path d="M88 76 Q100 70 112 76" fill="none" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          {/* body */}
          <path d="M100 90 L100 170" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* arms */}
          <path d="M100 110 L72 150" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 110 L128 150" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* legs */}
          <path d="M100 170 L82 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L118 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
        </g>
      )}

      {/* V1 — scratching head */}
      {variant === 1 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          <circle cx="90" cy="58" r="2.5" fill={bodyColor} />
          <circle cx="110" cy="58" r="2.5" fill={bodyColor} />
          <path d="M92 74 L108 74" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M100 90 L100 170" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* scratching arm up to head */}
          <path d="M100 110 L130 80 L128 48" fill="none" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
          <path d="M100 110 L75 150" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L82 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L118 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
        </g>
      )}

      {/* V2 — arms in air exasperated */}
      {variant === 2 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          <path d="M86 58 L94 64" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M106 64 L114 58" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M88 78 Q100 72 112 78" fill="none" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M100 90 L100 170" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* arms raised */}
          <path d="M100 110 L65 70" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 110 L135 70" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L82 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L118 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
        </g>
      )}

      {/* V3 — sitting at a desk */}
      {variant === 3 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          <circle cx="90" cy="60" r="2.5" fill={bodyColor} />
          <circle cx="110" cy="60" r="2.5" fill={bodyColor} />
          <path d="M92 76 L108 76" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M100 90 L100 150" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 115 L70 125" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 115 L130 125" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* desk */}
          <rect x="50" y="155" width="100" height="4" fill={bodyColor} />
          <rect x="55" y="159" width="3" height="30" fill={bodyColor} />
          <rect x="142" y="159" width="3" height="30" fill={bodyColor} />
          {/* laptop */}
          <rect x="75" y="140" width="50" height="15" fill={bodyColor} />
          <rect x="80" y="144" width="40" height="8" fill={accent} opacity="0.8" />
          {/* legs sit */}
          <path d="M100 150 L80 170 L80 205" fill="none" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
          <path d="M100 150 L120 170 L120 205" fill="none" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
        </g>
      )}

      {/* V4 — shrugging */}
      {variant === 4 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          <path d="M86 58 L94 58" stroke={bodyColor} strokeWidth="2.5" strokeLinecap="round" />
          <path d="M106 58 L114 58" stroke={bodyColor} strokeWidth="2.5" strokeLinecap="round" />
          <path d="M92 76 Q100 74 108 76" fill="none" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M100 90 L100 170" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* shrug arms */}
          <path d="M100 110 L70 100 L66 120" fill="none" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
          <path d="M100 110 L130 100 L134 120" fill="none" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" />
          <path d="M100 170 L82 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L118 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
        </g>
      )}

      {/* V5 — holding phone, stressed */}
      {variant === 5 && (
        <g>
          <circle cx="100" cy="62" r="28" fill={skin} stroke={bodyColor} strokeWidth="2.5" />
          <circle cx="90" cy="58" r="2.5" fill={bodyColor} />
          <circle cx="110" cy="58" r="2.5" fill={bodyColor} />
          <path d="M88 78 Q100 72 112 78" fill="none" stroke={bodyColor} strokeWidth="2" strokeLinecap="round" />
          <path d="M100 90 L100 170" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 110 L75 135" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 110 L120 135" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          {/* phone */}
          <rect x="110" y="128" width="18" height="30" fill={bodyColor} rx="2" />
          <rect x="113" y="132" width="12" height="18" fill={accent} />
          <path d="M100 170 L82 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
          <path d="M100 170 L118 220" stroke={bodyColor} strokeWidth="3" strokeLinecap="round" />
        </g>
      )}
    </svg>
  );
}
window.Character = Character;

/* Character animation section */
function CharacterQuestions() {
  const [lang] = useLang();
  const lines = [
    { fr: "Où est le brief de la campagne du mois dernier ?", en: "Where's last month's campaign brief?", v: 1 },
    { fr: "Qui devait combien à qui ?", en: "Who owed what to whom?", v: 4 },
    { fr: "Combien on a dépensé sur ce single ?", en: "How much did we spend on this single?", v: 3 },
    { fr: "Les paiements sont partis ? vraiment ?", en: "Did the payouts go out? really?", v: 5 },
    { fr: "Le son marche ou pas ?", en: "Is the sound working or not?", v: 2 },
    { fr: "Qui a validé ce contrat ?", en: "Who approved this contract?", v: 0 },
  ];

  const [active, setActive] = useState(0);
  const secRef = useRef();
  const progress = useSectionProgress(secRef);
  const lastScrollY = useRef(0);
  const scrollVel = useRef(0);

  // Track scroll velocity
  useEffect(() => {
    const on = () => {
      const y = window.scrollY;
      scrollVel.current = Math.abs(y - lastScrollY.current);
      lastScrollY.current = y;
    };
    window.addEventListener("scroll", on, { passive: true });
    return () => window.removeEventListener("scroll", on);
  }, []);

  // Auto-advance — 4s base, faster when scrolling
  useEffect(() => {
    let timeout;
    const tick = () => {
      setActive(a => (a + 1) % lines.length);
      // velocity decays; when scrolling, timeout is shorter
      const v = Math.min(scrollVel.current, 80);
      scrollVel.current = Math.max(0, scrollVel.current - 10);
      const delay = Math.max(650, 4000 - v * 45);
      timeout = setTimeout(tick, delay);
    };
    timeout = setTimeout(tick, 4000);
    return () => clearTimeout(timeout);
  }, [lines.length]);

  return (
    <section ref={secRef} className="section" style={{
      position: "relative", background: "var(--paper-soft)", color: "var(--ink)",
      padding: "200px 0", overflow: "hidden",
    }}>
      <div className="section-number">01 / 08 — {t(lang, "LES QUESTIONS DU LUNDI", "MONDAY QUESTIONS")}</div>

      <div className="container">
        <div data-reveal ref={useReveal()} style={{ marginBottom: 120, maxWidth: 1100 }}>
          <div className="eyebrow" style={{ marginBottom: 28 }}>{t(lang, "LE MATIN, DANS LES LABELS", "MONDAY MORNING, INSIDE LABELS")}</div>
          <h2 className="display" style={{ fontSize: "clamp(44px, 6vw, 84px)" }}>
            {t(lang,
              "Tous les lundis, les mêmes questions. Toujours sans réponse.",
              "Every Monday, the same questions. Always unanswered.")}
          </h2>
        </div>

        <div className="cq-outer" style={{
          display: "grid", gridTemplateColumns: "360px 1fr",
          gap: 80, alignItems: "center", minHeight: 460,
        }}>
          {/* Character stage */}
          <div className="cq-stage" style={{ position: "relative", height: 460 }}>
            {lines.map((l, i) => (
              <div key={i} style={{
                position: "absolute", inset: 0,
                opacity: active === i ? 1 : 0,
                transform: active === i ? "translateX(0) scale(1)" : `translateX(${active > i ? -60 : 60}px) scale(0.9)`,
                transition: "opacity 0.55s ease, transform 0.6s cubic-bezier(.2,.7,.2,1)",
              }}>
                <Character variant={l.v} talking />
              </div>
            ))}
            {/* ground line */}
            <div style={{
              position: "absolute", left: -40, right: -40, bottom: 8,
              height: 1, background: "var(--rule)",
            }} />
          </div>

          {/* Questions stack */}
          <div style={{ display: "flex", flexDirection: "column", gap: 0, borderTop: "1px solid var(--rule)" }}>
            {lines.map((l, i) => {
              const isActive = active === i;
              return (
                <button key={i} onClick={() => setActive(i)} style={{
                  textAlign: "left", padding: "28px 0",
                  borderBottom: "1px solid var(--rule)",
                  display: "grid", gridTemplateColumns: "60px 1fr 30px",
                  alignItems: "center", gap: 24,
                  color: isActive ? "var(--ink)" : "var(--stone)",
                  transition: "color 0.3s",
                }}>
                  <div className="mono" style={{ fontSize: 11, color: isActive ? "var(--vermillion)" : "var(--stone)" }}>
                    Q.{String(i+1).padStart(2,"0")}
                  </div>
                  <div className="display-italic" style={{
                    fontSize: isActive ? "clamp(24px, 2.4vw, 32px)" : "clamp(20px, 2vw, 26px)",
                    lineHeight: 1.15,
                    transition: "font-size 0.3s",
                  }}>
                    « {t(lang, l.fr, l.en)} »
                  </div>
                  <div className="mono" style={{
                    fontSize: 10, textAlign: "right",
                    color: isActive ? "var(--acid)" : "transparent",
                    transition: "color 0.3s",
                  }}>●</div>
                </button>
              );
            })}
          </div>
        </div>

        <div style={{ marginTop: 120, textAlign: "center" }} data-reveal ref={useReveal()}>
          <div className="display" style={{ fontSize: "clamp(40px, 5vw, 72px)" }}>
            — {t(lang, "NIA a les réponses.", "NIA has the answers.")}
          </div>
        </div>
      </div>

      <style>{`
        @keyframes thinkPulse {
          0%, 100% { opacity: 0.6; transform: translateY(0); }
          50% { opacity: 1; transform: translateY(-4px); }
        }
      `}</style>
    </section>
  );
}
window.CharacterQuestions = CharacterQuestions;
