/* global React */
const { useState } = React;

/* ---------- Logo — uses the real PNG ---------- */
function Logo({ variant = 'black', size = 40 }) {
  const src = variant === 'white' ? './assets/logo-white.png' : './assets/logo.png';
  return (
    <img src={src} alt="robis"
      style={{ width: 130, height: 38, objectFit: 'contain', display: 'block' }} />
  );
}

/* ---------- Nav — matches live site: Montserrat Bold 16.5px uppercase, thin underline ---------- */
function Nav({ current, onNav, onDark = false, logoSize = 44 }) {
  const fg = onDark ? '#fff' : 'rgba(0,0,0,0.99)';
  const items = ['Home', 'Photo', 'Video', 'Contact Us'];
  return (
    <header style={{
      position: 'absolute', top: 0, left: 0, right: 0, zIndex: 20,
      height: 112, display: 'flex', alignItems: 'center',
      padding: '0 64px',
    }}>
      <a onClick={() => onNav('Home')} style={{ cursor: 'pointer', display: 'inline-flex' }}>
        <Logo variant={onDark ? 'white' : 'black'} size={logoSize} />
      </a>
      <nav style={{
        position: 'absolute', left: '50%', top: '50%',
        transform: 'translate(-50%, -50%)',
        display: 'flex', gap: 44,
      }}>
        {items.map((n) => {
          const label = n.toUpperCase();
          const active = current === n;
          return (
            <a key={n} onClick={() => onNav(n)} style={{
              cursor: 'pointer', color: fg,
              fontFamily: '"Montserrat", "Helvetica Neue", Arial, sans-serif',
              fontWeight: 700,
              fontSize: 16.5,
              lineHeight: '26.4px',
              letterSpacing: '0',
              padding: '1.65px 0',
              display: 'inline-flex',
              alignItems: 'center',
              // thin underline when active (mirrors live 1px bottom gradient)
              backgroundImage: active
                ? `linear-gradient(${fg}, ${fg})`
                : 'none',
              backgroundRepeat: 'no-repeat',
              backgroundPosition: '0 calc(100% - 1.65px)',
              backgroundSize: '100% 1px',
              transition: 'opacity .2s',
            }}
            onMouseEnter={e => e.currentTarget.style.opacity = 0.7}
            onMouseLeave={e => e.currentTarget.style.opacity = 1}
            >{label}</a>
          );
        })}
      </nav>
      <a onClick={() => onNav('Contact Us')} style={{ color: fg, cursor: 'pointer', display: 'inline-flex', marginLeft: 'auto' }} title="Contact">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
          <rect x="3" y="5" width="18" height="14" rx="2"/><path d="M3 7l9 6 9-6"/>
        </svg>
      </a>
    </header>
  );
}

/* ---------- Squircle tile (rounded-square with extra curvature) ---------- */
/* True iOS-style superellipse clip-path (squircle). The path is normalized
   0..1 so we apply it via clipPath with objectBoundingBox. */
const SQUIRCLE_PATH = "M .5 0 C .82 0 1 .18 1 .5 C 1 .82 .82 1 .5 1 C .18 1 0 .82 0 .5 C 0 .18 .18 0 .5 0 Z";

function Squircle({ children, width, height, size, style, onClick, hover = true, shadow = true }) {
  const [h, setH] = useState(false);
  const w = width ?? size;
  const ht = height ?? size;
  return (
    <div
      onClick={onClick}
      onMouseEnter={() => hover && setH(true)}
      onMouseLeave={() => setH(false)}
      style={{
        width: w, height: ht,
        overflow: 'hidden', flexShrink: 0, cursor: onClick ? 'pointer' : 'default',
        position: 'relative',
        filter: shadow ? 'drop-shadow(0 18px 24px rgba(0,0,0,.35)) drop-shadow(0 4px 8px rgba(0,0,0,.25))' : 'none',
        transform: h ? 'scale(1.06) translateY(-2px)' : 'scale(1)',
        transition: 'transform .35s cubic-bezier(.2,.7,.15,1)',
        clipPath: `path('${tvMaskPath(w, ht)}')`,
        WebkitClipPath: `path('${tvMaskPath(w, ht)}')`,
        ...style,
      }}
    >{children}</div>
  );
}

/* Squarespace's "TV" fluid-image mask — captured directly from robis.org.
   Normalized path (0..1), scaled to pixel dimensions so it works with any
   width × height (square, portrait, or landscape tiles). */
