/* global React */

/* =========================================================================
   SHARED — data, theme, nav, footer, mascot
   Loaded BEFORE every page-specific script.
   All exports live on window for cross-script access.
   ========================================================================= */

/* ----- Content ----- */
const BOOKING_EMAIL = "williampayne831@gmail.com";

const SOCIALS = [
  { label: "Instagram",  url: "https://www.instagram.com/itsjustwoodi", handle: "@itsjustwoodi" },
  { label: "SoundCloud", url: "https://soundcloud.com/dj_woodi",         handle: "@dj_woodi" },
  { label: "YouTube",    url: "https://www.youtube.com/@DJWoodi",        handle: "@DJWoodi" },
];

const SOCIAL_ICONS = {
  Instagram: <path d="M12 2.16c3.2 0 3.58 0 4.85.07 1.17.05 1.8.25 2.23.42.56.22.96.48 1.38.9.42.42.68.82.9 1.38.17.42.37 1.06.42 2.23.07 1.26.07 1.64.07 4.84s0 3.58-.07 4.85c-.05 1.17-.25 1.8-.42 2.23-.22.56-.48.96-.9 1.38-.42.42-.82.68-1.38.9-.42.17-1.06.37-2.23.42-1.26.07-1.64.07-4.85.07s-3.58 0-4.84-.07c-1.17-.05-1.81-.25-2.23-.42a3.72 3.72 0 0 1-1.38-.9 3.72 3.72 0 0 1-.9-1.38c-.17-.42-.37-1.06-.42-2.23C2.16 15.58 2.16 15.2 2.16 12s0-3.58.07-4.85c.05-1.17.25-1.81.42-2.23.22-.56.48-.96.9-1.38a3.72 3.72 0 0 1 1.38-.9c.42-.17 1.06-.37 2.23-.42C8.42 2.16 8.8 2.16 12 2.16zM12 0C8.74 0 8.33 0 7.05.07 5.78.13 4.9.33 4.14.63a5.88 5.88 0 0 0-2.13 1.38A5.88 5.88 0 0 0 .63 4.14c-.3.76-.5 1.64-.56 2.91C0 8.33 0 8.74 0 12s0 3.67.07 4.95c.06 1.27.26 2.15.56 2.91.31.81.74 1.5 1.38 2.13.63.64 1.32 1.07 2.13 1.38.76.3 1.64.5 2.91.56C8.33 24 8.74 24 12 24s3.67 0 4.95-.07c1.27-.06 2.15-.26 2.91-.56a6.13 6.13 0 0 0 2.13-1.38 6.13 6.13 0 0 0 1.38-2.13c.3-.76.5-1.64.56-2.91.07-1.28.07-1.69.07-4.95s0-3.67-.07-4.95c-.06-1.27-.26-2.15-.56-2.91a5.88 5.88 0 0 0-1.38-2.13A5.88 5.88 0 0 0 19.86.63c-.76-.3-1.64-.5-2.91-.56C15.67 0 15.26 0 12 0zm0 5.84a6.16 6.16 0 1 0 0 12.32 6.16 6.16 0 0 0 0-12.32zM12 16a4 4 0 1 1 0-8 4 4 0 0 1 0 8zm6.4-11.85a1.44 1.44 0 1 0 0 2.88 1.44 1.44 0 0 0 0-2.88z"/>,
  SoundCloud: <path d="M.45 13.66c-.07 0-.13.06-.14.14l-.18 1.59.18 1.55c.01.08.07.13.14.13.06 0 .12-.05.13-.13l.21-1.55-.21-1.59c-.01-.08-.07-.14-.13-.14zm.95-1.04c-.08 0-.14.06-.15.14L1 15.39l.25 2.5c.01.09.07.15.15.15.08 0 .14-.06.15-.14l.28-2.51-.28-2.62c-.01-.08-.07-.14-.15-.14zm1.04-.5a.18.18 0 0 0-.18.17L2 15.39l.26 3.07c.01.09.08.16.18.16.09 0 .17-.07.18-.16l.3-3.07-.3-3.1a.18.18 0 0 0-.18-.17zm1.07-.36a.21.21 0 0 0-.2.19l-.23 3.44.23 3.31a.2.2 0 0 0 .4 0l.26-3.31-.26-3.44a.21.21 0 0 0-.2-.19zm1.09-.1a.23.23 0 0 0-.22.21l-.22 3.52.22 3.41a.22.22 0 0 0 .44 0l.25-3.41-.25-3.52a.23.23 0 0 0-.22-.21zm1.2.13a.25.25 0 0 0-.24.23l-.2 3.39.2 3.38a.24.24 0 0 0 .48 0l.23-3.38-.23-3.39a.25.25 0 0 0-.24-.23zm1.11-.5a.26.26 0 0 0-.26.25l-.19 3.85.19 3.34a.26.26 0 0 0 .52 0l.21-3.34-.21-3.85a.26.26 0 0 0-.26-.25zm1.13-.85a.28.28 0 0 0-.28.27l-.17 4.71.17 3.3a.28.28 0 0 0 .56 0l.2-3.3-.2-4.71a.28.28 0 0 0-.28-.27zm1.15-.31a.3.3 0 0 0-.3.28l-.16 5.01.16 3.27a.3.3 0 0 0 .6 0l.18-3.27-.18-5.01a.3.3 0 0 0-.3-.28zm1.15-.32a.31.31 0 0 0-.31.3l-.15 5.31.15 3.25a.31.31 0 0 0 .62 0l.17-3.25-.17-5.31a.31.31 0 0 0-.31-.3zm1.17-.43a.33.33 0 0 0-.33.31l-.14 5.74.14 3.2a.33.33 0 0 0 .66 0l.15-3.2-.15-5.74a.33.33 0 0 0-.33-.31zm1.18-.13a.35.35 0 0 0-.35.34l-.13 5.87.13 3.18a.35.35 0 0 0 .7 0l.14-3.18-.14-5.87a.35.35 0 0 0-.35-.34zm1.27.06a.36.36 0 0 0-.37.36l-.12 5.81.12 3.15a.37.37 0 0 0 .74 0l.13-3.15-.13-5.81a.36.36 0 0 0-.37-.36zm1.51-.97c-.21 0-.42.05-.6.14a.41.41 0 0 0-.21.36l-.1 6.64.1 3.12c0 .22.19.4.41.4h7.06a3.7 3.7 0 0 0 0-7.4c-.55 0-1.07.12-1.55.33-.32-2.05-2.08-3.63-4.2-3.63-.36 0-.7.04-1.02.13z"/>,
  YouTube: <path d="M23.5 6.5a3 3 0 0 0-2.1-2.1C19.6 4 12 4 12 4s-7.6 0-9.4.4A3 3 0 0 0 .5 6.5C.1 8.4.1 12 .1 12s0 3.6.4 5.5a3 3 0 0 0 2.1 2.1C4.4 20 12 20 12 20s7.6 0 9.4-.4a3 3 0 0 0 2.1-2.1c.4-1.9.4-5.5.4-5.5s0-3.6-.4-5.5zM9.6 15.6V8.4l6.4 3.6-6.4 3.6z"/>,
  Email: <path d="M2 4h20v16H2V4zm2 2v.5l8 5 8-5V6H4zm0 2.7V18h16V8.7l-8 5-8-5z" />,
};

