/* global React, useViewport, PageIntro, EditorialCarousel, ThreadBadge,
           SpecificationSection, SpecifierDownloads, EnquiryBlock */
/*
 * PillDetail — Pill, by Derlot Editions. Dwell thread.
 * Sculptural modular leather lounge seating — organic, walk-around "pill"
 * forms for premium lobbies and hospitality interiors. Indoor.
 */

const { useState: useStatePil, useEffect: useEffectPil } = React;

const PIL_SLIDES_BASE = [
  { title: "Pill", image: "images/collections/pill/hero/01.jpg",   pos: "center 60%" },
  { title: "Pill", image: "images/collections/pill/hero/02.jpg",  pos: "center 55%" },
  { title: "Pill", image: "images/collections/pill/hero/03.jpg", pos: "center 55%" },
];
const PIL_SLIDES = (window.IMG && window.IMG.extend) ? window.IMG.extend("pill", "hero", PIL_SLIDES_BASE) : PIL_SLIDES_BASE;

function PillCarousel() {
  const vw = useViewport();
  const isMobile = vw < 760;
  const [i, setI] = useStatePil(0);
  const [paused, setPaused] = useStatePil(false);

  useEffectPil(() => {
    if (paused) return;
    const id = setInterval(() => setI((x) => (x + 1) % PIL_SLIDES.length), 7000);
    return () => clearInterval(id);
  }, [paused]);

  const s = PIL_SLIDES[i];

  return (
    <section
      onMouseEnter={() => setPaused(true)}
      onMouseLeave={() => setPaused(false)}
      style={{ position: "relative", width: "100%", height: isMobile ? "76vh" : "78vh",
        minHeight: isMobile ? 520 : 620, maxHeight: isMobile ? "none" : 820,
        overflow: "hidden", background: "#e7e5df" }}>
      {PIL_SLIDES.map((ss, idx) => (
        <div key={idx} style={{ position: "absolute", inset: 0, opacity: idx === i ? 1 : 0, transition: "opacity 1100ms ease" }}>
          <img src={ss.image} srcSet={window.IMG && window.IMG.srcsetFor(ss.image)} sizes={window.IMG && window.IMG.sizes()} alt={ss.title} loading={idx === 0 ? "eager" : "lazy"}
            style={{ width: "100%", height: "100%", objectFit: "cover", objectPosition: ss.pos || "center", display: "block" }} />
        </div>
      ))}
      <div style={{ position: "absolute", inset: 0,
        background: "linear-gradient(to bottom, transparent 52%, rgba(26,25,23,0.5) 100%)", pointerEvents: "none" }} />
      <div style={{ position: "absolute", left: 0, right: 0, bottom: isMobile ? 84 : 100,
        padding: isMobile ? "0 24px" : "0 32px", textAlign: "center", color: "#f5f3ef" }}>
        <h3 style={{ fontFamily: "var(--font-display, Poppins, sans-serif)", fontWeight: 500,
          fontSize: isMobile ? "clamp(20px, 5vw, 26px)" : "clamp(22px, 1.9vw, 32px)",
          lineHeight: 1.18, letterSpacing: "-0.01em", margin: "0 auto", maxWidth: "26ch", textWrap: "balance" }}>
          {s.title}
        </h3>
      </div>
      <div style={{ position: "absolute", bottom: isMobile ? 36 : 44, left: 0, right: 0, display: "flex", justifyContent: "center", gap: 10 }}>
        {PIL_SLIDES.map((_, idx) => (
          <button key={idx} onClick={() => setI(idx)} aria-label={`Show slide ${idx + 1}`}
            style={{ width: idx === i ? 24 : 6, height: 2,
              background: idx === i ? "rgba(245,243,239,0.95)" : "rgba(245,243,239,0.45)",
              border: "none", cursor: "pointer", padding: 0, transition: "width 320ms ease, background 320ms ease" }} />
        ))}
      </div>
    </section>
  );
}