function tvMaskPath(w, h) {
  // Original normalized path from Squarespace:
  // M0.5,0 C0.768,0,0.894,0.056,0.919,0.081 C0.944,0.106,1,0.242,1,0.505
  // C1,0.768,0.947,0.891,0.919,0.919 C0.891,0.947,0.768,1,0.5,1
  // C0.202,1,0.11,0.948,0.081,0.919 C0.052,0.89,0,0.768,0,0.5
  // C0,0.232,0.053,0.109,0.081,0.081 C0.109,0.053,0.232,0,0.5,0
  const pts = [
    ['M', 0.5, 0],
    ['C', 0.768, 0, 0.894, 0.056, 0.919, 0.081],
    ['C', 0.944, 0.106, 1, 0.242, 1, 0.505],
    ['C', 1, 0.768, 0.947, 0.891, 0.919, 0.919],
    ['C', 0.891, 0.947, 0.768, 1, 0.5, 1],
    ['C', 0.202, 1, 0.11, 0.948, 0.081, 0.919],
    ['C', 0.052, 0.89, 0, 0.768, 0, 0.5],
    ['C', 0, 0.232, 0.053, 0.109, 0.081, 0.081],
    ['C', 0.109, 0.053, 0.232, 0, 0.5, 0],
  ];
  return pts.map(seg => {
    const cmd = seg[0];
    const nums = seg.slice(1).map((v, i) => (i % 2 === 0 ? v * w : v * h).toFixed(3));
    return cmd + ' ' + nums.join(' ');
  }).join(' ') + ' Z';
}

function ImgTile({ src, label, size, width, height, onClick, rotate = 0, style }) {
  return (
    <Squircle size={size} width={width} height={height} onClick={onClick}
      style={{ transform: `rotate(${rotate}deg)`, ...(style || {}) }}>
      <img src={src} alt={label} style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
    </Squircle>
  );
}

/* ---------- Hero Statement — "DISCOVER.  CREATE.  INSPIRE." in quotes ---------- */
function HeroQuote({ words, color = '#000000' }) {
  return (
    <div style={{
      fontFamily: '"Montserrat", "Helvetica Neue", Arial, sans-serif',
      fontStyle: 'normal', fontWeight: 700,
      fontSize: 54, lineHeight: '58px',
      letterSpacing: '0', textTransform: 'uppercase', color,
      textAlign: 'center', whiteSpace: 'nowrap',
    }}>
      <span style={{ fontWeight: 900 }}>&ldquo;</span>
      {words.map((w, i) => (
        <span key={i} style={{ marginRight: i < words.length - 1 ? '1.2em' : 0 }}>
          {w}.
        </span>
      ))}
      <span style={{ fontWeight: 900 }}>&rdquo;</span>
    </div>
  );
}

/* ---------- Underlined "CONTACT US." link with blue scribble ---------- */
function UnderlineLink({ children, color = '#0a0a0b', onClick }) {
  return (
    <a onClick={onClick} style={{
      position: 'relative',
      fontFamily: 'var(--font-display)', fontStyle: 'italic', fontWeight: 900,
      textTransform: 'uppercase', letterSpacing: '-.02em',
      fontSize: 32, color, cursor: 'pointer',
      display: 'inline-block', paddingBottom: 12,
    }}>
      {children}
      {/* Hand-drawn blue underline */}
      <svg width="100%" height="14" viewBox="0 0 260 14" preserveAspectRatio="none"
        style={{ position: 'absolute', left: 0, bottom: -2, width: '100%' }}>
        <path d="M2 8 C 40 2, 90 12, 140 6 S 230 10, 258 4"
          stroke="#79a9f5" strokeWidth="3" fill="none" strokeLinecap="round" />
      </svg>
    </a>
  );
}

/* ---------- Spotify-style Media Card ---------- */
function MediaCard() {
  const [hover, setHover] = useState(false);
  return (
    <div
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{
        background: '#2b5ba8', borderRadius: 14,
        padding: '14px 18px',
        display: 'flex', alignItems: 'center', gap: 16, color: '#fff',
        width: 380,
        boxShadow: hover
          ? '0 28px 60px -10px rgba(0,0,0,.45)'
          : '0 20px 50px -12px rgba(0,0,0,.35)',
        transform: hover ? 'translateY(-2px)' : 'translateY(0)',
        transition: 'all .25s cubic-bezier(.2,.7,.15,1)',
        cursor: 'pointer',
      }}>
      <div style={{
        width: 60, height: 60, borderRadius: 10,
        background: 'radial-gradient(120% 120% at 30% 30%, #eaf2ff 0%, #b5d1ff 45%, #79a9f5 100%)',
        flexShrink: 0,
      }} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontFamily: 'var(--font-text)', fontWeight: 700, fontSize: 17, letterSpacing: '.01em' }}>ROBIS.org</div>
        <div style={{ fontSize: 11, opacity: .8, marginTop: 3 }}>
          <b style={{ fontWeight: 700 }}>Preview</b>&nbsp;&nbsp;Jack R
        </div>
        <div style={{ fontSize: 12, marginTop: 8, opacity: .95, display: 'flex', alignItems: 'center', gap: 6 }}>
          <span style={{
            display: 'inline-flex', width: 14, height: 14, borderRadius: '50%',
            border: '1.5px solid #fff', alignItems: 'center', justifyContent: 'center',
            fontSize: 14, fontWeight: 600, lineHeight: 1,
          }}>+</span>
          Save on Spotify
        </div>
      </div>
      <div style={{
        width: 30, height: 30, borderRadius: '50%', background: '#1db954',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        flexShrink: 0,
      }}>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="#000">
          <path d="M12 2a10 10 0 100 20 10 10 0 000-20zm4.5 14.4a.62.62 0 01-.86.2c-2.36-1.44-5.33-1.77-8.83-.97a.62.62 0 11-.28-1.22c3.83-.87 7.1-.5 9.77 1.13.3.18.4.57.2.86zm1.2-2.67a.78.78 0 01-1.07.26c-2.7-1.66-6.82-2.14-10.02-1.17a.78.78 0 11-.45-1.5c3.66-1.1 8.2-.58 11.3 1.32a.78.78 0 01.25 1.09zm.1-2.78c-3.24-1.93-8.6-2.1-11.7-1.17a.93.93 0 11-.54-1.78c3.56-1.08 9.48-.87 13.23 1.35a.93.93 0 11-.96 1.6z" />
        </svg>
      </div>
    </div>
  );
}

