/* Components for Deinvtuber site
   Exposes: Topbar, Marquee, Hero, Socials, BlogSection, Lore, Shop, Support, Footer, SocialIcons
*/

const { useState, useEffect, useRef } = React;

// ── icons ────────────────────────────────────────────────────────────
const Icons = {
  Twitch: (props) =>
    <svg viewBox="0 0 24 24" {...props}><path d="M4 2L2 6.5V19h4v3h2.5L11 19h4l5-5V2H4zm14 11l-3 3h-4l-3 3v-3H5V4h13v9zm-3-7h-2v5h2V6zm-5 0H8v5h2V6z" /></svg>,

  YouTube: (props) =>
    <svg viewBox="0 0 24 24" {...props}><path d="M21.6 7.2a2.5 2.5 0 0 0-1.7-1.7C18.3 5 12 5 12 5s-6.3 0-7.9.5A2.5 2.5 0 0 0 2.4 7.2C2 8.8 2 12 2 12s0 3.2.4 4.8a2.5 2.5 0 0 0 1.7 1.7c1.6.5 7.9.5 7.9.5s6.3 0 7.9-.5a2.5 2.5 0 0 0 1.7-1.7c.4-1.6.4-4.8.4-4.8s0-3.2-.4-4.8zM10 15V9l5 3-5 3z" /></svg>,

  TikTok: (props) =>
    <svg viewBox="0 0 24 24" {...props}><path d="M19.5 8.5a7 7 0 0 1-4-1.3v6.6a5.6 5.6 0 1 1-5.6-5.6c.3 0 .6 0 .9.1v2.9a2.7 2.7 0 1 0 1.9 2.6V2h2.8a4 4 0 0 0 4 4v2.5z" /></svg>,

  Instagram: (props) =>
    <svg viewBox="0 0 24 24" {...props}><path d="M12 2.2c3.2 0 3.6 0 4.8.1 1.2.1 1.8.3 2.2.4.6.2 1 .5 1.4.9.4.4.7.8.9 1.4.2.4.4 1 .4 2.2.1 1.2.1 1.6.1 4.8s0 3.6-.1 4.8c-.1 1.2-.3 1.8-.4 2.2a3.7 3.7 0 0 1-.9 1.4 3.7 3.7 0 0 1-1.4.9c-.4.2-1 .4-2.2.4-1.2.1-1.6.1-4.8.1s-3.6 0-4.8-.1a6 6 0 0 1-2.2-.4 3.7 3.7 0 0 1-1.4-.9 3.7 3.7 0 0 1-.9-1.4c-.2-.4-.4-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5.2 0c-1 0-1.8-.9-1.8-2s.8-2 1.8-2 1.8.9 1.8 2-.8 2-1.8 2z" /></svg>,

  Cart: (props) =>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" {...props}><path d="M3 4h2l2.7 13.4a2 2 0 0 0 2 1.6h7.7a2 2 0 0 0 2-1.5L21 8H6" /><circle cx="9" cy="21" r="1.5" /><circle cx="18" cy="21" r="1.5" /></svg>,

  Heart: (props) =>
    <svg viewBox="0 0 24 24" fill="currentColor" {...props}><path d="M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6c-2.5 4.5-9.5 9-9.5 9z" /></svg>,

  Arrow: (props) =>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" {...props}><path d="M5 12h14M13 5l7 7-7 7" /></svg>

};

