/* global React, window, palette */
// Visual atoms - striped placeholders standing in for product photography.
// Intentionally abstract: rectangles + stripes only, with monospace labels.

const { useMemo } = React;

/* Реактивный детектор мобильной ширины - на инлайн-стилях media-запросы
   не работают, поэтому ширину отслеживаем через matchMedia. */
function useIsMobile(breakpoint = 820) {
  const query = `(max-width: ${breakpoint}px)`;
  const [isMobile, setIsMobile] = React.useState(
    () => typeof window !== "undefined" && window.matchMedia(query).matches
  );
  React.useEffect(() => {
    const mq = window.matchMedia(query);
    const onChange = e => setIsMobile(e.matches);
    setIsMobile(mq.matches);
    mq.addEventListener("change", onChange);
    return () => mq.removeEventListener("change", onChange);
  }, [query]);
  return isMobile;
}

function hash(str) {
  let h = 0;
  for (let i = 0; i < str.length; i++) h = ((h << 5) - h + str.charCodeAt(i)) | 0;
  return Math.abs(h);
}

/* A flat "case" - coloured rectangle with subtle vertical strap and stripes.
   Keeps shapes restricted to rect + lines + circles. */
function CasePlate({ id, tones = [palette.cognac, palette.brass, palette.bone], ratio = "4/3", label, dense = false }) {
  const seed = useMemo(() => hash(id || "x"), [id]);
  const stripes = (seed % 6) + 4;
  const [body, accent, ground] = tones;
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: ratio, background: ground, overflow: "hidden" }}>
      {/* horizontal stripes for fabric/leather texture */}
      <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ position: "absolute", inset: 0, display: "block" }}>
        {Array.from({ length: stripes }).map((_, i) => (
          <rect key={i} x="0" y={(i + 1) * (100 / (stripes + 1))} width="100" height="0.18" fill="rgba(0,0,0,0.06)" />
        ))}
      </svg>
      {/* case silhouette */}
      <svg width="100%" height="100%" viewBox="0 0 100 75" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, display: "block" }}>
        {/* handle */}
        <rect x="44" y="9" width="12" height="2" fill={accent} opacity="0.85" />
        <rect x="43" y="9" width="1.4" height="4" fill={accent} opacity="0.85" />
        <rect x="55.6" y="9" width="1.4" height="4" fill={accent} opacity="0.85" />
        {/* body */}
        <rect x="14" y="13" width="72" height="52" fill={body} />
        {/* strap */}
        <rect x="14" y="36" width="72" height="3" fill="rgba(0,0,0,0.18)" />
        <rect x="14" y="39" width="72" height="0.6" fill={accent} opacity="0.6" />
        {/* corners - small circles for clasps */}
        <circle cx="20"  cy="38" r="1.6" fill={accent} />
        <circle cx="80"  cy="38" r="1.6" fill={accent} />
        {/* monogram square */}
        {!dense && <rect x="46" y="46" width="8" height="10" fill="rgba(0,0,0,0.08)" />}
      </svg>
      {label && (
        <div style={{ position: "absolute", left: 12, bottom: 10, fontFamily: "'JetBrains Mono', ui-monospace, monospace", fontSize: 10, letterSpacing: "0.08em", color: "rgba(0,0,0,0.5)", textTransform: "uppercase" }}>
          {label}
        </div>
      )}
    </div>
  );
}

