/* global React, useScrollY */

function ScrollProgress() {
  const y = useScrollY();
  const [pct, setPct] = React.useState(0);
  React.useEffect(() => {
    const total = document.documentElement.scrollHeight - window.innerHeight;
    setPct(total > 0 ? Math.min(y / total, 1) : 0);
  }, [y]);
  return (
    <div style={{
      position: 'fixed', top: 0, left: 0, right: 0, height: 2,
      zIndex: 200, pointerEvents: 'none',
      background: 'rgba(245, 241, 232, 0.06)',
    }}>
      <div style={{
        height: '100%', background: 'linear-gradient(90deg, var(--vermillion), var(--gold))',
        width: `${pct * 100}%`,
        transition: 'width 80ms linear',
      }} />
    </div>
  );
}

Object.assign(window, { ScrollProgress });
const { useEffect: useEffectH, useRef: useRefH, useState: useStateH } = React;

// ============ Hero ============
function Hero({ headlineVariant = 'grow', showCn = true }) {
  const y = useScrollY();
  const heroRef = useRefH(null);
  const [mounted, setMounted] = useStateH(false);
  useEffectH(() => { setMounted(true); }, []);

  // Parallax transforms
  const bgY = y * 0.4;
  const midY = y * 0.18;
  const fgY = y * -0.08;
  const opacity = Math.max(0, 1 - y / 700);

  return (
    <section ref={heroRef} id="top" style={{
      position: 'relative',
      minHeight: '100vh',
      background: 'var(--ink)',
      overflow: 'hidden',
      color: 'var(--paper)',
      paddingTop: 88,
    }}>
      {/* Parallax photo bg */}
      <div style={{
        position: 'absolute', inset: '-10% 0 0 0', height: '120%',
        transform: `translate3d(0, ${bgY}px, 0)`,
        backgroundImage: 'url(https://images.unsplash.com/photo-1493946740644-2d8a1f1a6aff?w=2400&q=80)',
        backgroundSize: 'cover', backgroundPosition: 'center',
        opacity: 0.42,
      }} />
      {/* Vignette */}
      <div style={{
        position: 'absolute', inset: 0,
        background: 'linear-gradient(180deg, rgba(10,22,40,0.55) 0%, rgba(10,22,40,0.4) 40%, rgba(10,22,40,0.95) 100%)',
      }} />
      {/* Grain */}
      <div style={{
        position: 'absolute', inset: 0, opacity: 0.4, mixBlendMode: 'overlay',
        background: 'radial-gradient(circle at 30% 20%, rgba(212,168,87,0.15), transparent 60%), radial-gradient(circle at 80% 70%, rgba(200,71,46,0.12), transparent 50%)',
      }} />

      {/* Decorative oversize wordmark */}
      <div className="display" style={{
        position: 'absolute', right: '-2%', top: '6%',
        fontSize: 'clamp(220px, 30vw, 460px)',
        color: 'var(--gold)', opacity: 0.05, fontWeight: 300,
        fontStyle: 'italic',
        transform: `translate3d(0, ${midY}px, 0)`,
        userSelect: 'none', pointerEvents: 'none',
        lineHeight: 0.85, letterSpacing: '-0.06em',
      }}>Trade.</div>

      <div className="container" style={{
        position: 'relative', minHeight: 'calc(100vh - 88px)',
        display: 'flex', flexDirection: 'column', justifyContent: 'center',
        paddingBottom: 80, paddingTop: 40,
        opacity,
        transform: `translate3d(0, ${fgY}px, 0)`,
      }}>
        <div style={{ maxWidth: 980 }}>
          <div style={{
            opacity: mounted ? 1 : 0,
            transform: mounted ? 'translateY(0)' : 'translateY(20px)',
            transition: 'all 800ms cubic-bezier(0.2, 0.8, 0.2, 1)',
          }}>
            <div className="eyebrow" style={{ color: 'var(--gold)', marginBottom: 32 }}>
              <span className="mono">PG/</span> Global Trade Partner — Hong Kong
            </div>
          </div>

          <h1 className="display" style={{
            fontSize: 'clamp(56px, 9vw, 132px)',
            marginBottom: 36,
            fontWeight: 500,
          }}>
            {headlineVariant === 'grow' && <>
              <HeroLine delay={120}>Grow faster</HeroLine>
              <HeroLine delay={280}>with <em style={{ fontStyle: 'normal', color: 'var(--gold)', fontWeight: 600 }}>Prime Global</em></HeroLine>
              <HeroLine delay={440}>Trade<span style={{ color: 'var(--vermillion)' }}>.</span></HeroLine>
            </>}
            {headlineVariant === 'connect' && <>
              <HeroLine delay={120}>Connecting you</HeroLine>
              <HeroLine delay={280}>with <em style={{ fontStyle: 'normal', color: 'var(--gold)', fontWeight: 600 }}>China's best</em></HeroLine>
              <HeroLine delay={440}>opportunities<span style={{ color: 'var(--vermillion)' }}>.</span></HeroLine>
            </>}
            {headlineVariant === 'launch' && <>
              <HeroLine delay={120}>Launch your</HeroLine>
              <HeroLine delay={280}>business with <em style={{ fontStyle: 'normal', color: 'var(--gold)', fontWeight: 600 }}>Prime</em></HeroLine>
              <HeroLine delay={440}>Global Trade<span style={{ color: 'var(--vermillion)' }}>.</span></HeroLine>
            </>}
          </h1>

          <p style={{
            fontSize: 'clamp(18px, 1.4vw, 22px)',
            maxWidth: 620, lineHeight: 1.5, color: 'rgba(245,241,232,0.75)',
            marginBottom: 48, fontWeight: 300,
            opacity: mounted ? 1 : 0,
            transform: mounted ? 'translateY(0)' : 'translateY(20px)',
            transition: 'all 800ms cubic-bezier(0.2, 0.8, 0.2, 1) 600ms',
          }}>
            Connecting You with China's Best Opportunities — integrated solutions for logistics, sourcing, and consulting from a verified network of Chinese partners.
          </p>

          <div style={{
            display: 'flex', gap: 16, flexWrap: 'wrap',
            opacity: mounted ? 1 : 0,
            transform: mounted ? 'translateY(0)' : 'translateY(20px)',
            transition: 'all 800ms cubic-bezier(0.2, 0.8, 0.2, 1) 760ms',
          }}>
            <a href="#contact" className="btn btn-primary">
              Get a Quote <span className="arrow">→</span>
            </a>
            <a href="#services" className="btn btn-ghost">
              Explore Services
            </a>
          </div>
        </div>

        {/* Bottom meta strip */}
        <div style={{
          position: 'absolute', bottom: 32, left: 48, right: 48,
          display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end',
          opacity: mounted ? 1 : 0,
          transition: 'opacity 1000ms ease 1100ms',
        }} className="hero-meta">
          <div style={{ display: 'flex', gap: 48 }}>
            <MetaStat label="Routes" value="68+" />
            <MetaStat label="Vetted suppliers" value="2,400" />
            <MetaStat label="Years operating" value="11" />
          </div>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 14,
            fontSize: 11, letterSpacing: '0.18em', textTransform: 'uppercase',
            color: 'rgba(245,241,232,0.55)',
          }}>
            <span className="scroll-line" />
            Scroll to explore
          </div>
        </div>
      </div>

      <style>{`
        .scroll-line {
          display: inline-block; width: 1px; height: 32px;
          background: linear-gradient(180deg, transparent, var(--gold), transparent);
          background-size: 100% 200%;
          animation: scrollPulse 2s linear infinite;
          margin-right: 4px;
        }
        @keyframes scrollPulse {
          0% { background-position: 0 -100%; }
          100% { background-position: 0 100%; }
        }
        @media (max-width: 860px) {
          .hero-meta {
            position: relative !important;
            left: 0 !important; right: 0 !important; bottom: 0 !important;
            margin-top: 56px;
            flex-direction: column; align-items: flex-start !important; gap: 28px;
          }
          .hero-meta > div:first-child { flex-wrap: wrap; gap: 32px !important; }
          .nav-links a:not(.btn) { display: none; }
        }
      `}</style>
    </section>
  );
}

function HeroLine({ children, delay }) {
  const [shown, setShown] = useStateH(false);
  useEffectH(() => {
    const t = setTimeout(() => setShown(true), delay);
    return () => clearTimeout(t);
  }, [delay]);
  return (
    <span style={{ display: 'block', overflow: 'hidden' }}>
      <span style={{
        display: 'inline-block',
        transform: shown ? 'translateY(0)' : 'translateY(110%)',
        transition: 'transform 1100ms cubic-bezier(0.2, 0.8, 0.2, 1)',
      }}>{children}</span>
    </span>
  );
}

function MetaStat({ label, value }) {
  return (
    <div>
      <div className="tabular" style={{ fontFamily: 'var(--sans)', fontSize: 38, color: 'var(--gold)', fontWeight: 500, letterSpacing: '-0.02em', lineHeight: 1 }}>
        {value}
      </div>
      <div className="mono" style={{ fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: 'rgba(245,241,232,0.55)', marginTop: 8 }}>
        {label}
      </div>
    </div>
  );
}

Object.assign(window, { Hero });