const MIXES = [
  { id: "TX-003", title: "R&B at ETC.", sub: "Live set · all R&B",
    date: "2026", bpm: "95", tag: "R&B · Live",
    scUrl: "https://soundcloud.com/dj_woodi/etc_rnb_mix_2026" },
  { id: "TX-002", title: "Truly G.O.M.D.", sub: "Spandau Ballet × J. Cole · house flip",
    date: "2025", bpm: "122", tag: "House · Flip",
    scUrl: "https://soundcloud.com/dj_woodi/truly-gomd-spandau-ballet-x-j-cole-house-flipdj-woodi" },
  { id: "TX-001", title: "Loaded", sub: "Lil Uzi · techno edit",
    date: "2025", bpm: "128", tag: "Techno · Edit",
    scUrl: "https://soundcloud.com/dj_woodi/loaded-lil-uzi-techno-edit" },
];

const VIDEOS = [
  { kind: "youtube",   id: "IhqKKuIWUtw", title: "Live transmission · 01", caption: "Live in the booth" },
  { kind: "youtube",   id: "jBMmpVA3F2s", title: "Live transmission · 02", caption: "Live in the booth" },
  // Add Instagram clips like:
  // { kind: "instagram", id: "SHORTCODE",   title: "...", caption: "Instagram reel" },
];