/* A "bag" silhouette - slightly taller proportion with a top flap. */
function BagPlate({ id, tones = [palette.navy, palette.brass, palette.bone], ratio = "4/5", label }) {
  const [body, accent, ground] = tones;
  const seed = useMemo(() => hash(id || "x"), [id]);
  const stripes = (seed % 5) + 3;
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: ratio, background: ground, overflow: "hidden" }}>
      <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ position: "absolute", inset: 0, display: "block" }}>
        {Array.from({ length: stripes }).map((_, i) => (
          <rect key={i} x="0" y={(i + 1) * (100 / (stripes + 1))} width="100" height="0.16" fill="rgba(0,0,0,0.05)" />
        ))}
      </svg>
      <svg width="100%" height="100%" viewBox="0 0 100 120" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, display: "block" }}>
        {/* handle loop */}
        <rect x="46" y="14" width="8" height="2" fill={accent} />
        <rect x="45.6" y="14" width="1.2" height="6" fill={accent} />
        <rect x="53.2" y="14" width="1.2" height="6" fill={accent} />
        {/* body */}
        <rect x="20" y="22" width="60" height="80" fill={body} />
        {/* flap */}
        <rect x="20" y="22" width="60" height="22" fill="rgba(0,0,0,0.18)" />
        <rect x="20" y="44" width="60" height="0.6" fill={accent} opacity="0.7" />
        {/* clasp */}
        <rect x="47" y="40" width="6" height="6" fill={accent} />
        {/* side stitch */}
        <rect x="22" y="24" width="0.4" height="76" fill="rgba(255,255,255,0.18)" />
        <rect x="77.6" y="24" width="0.4" height="76" fill="rgba(255,255,255,0.18)" />
      </svg>
      {label && (
        <div style={{ position: "absolute", left: 12, bottom: 10, fontFamily: "'JetBrains Mono', ui-monospace, monospace", fontSize: 10, letterSpacing: "0.08em", color: "rgba(0,0,0,0.5)", textTransform: "uppercase" }}>
          {label}
        </div>
      )}
    </div>
  );
}

/* A "trunk" / chest - wide horizontal body with a lid seam and metal bands.
   Used for storage chests (сундуки). */
function TrunkPlate({ id, tones = [palette.cognac, palette.brass, palette.bone], ratio = "4/3", label }) {
  const seed = useMemo(() => hash(id || "x"), [id]);
  const stripes = (seed % 5) + 3;
  const [body, accent, ground] = tones;
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: ratio, background: ground, overflow: "hidden" }}>
      <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ position: "absolute", inset: 0, display: "block" }}>
        {Array.from({ length: stripes }).map((_, i) => (
          <rect key={i} x="0" y={(i + 1) * (100 / (stripes + 1))} width="100" height="0.18" fill="rgba(0,0,0,0.06)" />
        ))}
      </svg>
      <svg width="100%" height="100%" viewBox="0 0 100 75" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, display: "block" }}>
        {/* body */}
        <rect x="10" y="22" width="80" height="44" fill={body} />
        {/* lid seam */}
        <rect x="10" y="34" width="80" height="0.8" fill="rgba(0,0,0,0.25)" />
        {/* metal bands (vertical) */}
        <rect x="26" y="22" width="1.2" height="44" fill={accent} opacity="0.85" />
        <rect x="72" y="22" width="1.2" height="44" fill={accent} opacity="0.85" />
        {/* corner caps */}
        <rect x="10" y="22" width="3" height="3" fill={accent} />
        <rect x="87" y="22" width="3" height="3" fill={accent} />
        <rect x="10" y="63" width="3" height="3" fill={accent} />
        <rect x="87" y="63" width="3" height="3" fill={accent} />
        {/* lock plate */}
        <rect x="46" y="32" width="8" height="6" fill={accent} />
        <rect x="49" y="34" width="2" height="2" fill="rgba(0,0,0,0.4)" />
        {/* side handle hints */}
        <rect x="6" y="40" width="4" height="1.4" fill={accent} opacity="0.8" />
        <rect x="90" y="40" width="4" height="1.4" fill={accent} opacity="0.8" />
      </svg>
      {label && (
        <div style={{ position: "absolute", left: 12, bottom: 10, fontFamily: "'JetBrains Mono', ui-monospace, monospace", fontSize: 10, letterSpacing: "0.08em", color: "rgba(0,0,0,0.5)", textTransform: "uppercase" }}>
          {label}
        </div>
      )}
    </div>
  );
}