/* ---------- Scribbled arrow annotation — big J-curve, label on left ---------- */
function Annotation({ label, style }) {
  return (
    <div style={{
      position: 'absolute', display: 'inline-flex', alignItems: 'flex-end', gap: 0,
      ...style,
    }}>
      <img src="./assets/icons-clickable.png" alt={label || 'icons are clickable.'}
        style={{ height: 46, width: 'auto', display: 'block' }} />
    </div>
  );
}

/* ---------- Plane svg for sky hero ---------- */
function Plane({ style }) {
  return (
    <svg width="56" height="26" viewBox="0 0 56 26" style={{ ...style, animation: 'planeDrift 9s ease-in-out infinite' }}>
      <path d="M2 13 L20 11 L30 3 L33 3 L28 12 L42 11 L48 6 L50 7 L47 12 L54 13 L47 14 L50 19 L48 20 L42 15 L28 14 L33 23 L30 23 L20 15 L2 13Z"
        fill="#1b1e24" />
    </svg>
  );
}

/* ---------- Pill button — matches live site: Montserrat 800 italic, capitalize, 18.75px, opacity 0.8 idle → 1 hover ---------- */
function PillButton({ children, onClick, variant = 'dark' }) {
  const [hover, setHover] = useState(false);
  const [press, setPress] = useState(false);
  const styles = variant === 'dark'
    ? { background: '#000', color: '#fff' }
    : { background: '#fff', color: '#000' };
  return (
    <button
      onClick={onClick}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => { setHover(false); setPress(false); }}
      onMouseDown={() => setPress(true)}
      onMouseUp={() => setPress(false)}
      style={{
        ...styles,
        border: 0,
        fontFamily: '"Montserrat", "Helvetica Neue", Arial, sans-serif',
        fontStyle: 'italic', fontWeight: 800,
        fontSize: 18,
        textTransform: 'capitalize',
        letterSpacing: '0',
        padding: '14px 22px',
        borderRadius: 300, cursor: 'pointer',
        transform: press ? 'scale(.97)' : 'scale(1)',
        opacity: hover ? 1 : 0.8,
        transition: 'opacity .1s linear, transform .15s',
      }}>{children}</button>
  );
}

/* ---------- Section wavy divider ---------- */
function WaveDivider({ flip = false, fill = '#000', bg = 'transparent' }) {
  return (
    <div style={{ position: 'relative', lineHeight: 0, background: bg }}>
      <svg viewBox="0 0 1440 80" preserveAspectRatio="none" style={{
        display: 'block', width: '100%', height: 80,
        transform: flip ? 'scaleY(-1)' : 'none',
      }}>
        <path d="M0 80 C 360 10, 1080 10, 1440 80 L1440 80 L0 80 Z" fill={fill} />
      </svg>
    </div>
  );
}

/* ---------- Footer ---------- */
function Footer({ onDark = true, onNav }) {
  const fg = onDark ? '#b4b4bc' : '#2a2a2e';
  return (
    <footer style={{
      padding: '40px 36px 32px',
      display: 'flex', justifyContent: 'space-between', alignItems: 'center',
      fontFamily: 'var(--font-text)', fontSize: 12,
      color: fg, borderTop: `1px solid ${onDark ? '#161618' : 'rgba(0,0,0,.1)'}`,
      flexWrap: 'wrap', gap: 20,
    }}>
      <Logo variant={onDark ? 'white' : 'black'} size={18} />
      <div style={{ display: 'flex', gap: 28, letterSpacing: '.04em' }}>
        <a style={{ cursor: 'pointer' }} onClick={() => onNav && onNav('Contact Us')}>hello@robis.org</a>
        <a style={{ cursor: 'pointer' }}>Instagram</a>
        <a style={{ cursor: 'pointer' }}>Vimeo</a>
      </div>
      <div style={{ fontFamily: 'var(--font-mono)', fontSize: 11, opacity: .8 }}>© robis 2026</div>
    </footer>
  );
}

Object.assign(window, {
  Logo, Nav, Squircle, ImgTile, HeroQuote, UnderlineLink,
  MediaCard, Annotation, Plane, PillButton, WaveDivider, Footer,
});