const VENUES = [
  "Everything Bar", "ETC.", "The Dime",
  "Hubbard Inn", "Penthouse", "The Loft",
];

const ALONGSIDE = [
  { name: "Timmy V",          role: "Played alongside" },
  { name: "FourthCo.",        role: "Support set" },
  { name: "Vince Lazarus",    role: "Played alongside" },
  { name: "DJ Bern",            role: "Support set" },
  { name: "Midnight Grooves", role: "Played alongside" },
];

const GIGS = [
  { date: "Jun 06", city: "Chicago", venue: "South Shore Golf Club", status: "on-sale" },
  { date: "TBA",    city: "—",       venue: "Next show loading…",    status: "tba" },
  { date: "TBA",    city: "—",       venue: "Next show loading…",    status: "tba" },
];

const GENRES = ["House", "Disco", "Hip-Hop", "R&B", "Club", "Miami Bass"];

/* ----- Theme presets (localStorage-backed) ----- */
const THEMES = {
  "Burnt Sienna":  ["#b3471d", "#842f0f", "#f6dccc", "#842f0f"],
  "Forest":        ["#2e6042", "#1c3f2a", "#d9e4dc", "#1c3f2a"],
  "Cobalt":        ["#1f3ba8", "#102270", "#d4dbf2", "#102270"],
  "Wine":          ["#7a1f2e", "#4d121d", "#f0d5da", "#4d121d"],
  "Plum":          ["#6e1f4a", "#4a132e", "#f5dce4", "#9c1834"],
  "Charcoal":      ["#2a2a2a", "#0d0d0d", "#dcdcdc", "#1a1a1a"],
};

function getStoredAccent() {
  try { return localStorage.getItem("djw-accent") || "Burnt Sienna"; }
  catch { return "Burnt Sienna"; }
}
function setStoredAccent(name) {
  try { localStorage.setItem("djw-accent", name); } catch {}
}
function applyTheme(name) {
  const t = THEMES[name] || THEMES["Burnt Sienna"];
  const r = document.documentElement;
  r.style.setProperty("--plum",      t[0]);
  r.style.setProperty("--plum-deep", t[1]);
  r.style.setProperty("--plum-soft", t[2]);
  r.style.setProperty("--plum-text", t[3]);
}
function useAccent() {
  const { useState, useEffect } = React;
  const [accent, setAccent] = useState(getStoredAccent);
  useEffect(() => {
    applyTheme(accent);
    setStoredAccent(accent);
  }, [accent]);
  return [accent, setAccent];
}
// Apply immediately on load (before React mounts) to avoid flash:
applyTheme(getStoredAccent());

/* =========================================================================
   PAGE SHELL — common nav + footer
   ========================================================================= */
function TopNav({ current }) {
  const items = [
    ["/",        "Home"],
    ["/about",   "About"],
    ["/music",   "Music"],
    ["/video",   "Video"],
    ["/tour",    "Tour"],
    ["/friends", "Friends"],
  ];
  return (
    <nav className="topnav" data-screen-label="nav">
      <a href="/" className="topnav-mark">DJ WOODI</a>
      <div className="topnav-links">
        {items.slice(1).map(([href, label]) => (
          <a key={href} href={href}
             className={current === label.toLowerCase() ? "is-current" : ""}>
            {label}
          </a>
        ))}
      </div>
      <a href="/book" className="topnav-cta">Book</a>
    </nav>
  );
}