/* A round / cylindrical box (шляпная коробка, круглый футляр). */
function RoundPlate({ id, tones = [palette.cognac, palette.brass, palette.bone], ratio = "4/5", label }) {
  const seed = useMemo(() => hash(id || "x"), [id]);
  const ringOffset = (seed % 4) + 2;
  const [body, accent, ground] = tones;
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: ratio, background: ground, overflow: "hidden" }}>
      <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, display: "block" }}>
        {/* body cylinder - large circle */}
        <circle cx="50" cy="54" r="34" fill={body} />
        {/* inner ring (lid edge) */}
        <circle cx="50" cy="54" r={34 - ringOffset} fill="none" stroke="rgba(0,0,0,0.18)" strokeWidth="0.6" />
        {/* metal strap across top */}
        <rect x="16" y="40" width="68" height="1.6" fill={accent} opacity="0.85" />
        {/* handle on top */}
        <rect x="42" y="14" width="16" height="2" fill={accent} />
        <rect x="42" y="14" width="1.6" height="8" fill={accent} />
        <rect x="56.4" y="14" width="1.6" height="8" fill={accent} />
        {/* monogram dot */}
        <circle cx="50" cy="60" r="3" fill="rgba(0,0,0,0.1)" />
      </svg>
      {label && (
        <div style={{ position: "absolute", left: 12, bottom: 10, fontFamily: "'JetBrains Mono', ui-monospace, monospace", fontSize: 10, letterSpacing: "0.08em", color: "rgba(0,0,0,0.5)", textTransform: "uppercase" }}>
          {label}
        </div>
      )}
    </div>
  );
}

/* A shoe cabinet silhouette - tall rectangle with horizontal drawer seams. */
function ShoeCabinetPlate({ id, tones = [palette.cognac, palette.brass, palette.bone], ratio = "3/4", label }) {
  const seed = useMemo(() => hash(id || "x"), [id]);
  const drawers = (seed % 2) + 3; // 3 or 4 drawers
  const [body, accent, ground] = tones;
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: ratio, background: ground, overflow: "hidden" }}>
      <svg width="100%" height="100%" viewBox="0 0 100 130" preserveAspectRatio="xMidYMid meet" style={{ position: "absolute", inset: 0, display: "block" }}>
        {/* body */}
        <rect x="18" y="10" width="64" height="110" fill={body} />
        {/* drawer seams */}
        {Array.from({ length: drawers }).map((_, i) => {
          const y = 10 + (110 / drawers) * (i + 1);
          return <rect key={i} x="18" y={y - 0.4} width="64" height="0.8" fill="rgba(0,0,0,0.22)" />;
        })}
        {/* drawer pulls */}
        {Array.from({ length: drawers }).map((_, i) => {
          const cy = 10 + (110 / drawers) * (i + 0.5);
          return <rect key={i} x="46" y={cy - 0.8} width="8" height="1.6" fill={accent} />;
        })}
        {/* feet */}
        <rect x="20" y="120" width="6" height="4" fill={accent} opacity="0.7" />
        <rect x="74" y="120" width="6" height="4" fill={accent} opacity="0.7" />
        {/* top edge */}
        <rect x="18" y="10" width="64" height="1.4" fill="rgba(0,0,0,0.18)" />
      </svg>
      {label && (
        <div style={{ position: "absolute", left: 12, bottom: 10, fontFamily: "'JetBrains Mono', ui-monospace, monospace", fontSize: 10, letterSpacing: "0.08em", color: "rgba(0,0,0,0.5)", textTransform: "uppercase" }}>
          {label}
        </div>
      )}
    </div>
  );
}

/* Eyebrow / kicker text */
function Eyebrow({ children, color }) {
  return (
    <div style={{
      fontFamily: "'Manrope', sans-serif",
      fontSize: 11, letterSpacing: "0.22em", textTransform: "uppercase",
      color: color || "rgba(0,0,0,0.55)",
      fontWeight: 500,
    }}>{children}</div>
  );
}

/* A thin horizontal rule used between editorial sections */
function Rule({ color = "rgba(0,0,0,0.12)", style }) {
  return <div style={{ height: 1, background: color, width: "100%", ...style }} />;
}

Object.assign(window, { CasePlate, BagPlate, TrunkPlate, RoundPlate, ShoeCabinetPlate, Eyebrow, Rule, useIsMobile });
