// Tryovi -- Kiosk Screens
// =============================================================================

const { useState, useEffect, useRef } = React;

// =============================================================================
// 1. WELCOME / ATTRACT
// =============================================================================
function ScreenWelcome({ onStart }) {
  const [tick, setTick] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setTick(t => t + 1), 80);
    return () => clearInterval(id);
  }, []);

  // Tabloid background -- coral half + cream half + giant collage
  return (
    <div data-screen-label="01 Welcome" className="kiosk" style={{ background: 'var(--cream)' }}>
      <StatusBar />

      {/* Top brand mark + date strip */}
      <div style={{ position: 'absolute', top: 96, left: 64, right: 64, display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start' }}>
        <Logo size={44} />
        <div className="mono" style={{ fontSize: 14, textAlign: 'right', letterSpacing: '0.08em' }}>
          <div>VOL. 04 — ISSUE 22</div>
          <div style={{ opacity: 0.55, marginTop: 4 }}>SPRING / SUMMER 2026</div>
        </div>
      </div>

      {/* Coral splash on right side -- magazine cover feel */}
      <div style={{
        position: 'absolute', top: 200, right: -120, width: 720, height: 920,
        background: 'var(--coral)', borderRadius: '60% 50% 55% 45% / 50% 60% 40% 50%',
        opacity: 0.95,
      }} />

      {/* Editorial photo collage */}
      <div style={{ position: 'absolute', top: 240, right: 80, width: 520, height: 720, transform: 'rotate(2deg)', boxShadow: '0 40px 80px rgba(0,0,0,0.18)' }}>
        <img src={PEOPLE.attract1} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
      </div>
      <div style={{ position: 'absolute', top: 760, right: 540, width: 280, height: 360, transform: 'rotate(-4deg)', boxShadow: '0 24px 48px rgba(0,0,0,0.22)' }}>
        <img src={PEOPLE.attract2} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
      </div>

      {/* Massive editorial headline */}
      <div style={{ position: 'absolute', top: 360, left: 72, width: 620, zIndex: 3 }}>
        <div className="eyebrow" style={{ marginBottom: 28, color: 'var(--ink)' }}>
          ◉ Live · Virtual Try-On Studio
        </div>
        <h1 className="display" style={{ fontSize: 220, lineHeight: 0.85 }}>
          Find<br />
          <span className="display-italic" style={{ color: 'var(--coral)' }}>your</span><br />
          look.
        </h1>
        <p style={{ marginTop: 36, fontSize: 30, lineHeight: 1.3, maxWidth: 540, color: 'var(--ink)', opacity: 0.78 }}>
          A studio in a screen. Step in, try on, walk out. No fitting rooms — just you and a hundred new outfits.
        </p>
      </div>

      {/* Sparkle decorations */}
      <div style={{ position: 'absolute', top: 320, right: 60, animation: 'spin-slow 12s linear infinite' }}>
        <Sparkle size={56} color="var(--acid)" />
      </div>
      <div style={{ position: 'absolute', top: 1080, left: 540, animation: 'spin-slow 8s linear infinite reverse' }}>
        <Sparkle size={36} color="var(--ink)" />
      </div>

      {/* CTA Tap to begin -- bottom center, animated */}
      <div style={{ position: 'absolute', bottom: 240, left: 0, right: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 28 }}>
        <button onClick={onStart} className="btn btn-primary" style={{
          fontSize: 36, padding: '36px 72px', position: 'relative',
        }}>
          <span style={{ display: 'inline-flex', color: 'var(--acid)' }}>
            <WaveHand size={44} />
          </span>
          Tap to begin
          <span className="mono" style={{ opacity: 0.5, fontSize: 22, marginLeft: 8 }}>↓</span>
        </button>
        <div style={{ position: 'relative', width: 120, height: 120 }}>
          <div style={{ position: 'absolute', inset: 0, borderRadius: '50%', border: '3px solid var(--coral)', animation: 'pulse-ring 1.8s ease-out infinite' }} />
          <div style={{ position: 'absolute', inset: 0, borderRadius: '50%', border: '3px solid var(--coral)', animation: 'pulse-ring 1.8s ease-out infinite 0.6s' }} />
          <div style={{ position: 'absolute', inset: 30, borderRadius: '50%', background: 'var(--ink)', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="var(--cream)" strokeWidth="2.5" strokeLinecap="round">
              <path d="M 12 6 L 12 18 L 8 14 M 12 18 Q 12 24 18 22 L 24 20 Q 28 19 26 24 L 22 30" />
            </svg>
          </div>
        </div>
      </div>

      {/* Bottom marquee strip */}
      <div style={{ position: 'absolute', bottom: 0, left: 0, right: 0, height: 80, background: 'var(--ink)', color: 'var(--cream)', display: 'flex', alignItems: 'center', overflow: 'hidden' }}>
        <div className="marquee mono" style={{ fontSize: 22, letterSpacing: '0.16em' }}>
          {Array(2).fill(0).map((_, k) => (
            <React.Fragment key={k}>
              <span>★ TRY ON IN SECONDS</span>
              <span style={{ color: 'var(--acid)' }}>SPRING / SUMMER 2026</span>
              <span>★ NO FITTING ROOMS</span>
              <span style={{ color: 'var(--coral)' }}>OVER 240 STYLES</span>
              <span>★ PHOTOREALISTIC AI</span>
              <span style={{ color: 'var(--acid)' }}>SHARE TO PHONE</span>
            </React.Fragment>
          ))}
        </div>
      </div>
    </div>
  );
}

// =============================================================================
// 2. PALM-UP TO CAPTURE
// =============================================================================
function ScreenPalm({ onContinue }) {
  const [progress, setProgress] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setProgress(p => Math.min(p + 0.02, 1)), 50);
    return () => clearInterval(id);
  }, []);
  useEffect(() => {
    if (progress >= 1) {
      const t = setTimeout(onContinue, 400);
      return () => clearTimeout(t);
    }
  }, [progress]);

  return (
    <div data-screen-label="02 Palm" className="kiosk" style={{ background: 'var(--ink)', color: 'var(--cream)' }}>
      <StatusBar dark step={2} total={9} />

      <div style={{ position: 'absolute', top: 140, left: 64, right: 64 }}>
        <div className="eyebrow" style={{ color: 'var(--acid)' }}>◉ Hand recognition active</div>
        <h2 className="display" style={{ fontSize: 110, marginTop: 24, color: 'var(--cream)' }}>
          Hold up your<br /><span className="display-italic" style={{ color: 'var(--coral)' }}>palm</span> to start.
        </h2>
        <p style={{ fontSize: 26, marginTop: 24, opacity: 0.65, maxWidth: 700, lineHeight: 1.4 }}>
          Show your palm to the camera. We'll start a three-second countdown when we see you.
        </p>
      </div>

      {/* Big palm illustration with scanning */}
      <div style={{ position: 'absolute', top: 580, left: 0, right: 0, display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
        <div style={{ position: 'relative', width: 540, height: 640, display: 'flex', justifyContent: 'center' }}>
          {/* concentric rings */}
          {[0, 1, 2, 3].map(i => (
            <div key={i} style={{
              position: 'absolute', top: '50%', left: '50%', width: 460 + i * 80, height: 460 + i * 80,
              transform: 'translate(-50%, -50%)',
              borderRadius: '50%',
              border: '1px solid rgba(232,255,74,' + (0.3 - i * 0.06) + ')',
            }} />
          ))}
          {/* scan line */}
          <div style={{
            position: 'absolute', top: 0, left: '50%', transform: 'translateX(-50%)',
            width: 380, height: 4,
            background: 'linear-gradient(90deg, transparent, var(--acid), transparent)',
            animation: 'scan 2.4s ease-in-out infinite',
            zIndex: 2,
          }} />
          <PalmIcon size={420} color="var(--acid)" />
          {/* tracking dots on knuckles */}
          {[
            [50, 22], [50, 50], [80, 30], [20, 30], [62, 12],
          ].map((p, i) => (
            <div key={i} style={{
              position: 'absolute', left: p[0] + '%', top: p[1] + '%',
              width: 14, height: 14, borderRadius: '50%',
              background: 'var(--coral)',
              boxShadow: '0 0 0 4px rgba(255,61,110,0.25)',
              animation: 'pulse-ring 1.5s ease-out infinite ' + (i * 0.2) + 's',
            }} />
          ))}
        </div>
      </div>

      {/* Detection progress */}
      <div style={{ position: 'absolute', bottom: 200, left: 64, right: 64 }}>
        <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: 16, fontSize: 18 }} className="mono">
          <span style={{ color: 'var(--acid)' }}>● DETECTING HAND</span>
          <span>{Math.round(progress * 100)}%</span>
        </div>
        <div style={{ height: 6, background: 'rgba(247,242,234,0.15)', borderRadius: 999, overflow: 'hidden' }}>
          <div style={{ height: '100%', width: (progress * 100) + '%', background: 'var(--acid)', transition: 'width 0.1s linear' }} />
        </div>
      </div>

      <div style={{ position: 'absolute', bottom: 80, left: 64, right: 64, display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
        <span className="mono" style={{ fontSize: 14, opacity: 0.5 }}>STEP 01 · CAPTURE</span>
        <span className="mono" style={{ fontSize: 14, opacity: 0.5 }}>NEXT: PHOTO →</span>
      </div>
    </div>
  );
}

// =============================================================================
// 3. CAMERA CAPTURE -- viewfinder w/ countdown
// =============================================================================
function ScreenCapture({ onCapture }) {
  const [count, setCount] = useState(3);
  const [flash, setFlash] = useState(false);

  useEffect(() => {
    if (count > 0) {
      const t = setTimeout(() => setCount(c => c - 1), 1100);
      return () => clearTimeout(t);
    } else {
      setFlash(true);
      const t = setTimeout(onCapture, 450);
      return () => clearTimeout(t);
    }
  }, [count]);

  return (
    <div data-screen-label="03 Capture" className="kiosk" style={{ background: 'var(--ink)' }}>
      {flash && <div style={{ position: 'absolute', inset: 0, background: 'white', zIndex: 99, animation: 'fadeUp 300ms ease-out reverse' }} />}

      {/* Live viewfinder fills most of screen */}
      <div style={{ position: 'absolute', top: 96, left: 48, right: 48, bottom: 280, overflow: 'hidden', borderRadius: 12, background: '#0a0a0a' }}>
        {/* simulated live cam (Unsplash photo, slight desaturate) */}
        <img src={PEOPLE.liveModel} alt="" style={{
          width: '100%', height: '100%', objectFit: 'cover', display: 'block',
          filter: 'saturate(0.92) contrast(1.05)',
        }} />

        {/* Body guide overlay -- centered */}
        <div style={{ position: 'absolute', inset: 0, display: 'flex', justifyContent: 'center', alignItems: 'center', pointerEvents: 'none' }}>
          <div style={{ width: 460, height: '88%', position: 'relative' }}>
            <BodyGuide stroke="#E8FF4A" opacity={0.6} />
          </div>
        </div>

        {/* Corner brackets */}
        <div className="bracket tl"></div>
        <div className="bracket tr"></div>
        <div className="bracket bl"></div>
        <div className="bracket br"></div>

        {/* Top HUD bar */}
        <div style={{ position: 'absolute', top: 20, left: 24, right: 24, display: 'flex', justifyContent: 'space-between', alignItems: 'center', color: 'var(--acid)', fontFamily: 'var(--mono)', fontSize: 14 }}>
          <div style={{ display: 'flex', gap: 16, alignItems: 'center' }}>
            <span style={{ display: 'inline-block', width: 10, height: 10, borderRadius: '50%', background: 'var(--coral)', animation: 'blink 1s infinite' }}></span>
            <span>REC · LIVE</span>
            <span style={{ opacity: 0.6 }}>1080 × 1920</span>
          </div>
          <div style={{ display: 'flex', gap: 12, alignItems: 'center' }}>
            <span style={{ background: 'rgba(232,255,74,0.15)', padding: '6px 12px', borderRadius: 4 }}>POSE: ✓ CENTERED</span>
            <span style={{ background: 'rgba(232,255,74,0.15)', padding: '6px 12px', borderRadius: 4 }}>LIGHT: 84%</span>
          </div>
        </div>

        {/* Bottom HUD */}
        <div style={{ position: 'absolute', bottom: 24, left: 24, right: 24, display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', color: 'var(--cream)', fontFamily: 'var(--mono)', fontSize: 13 }}>
          <div style={{ background: 'rgba(0,0,0,0.55)', padding: '12px 16px', borderRadius: 6, backdropFilter: 'blur(8px)' }}>
            <div style={{ color: 'var(--acid)' }}>● BODY DETECTED</div>
            <div style={{ opacity: 0.7, marginTop: 4 }}>Height ≈ 168 cm</div>
          </div>
          <div style={{ background: 'rgba(0,0,0,0.55)', padding: '12px 16px', borderRadius: 6, backdropFilter: 'blur(8px)', textAlign: 'right' }}>
            <div>FRAME 0238</div>
            <div style={{ opacity: 0.7, marginTop: 4 }}>f/2.8 · ISO 200</div>
          </div>
        </div>

        {/* Massive countdown number */}
        {count > 0 && (
          <div key={count} style={{ position: 'absolute', inset: 0, display: 'flex', justifyContent: 'center', alignItems: 'center', pointerEvents: 'none' }}>
            <div style={{
              fontFamily: 'var(--serif)', fontStyle: 'italic',
              fontSize: 540, lineHeight: 1, color: 'var(--cream)',
              textShadow: '0 0 80px rgba(0,0,0,0.5)',
              animation: 'fadeUp 300ms ease-out',
            }}>{count}</div>
          </div>
        )}
        {count === 0 && !flash && (
          <div style={{ position: 'absolute', inset: 0, display: 'flex', justifyContent: 'center', alignItems: 'center', pointerEvents: 'none' }}>
            <div style={{ fontFamily: 'var(--serif)', fontStyle: 'italic', fontSize: 200, color: 'var(--coral)', animation: 'fadeUp 200ms ease-out' }}>Smile</div>
          </div>
        )}
      </div>

      {/* Bottom panel -- capture controls */}
      <div style={{ position: 'absolute', bottom: 0, left: 0, right: 0, height: 280, background: 'var(--cream)', display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', gap: 20 }}>
        <div className="eyebrow">◉ Hold still — capturing in</div>
        <div style={{ display: 'flex', gap: 24, alignItems: 'center' }}>
          {[3, 2, 1].map(n => (
            <div key={n} style={{
              width: 80, height: 80, borderRadius: '50%',
              background: n === count ? 'var(--coral)' : (n > count ? 'var(--ink)' : 'transparent'),
              border: n < count ? '2px solid var(--ink)' : 'none',
              opacity: n < count ? 0.2 : 1,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontFamily: 'var(--serif)', fontSize: 44, color: n === count ? 'var(--cream)' : (n > count ? 'var(--cream)' : 'var(--ink)'),
              transition: 'all 200ms ease',
              transform: n === count ? 'scale(1.15)' : 'scale(1)',
            }}>{n}</div>
          ))}
        </div>
        <div className="mono" style={{ fontSize: 14, opacity: 0.5, marginTop: 8 }}>STAY CENTERED · KEEP ARMS RELAXED</div>
      </div>
    </div>
  );
}

// =============================================================================
// 4. CAPTURED CONFIRMATION
// =============================================================================
function ScreenCaptured({ onContinue, onRetake }) {
  return (
    <div data-screen-label="04 Captured" className="kiosk" style={{ background: 'var(--cream)' }}>
      <StatusBar step={3} total={9} />

      <div style={{ position: 'absolute', top: 140, left: 64, right: 64 }}>
        <div className="eyebrow" style={{ color: 'var(--coral)' }}>◉ That's the one</div>
        <h2 className="display" style={{ fontSize: 130, marginTop: 16 }}>
          Looking <span className="display-italic" style={{ color: 'var(--coral)' }}>good.</span>
        </h2>
        <p style={{ fontSize: 26, marginTop: 16, opacity: 0.7, maxWidth: 720 }}>
          We'll use this base photo to render every try-on. Want to keep it, or take another?
        </p>
      </div>

      {/* Big captured photo with editorial frame */}
      <div style={{ position: 'absolute', top: 480, left: '50%', transform: 'translateX(-50%)', width: 680, height: 920 }}>
        <div style={{ position: 'absolute', inset: 0, background: 'var(--coral)', transform: 'translate(20px, 20px)' }} />
        <div style={{ position: 'absolute', inset: 0, background: 'var(--ink)' }}>
          <img src={PEOPLE.captured} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover', filter: 'contrast(1.04)' }} />
          {/* Polaroid label */}
          <div style={{ position: 'absolute', bottom: 24, left: 24, right: 24, display: 'flex', justifyContent: 'space-between', color: 'var(--cream)', fontFamily: 'var(--mono)', fontSize: 14, letterSpacing: '0.1em' }}>
            <span>FRAME · 0238</span>
            <span>14:32 · KIOSK 04</span>
          </div>
        </div>
        {/* Sticker */}
        <div style={{
          position: 'absolute', top: -32, right: -32, width: 140, height: 140,
          background: 'var(--acid)', borderRadius: '50%',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'var(--serif)', fontStyle: 'italic', fontSize: 28, color: 'var(--ink)',
          textAlign: 'center', lineHeight: 1, transform: 'rotate(-12deg)',
          animation: 'float 3s ease-in-out infinite',
        }}>
          base<br />photo
        </div>
      </div>

      {/* Action buttons */}
      <div style={{ position: 'absolute', bottom: 80, left: 64, right: 64, display: 'flex', gap: 24 }}>
        <button onClick={onRetake} className="btn btn-ghost" style={{ flex: 1 }}>
          ↻ Retake
        </button>
        <button onClick={onContinue} className="btn btn-primary" style={{ flex: 2 }}>
          Continue → choose look
        </button>
      </div>
    </div>
  );
}

Object.assign(window, {
  ScreenWelcome, ScreenPalm, ScreenCapture, ScreenCaptured,
});