function Mascot() {
  return (
    <svg className="mascot" viewBox="0 0 96 96" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      <g fill="none" stroke="#141414" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
        <path d="M30 22 L66 22 L62 14 L34 14 Z" fill="#141414" />
        <rect x="28" y="22" width="40" height="3" fill="#141414" />
        <ellipse cx="48" cy="34" rx="14" ry="13" fill="var(--bg)" />
        <rect x="34" y="32" width="11" height="5" rx="1" fill="#141414" />
        <rect x="51" y="32" width="11" height="5" rx="1" fill="#141414" />
        <line x1="45" y1="34.5" x2="51" y2="34.5" />
        <path d="M42 41 Q48 45 54 41" />
        <path d="M30 30 Q30 18 48 18 Q66 18 66 30" />
        <ellipse cx="30" cy="33" rx="4" ry="6" fill="#141414" />
        <ellipse cx="66" cy="33" rx="4" ry="6" fill="#141414" />
        <path d="M30 60 L30 80 L66 80 L66 60 Q66 50 58 47 L38 47 Q30 50 30 60 Z" fill="#141414" />
        <path d="M30 60 Q22 62 18 70" stroke="#141414" />
        <path d="M66 60 Q74 62 78 70" stroke="#141414" />
        <circle cx="20" cy="74" r="5" fill="var(--plum)" />
        <circle cx="20" cy="74" r="1.4" fill="var(--bg)" />
        <circle cx="76" cy="74" r="5" fill="var(--plum)" />
        <circle cx="76" cy="74" r="1.4" fill="var(--bg)" />
        <path d="M70 26 L70 18 L78 16 L78 24" />
        <ellipse cx="68" cy="26" rx="2" ry="1.6" fill="#141414" />
        <ellipse cx="76" cy="24" rx="2" ry="1.6" fill="#141414" />
      </g>
    </svg>
  );
}

function Footer() {
  return (
    <footer className="footer" data-screen-label="footer">
      <div className="footer-wrap">
        <div className="f-mark">
          DJ WOODI
          <span className="f-mark-sub">Chicago · House DJ · 2026</span>
        </div>

        <Mascot />

        <div className="socials" aria-label="social links">
          {SOCIALS.map(s => (
            <a key={s.label} href={s.url} target="_blank" rel="noopener"
               aria-label={s.label} title={s.handle}>
              <svg viewBox="0 0 24 24" aria-hidden="true">{SOCIAL_ICONS[s.label]}</svg>
            </a>
          ))}
          <a href={`mailto:${BOOKING_EMAIL}`} aria-label="Email" title={BOOKING_EMAIL}>
            <svg viewBox="0 0 24 24" aria-hidden="true">{SOCIAL_ICONS.Email}</svg>
          </a>
        </div>
      </div>

      <div className="footer-bottom">
        <span>© 2026 DJ Woodi · All frequencies reserved</span>
        <span>Booking · <a href={`mailto:${BOOKING_EMAIL}`}>{BOOKING_EMAIL}</a></span>
      </div>
    </footer>
  );
}

/* =========================================================================
   THEME PICKER (small bottom-right widget)
   ========================================================================= */
function ThemePicker() {
  const { useState } = React;
  const [accent, setAccent] = useAccent();
  const [open, setOpen] = useState(false);
  return (
    <div className={"theme-picker " + (open ? "is-open" : "")}>
      <button className="theme-picker-toggle" onClick={() => setOpen(o => !o)}
              aria-label="Change accent color"
              style={{ background: THEMES[accent]?.[0] }}>
        <span className="sr-only">Theme</span>
      </button>
      {open && (
        <div className="theme-picker-panel">
          <div className="theme-picker-label">Accent</div>
          <div className="theme-picker-swatches">
            {Object.keys(THEMES).map(name => (
              <button
                key={name}
                title={name}
                className={"swatch " + (name === accent ? "on" : "")}
                style={{ background: THEMES[name][0] }}
                onClick={() => { setAccent(name); }}
              />
            ))}
          </div>
        </div>
      )}
    </div>
  );
}

/* =========================================================================
   PAGE WRAPPER — convenience for page jsx files
   ========================================================================= */
function Page({ current, children }) {
  return (
    <div data-screen-label={`page · ${current}`}>
      <TopNav current={current} />
      {children}
      <Footer />
      <ThemePicker />
    </div>
  );
}

/* expose to window for per-page scripts */
Object.assign(window, {
  // data
  BOOKING_EMAIL, SOCIALS, SOCIAL_ICONS, MIXES, VIDEOS,
  VENUES, ALONGSIDE, GIGS, GENRES, THEMES,
  // utilities
  applyTheme, useAccent,
  // components
  TopNav, Footer, Mascot, ThemePicker, Page,
});