function PillDetail({ setScreen }) {
  const vw = useViewport();
  const isMobile = vw < 720;
  return (
    <>
      <PillCarousel />
      <PageIntro
        eyebrow="Pill · Derlot Editions, 2019"
        title="Sculptural modular seating that flows through a room."
        body="Pill is a modular lounge collection of soft, organic forms — a family of leather-wrapped seats and benches that link into long, walk-around runs. Built to anchor premium lobbies and hospitality interiors, Pill brings a relaxed, sculptural presence to open floors, gathering people without enclosing them."
        compact noTopPad
      />

      <section style={{ padding: isMobile ? "56px 20px" : "96px max(24px, 3vw)", maxWidth: 1440, margin: "0 auto" }}>
        <div style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "1fr 1fr", gap: isMobile ? 32 : 48 }}>
          <EditorialCarousel
            slides={[
              { src: "images/collections/pill/cover.jpg",    alt: "Pill modular benches anchoring a marble lobby", caption: "01 · In service" },
              { src: "images/collections/pill/hero/03.jpg", alt: "Pill benches by a reception wall",              caption: "02 · Form" },
              { src: "images/collections/pill/hero/01.jpg",     alt: "Pill running the length of a corporate foyer",  caption: "03 · Run" },
            ]}
            aspect="3/4"
          />
          <div style={{ alignSelf: "center" }}>
            <div style={{ fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "#6c6862", fontWeight: 500, marginBottom: 14 }}>Dwell · Indoor</div>
            <h2 style={{ fontWeight: 500, fontSize: isMobile ? "clamp(22px, 5.5vw, 28px)" : "clamp(24px, 2.2vw, 32px)", lineHeight: 1.12, letterSpacing: "-0.01em", color: "#1a1917", margin: "0 0 16px 0", maxWidth: "22ch" }}>
              Soft islands for an open floor.
            </h2>
            <p style={{ fontSize: isMobile ? 15 : 16, lineHeight: 1.55, color: "#1a1917", maxWidth: "46ch", margin: 0, fontWeight: 500 }}>
              Specified as single modules or linked into sweeping serpentine runs, Pill draws relaxed islands of seating out of large lobbies and lounges. Its rounded, walk-around geometry softens hard stone-and-glass interiors and keeps sightlines open, while the leather upholstery wears the daily traffic of a premium foyer.
            </p>
          </div>
        </div>
      </section>

      {/* Three features */}
      <section style={{ padding: isMobile ? "64px 20px" : "120px max(24px, 3vw)", maxWidth: 1440, margin: "0 auto" }}>
        <div style={{ maxWidth: 720, marginBottom: isMobile ? 40 : 64 }}>
          <div style={{ fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "#6c6862", fontWeight: 500, marginBottom: 14 }}>What Pill does</div>
          <h2 style={{ fontFamily: "var(--font-display, Poppins, sans-serif)", fontWeight: 500, fontSize: isMobile ? "clamp(24px, 6vw, 30px)" : "clamp(26px, 2.6vw, 38px)", lineHeight: 1.12, letterSpacing: "-0.015em", color: "#1a1917", margin: 0, maxWidth: "26ch", textWrap: "balance" }}>
            Three things that define Pill.
          </h2>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "repeat(3, 1fr)", gap: isMobile ? 48 : 40 }}>
          {[
            { no: "01", kicker: "Organic form", title: "Soft, sculptural, walk-around.",
              body: "Pill's rounded silhouette reads as a single sculptural object rather than a row of furniture. There is no front or back — it invites approach from every side, giving an open lobby a calm, gathered centre.",
              image: "images/collections/pill/cover.jpg", alt: "Pill — organic walk-around form", pos: "center 55%" },
            { no: "02", kicker: "Modular runs", title: "From a single seat to a serpentine bench.",
              body: "Built as modules, Pill links into long curved runs that follow the architecture — wrapping columns, lining circulation and scaling from an intimate perch to a foyer-length bench.",
              image: "images/collections/pill/hero/01.jpg", alt: "Pill — modular serpentine run", pos: "center 60%" },
            { no: "03", kicker: "Leather, made to last", title: "Premium leather for high-traffic lobbies.",
              body: "Wrapped in full-grain leather over a robust foam-and-frame build, Pill is specified for the daily wear of premium commercial and hospitality foyers — softening hard interiors while standing up to constant use.",
              image: "images/collections/pill/hero/03.jpg", alt: "Pill — leather detail", pos: "center 50%" },
          ].map((f) => (
            <div key={f.no}>
              <div style={{ aspectRatio: "4/5", background: "#e4e2da", overflow: "hidden", marginBottom: isMobile ? 18 : 24 }}>
                <img src={f.image} srcSet={window.IMG && window.IMG.srcsetFor(f.image)} sizes={window.IMG && window.IMG.sizes()} alt={f.alt} loading="lazy" style={{ width: "100%", height: "100%", objectFit: "cover", objectPosition: f.pos || "center", display: "block" }} />
              </div>
              <div style={{ display: "flex", alignItems: "baseline", gap: 14, marginBottom: 12 }}>
                <div style={{ fontSize: 11, color: "#6c6862", fontWeight: 500, letterSpacing: "0.08em" }}>{f.no}</div>
                <div style={{ height: "0.5px", flex: 1, background: "rgba(26,25,23,0.25)" }} />
                <div style={{ fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", color: "#6c6862", fontWeight: 500 }}>{f.kicker}</div>
              </div>
              <h3 style={{ fontFamily: "var(--font-display, Poppins, sans-serif)", fontWeight: 500, fontSize: isMobile ? 19 : 20, lineHeight: 1.22, letterSpacing: "-0.01em", color: "#1a1917", margin: "0 0 10px 0", maxWidth: "20ch" }}>
                {f.title}
              </h3>
              <p style={{ fontSize: 14, lineHeight: 1.6, color: "#6c6862", margin: 0, maxWidth: "38ch" }}>{f.body}</p>
            </div>
          ))}
        </div>
      </section>

      {/* Project in use */}
      <section style={{ padding: isMobile ? "64px 20px" : "120px max(24px, 3vw)", maxWidth: 1440, margin: "0 auto" }}>
        <div style={{ fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "#6c6862", fontWeight: 500, marginBottom: isMobile ? 24 : 32 }}>Project in use</div>
        <div
          onClick={() => setScreen && setScreen("project-141-walker")}
          role="link" tabIndex={0}
          onKeyDown={(e) => { if (e.key === "Enter" && setScreen) setScreen("project-141-walker"); }}
          style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "1.4fr 1fr", gap: isMobile ? 24 : 48, alignItems: "center", cursor: "pointer", maxWidth: 1100 }}>
          <div style={{ aspectRatio: "16/10", background: "#e4e2da", overflow: "hidden" }}>
            <img src="images/collections/pill/hero/01.jpg" alt="Pill at 141 Walker Street, North Sydney" loading="lazy"
              style={{ width: "100%", height: "100%", objectFit: "cover", objectPosition: "center 60%", display: "block" }} />
          </div>
          <div>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 16, borderTop: "0.5px solid rgba(26,25,23,0.25)", paddingTop: 14 }}>
              <div style={{ fontFamily: "var(--font-display, Poppins, sans-serif)", fontSize: isMobile ? 22 : 26, color: "#1a1917", fontWeight: 500, letterSpacing: "-0.01em" }}>141 Walker Street</div>
              <div style={{ fontSize: 12, color: "#6c6862", letterSpacing: "0.04em" }}>North Sydney · 2019</div>
            </div>
            <div style={{ fontSize: 13, color: "#6c6862", marginTop: 8, letterSpacing: "0.04em" }}>Corporate · Incorp Solutions</div>
            <p style={{ fontSize: isMobile ? 14 : 15, lineHeight: 1.6, color: "#1a1917", margin: "18px 0 0 0", maxWidth: "40ch" }}>
              Pill (PIL-05) anchors the sleek reception lounge against dark marble and stone in the North Sydney foyer.
            </p>
            <div style={{ fontSize: 12, letterSpacing: "0.12em", textTransform: "uppercase", color: "#1a1917", fontWeight: 500, marginTop: 22, display: "inline-block", borderBottom: "0.5px solid #1a1917", paddingBottom: 4 }}>
              View project →
            </div>
          </div>
        </div>
      </section>

      <SpecificationSection
        headline="One module. Endless runs."
        meta={[
          ["Design", "Derlot Editions, 2019"],
          ["Sectors", "Corporate · Hospitality · Civic"],
          ["Environment", "Indoor"],
          ["Warranty", "5 year structural"],
          ["Manufactured", "Australia"],
        ]}
        materials="Full-grain leather upholstery over moulded foam on an internal timber and steel frame. Modules link to form straight or serpentine runs."
        groups={[{
          label: "Modular lounge",
          items: [
            { sku: "PIL-05", name: "Pill module", dims: "Linking module — see specification" },
          ],
        }]}
        finishHeadline="Full-grain leather as standard, in a range of tones. Custom upholstery available on request."
      />

      <SpecifierDownloads collection="Pill" />

      <section style={{ padding: isMobile ? "56px 20px" : "96px max(24px, 3vw)", maxWidth: 1440, margin: "0 auto", borderTop: "0.5px solid rgba(26,25,23,0.18)" }}>
        <div style={{ fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "#6c6862", fontWeight: 500, marginBottom: 20 }}>Related collections</div>
        <div style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "repeat(3, 1fr)", gap: isMobile ? 32 : 32 }}>
          {(window.COLLECTIONS || []).filter(c => ["caterpillar", "seed", "pinto"].includes(c.slug)).map((c) => (
            <div key={c.slug} onClick={() => setScreen && setScreen(c.slug)} style={{ cursor: setScreen ? "pointer" : "default" }}>
              <div style={{ aspectRatio: "4/5", background: "#e4e2da", overflow: "hidden" }}>
                {c.image && <img src={c.image} srcSet={window.IMG && window.IMG.srcsetFor(c.image)} sizes={window.IMG && window.IMG.sizes()} alt={c.name} loading="lazy" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />}
              </div>
              <div style={{ height: "0.5px", background: "rgba(26,25,23,0.18)", marginTop: 14 }} />
              <div style={{ fontSize: 16, color: "#1a1917", marginTop: 12 }}>{c.name}</div>
              <div style={{ marginTop: 10, display: "flex", flexWrap: "wrap", rowGap: 6 }}>
                {c.threads.map(tk => <ThreadBadge key={tk} threadKey={tk} />)}
              </div>
            </div>
          ))}
        </div>
      </section>

      <EnquiryBlock collection="Pill" />
    </>
  );
}

window.PillDetail = PillDetail;