// ── topbar ──────────────────────────────────────────────────────────
function Topbar({ live, onToggleLive }) {
  const [menuOpen, setMenuOpen] = useState(false);

  return (
    <header className="topbar" style={{ position: 'relative' }}>
      <div className="shell topbar-inner">
        <a href="#top" className="brand">
          <div className="brand-mark">D</div>
          <span>Deinvtuber<span style={{ color: 'var(--accent-deep)' }}>.</span></span>
        </a>
        <nav className="nav">
          <a href="#featured">Último Video</a>
          <a href="#sets">Sets de Pokémon</a>
          <a href="game.html">Juegos</a>
          <a href="#story">Lore</a>
          <a href="#support">Donaciones</a>
        </nav>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <button
            className="nav-hamburger"
            type="button"
            aria-label="Abrir menú"
            onClick={() => setMenuOpen(!menuOpen)}
          >
            {menuOpen
              ? <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M18 6L6 18M6 6l12 12" /></svg>
              : <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round"><path d="M3 8h18M3 16h18" /></svg>
            }
          </button>
          <button
            className="live-pill"
            data-on={live ? "true" : "false"}
            onClick={onToggleLive}
            type="button"
            title={live ? "Streaming now" : "Currently offline"}>
            <span className="live-dot"></span>
            {live ? "Live now" : "Offline"}
          </button>
        </div>
      </div>
      {menuOpen && (
        <nav className="mobile-menu">
          <a href="#featured" onClick={() => setMenuOpen(false)}>Último Video</a>
          <a href="#sets" onClick={() => setMenuOpen(false)}>Sets de Pokémon</a>
          <a href="game.html" onClick={() => setMenuOpen(false)}>Juegos</a>
          <a href="#story" onClick={() => setMenuOpen(false)}>Lore</a>
          <a href="#support" onClick={() => setMenuOpen(false)}>Donaciones</a>
        </nav>
      )}
    </header>);
}

// ── marquee ─────────────────────────────────────────────────────────
function Marquee() {
  const items = [
    <><span className="accent">★</span> Última Booster Box de Perfect Order</>,
    <><span className="orange">⚡</span> Reto: Abriendo Sobres de Perfect Order</>,
    <><span className="accent">▶</span> Abrimos Booster Box de One Piece OP 15</>,
  ];

  const cells = [...items, ...items, ...items];
  return (
    <div className="marquee" aria-hidden="true">
      <div className="marquee-track">
        {cells.map((c, i) => <span key={i}>{c}</span>)}
      </div>
    </div>);

}

// ── chaos rising countdown ───────────────────────────────────────────
function ChaosRisingCountdown() {
  const TARGET = new Date('2026-05-22T00:00:00');

  function getTimeLeft() {
    const diff = TARGET - new Date();
    if (diff <= 0) return null;
    return {
      d: Math.floor(diff / 86400000),
      h: Math.floor((diff % 86400000) / 3600000),
      m: Math.floor((diff % 3600000) / 60000),
      s: Math.floor((diff % 60000) / 1000),
    };
  }

  const [time, setTime] = useState(getTimeLeft());

  useEffect(() => {
    if (!getTimeLeft()) return;
    const timer = setInterval(() => {
      const t = getTimeLeft();
      setTime(t);
      if (!t) clearInterval(timer);
    }, 1000);
    return () => clearInterval(timer);
  }, []);

  const pad = n => String(n).padStart(2, '0');

  return (
    <span className="hero-eyebrow" style={{ fontSize: '1.05rem', padding: '8px 16px', gap: 12 }}>
      <span className="live-dot" style={{ boxShadow: '0 0 0 2px var(--ink)', width: 10, height: 10 }}></span>
      ESTRENO: CHAOS RISING —&nbsp;
      {time
        ? <>{pad(time.d)}d {pad(time.h)}h {pad(time.m)}m <span style={{ color: 'var(--accent)' }}>{pad(time.s)}s</span></>
        : <>¡YA DISPONIBLE!</>
      }
    </span>
  );
}

// ── hero ────────────────────────────────────────────────────────────
function Hero({ live }) {
  return (
    <section className="hero" id="top">
      <div className="hero-bg">
        <div className="grid-bg"></div>
        <div className="blob b1"></div>
        <div className="blob b2"></div>
      </div>
      <div className="shell hero-grid">
        <div>
          <ChaosRisingCountdown />
          <h1 className="hero-name">
            <span className="stamp">DEIN</span><br />
            <span className="stamp alt">VTUBER</span>
          </h1>
          <p className="hero-tag">
            Coleccionando cartas, explorando mundos y creando historias.
            Accede a mis redes y sé parte de la comunidad.
          </p>
          <div className="hero-cta">
            <a href="#featured" className="btn secondary">
              <Icons.YouTube width="18" height="18" /> Canal de TCG
            </a>
            <a
              href="#featured"
              className="btn"
              onClick={() => window.dispatchEvent(new CustomEvent('dein:channel', { detail: 'gaming' }))}
            >
              <svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor">
                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z" />
              </svg> Canal de Gaming
            </a>
            <a href="#support" className="btn ghost">
              <Icons.Heart width="16" height="16" /> Apoya en Ko-fi
            </a>
          </div>
          <div className="hero-meta" style={{ marginTop: 32 }}>
          </div>
        </div>

        <div className="hero-character">
          <div className="frame"></div>
          <div className="hero-card-stack left">
            <div className="tcg-card-mini">
              <div className="art"></div>
              <div className="name">Mew ex</div>
              <div className="hp">HP 280</div>
            </div>
          </div>
          <div className="hero-card-stack right">
            <div className="tcg-card-mini gold">
              <div className="art"></div>
              <div className="name">Charizard</div>
              <div className="hp">HP 330</div>
            </div>
          </div>
          <img src="assets/dein-point.webp" alt="Dein, the streamer, pointing forward" width="2055" height="2572" fetchpriority="high" />
        </div>
      </div>
    </section>);

}

// ── socials ─────────────────────────────────────────────────────────
function Socials() {
  const socials = [
    { p: 'youtube', name: 'YouTube', handle: '@DeinVTuberTCG', stat: '8,114', lbl: 'subscribers', Icon: Icons.YouTube, href: 'https://www.youtube.com/@Deinvtubertcg' },
    { p: 'tiktok', name: 'TikTok', handle: '@DeinVTuberTCG', stat: '1238', lbl: 'followers', Icon: Icons.TikTok, href: 'https://www.tiktok.com/@deinvtubertcg' },
    { p: 'youtube', name: 'YouTube', handle: '@DeinVTuber', stat: '0', lbl: 'subscribers', Icon: Icons.YouTube, href: 'https://www.youtube.com/@Deinvtuber' },
    { p: 'tiktok', name: 'TikTok', handle: '@DeinVTuber', stat: '0', lbl: 'followers', Icon: Icons.TikTok, href: 'https://www.tiktok.com/@deinvtuber' },];

  return (
    <section className="socials" id="streams">
      <div className="shell">
        <div className="section-head" style={{ marginBottom: 32, color: 'var(--surface)' }}>
          <div>
            <div className="section-eyebrow" style={{ color: 'var(--accent)' }}>
              <span style={{ background: 'var(--orange)', width: 28, height: 2 }}></span>
              01 / Todas Las Redes Sociales
            </div>
            <h2 className="h-lg" style={{ color: 'var(--surface)' }}>
              Únete y No Te Pierdas Nada
            </h2>
          </div>
          <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
          </div>
        </div>

        <div className="socials-grid">
          {socials.map((s) =>
            <a key={s.handle} className="social-card" data-platform={s.p} href={s.href}>
              <div className="row">
                <div className="icon"><s.Icon /></div>
                <Icons.Arrow width="22" height="22" />
              </div>
              <div>
                <div className="platform">{s.name}</div>
                <div className="handle">{s.handle}</div>
              </div>
              <div className="row" style={{ alignItems: 'flex-end' }}>
                <div>
                  <div className="stat">{s.stat}</div>
                  <div className="stat-lbl">{s.lbl}</div>
                </div>
                <span className="chip dark" style={{ borderColor: 'currentColor', background: 'transparent', color: 'currentColor' }}>FOLLOW</span>
              </div>
            </a>
          )}
        </div>
      </div>
    </section>);

}

// expose
Object.assign(window, { Icons, Topbar, Marquee, Hero, Socials });