/* Shared film helpers — Letterbox, FrameSlot, FilmHeader, Timecode, Outro
   Depends on: animations.jsx (Stage, Sprite, useTime, Easing, clamp)
   Exports: window.FilmShell = { Letterbox, FrameSlot, FilmHeader, Timecode, Outro, GOLD, GOLD_S, PAPER }
*/

const FilmShell = (() => {
  const GOLD = '#c4a26b';
  const GOLD_S = '#d4b783';
  const PAPER = '#f6f3ec';

  function Letterbox() {
    return (
      <>
        <div className="v-bars top"></div>
        <div className="v-bars bottom"></div>
      </>
    );
  }

  function FrameSlot({ x, y, w, h, label, children, style }) {
    return (
      <div className="frame-slot" style={{ left: x, top: y, width: w, height: h, ...style }}>
        <div className="corner tl"></div><div className="corner tr"></div>
        <div className="corner bl"></div><div className="corner br"></div>
        {label ? <div className="lbl">{label}</div> : null}
        {children}
      </div>
    );
  }

  function FilmHeader({ filmNo, title, runtime = 30 }) {
    return (
      <div style={{
        position: 'absolute', top: 80, left: 60, right: 60, zIndex: 12,
        display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start',
        fontFamily: 'IBM Plex Mono, monospace', fontSize: 11, letterSpacing: '0.18em',
        color: 'rgba(246,243,236,0.55)',
      }}>
        <div>
          <div style={{ display: 'flex', gap: 14, alignItems: 'center' }}>
            <span style={{ width: 24, height: 1, background: GOLD }}></span>
            <span style={{ color: GOLD_S }}>TAEKWANG GLASS</span>
          </div>
          <div style={{ marginTop: 6, color: 'rgba(246,243,236,0.4)' }}>EST. 1994 · INCHEON · PYEONGTAEK</div>
        </div>
        <div style={{ textAlign: 'right' }}>
          FILM {filmNo} / {title}<br/>
          <span style={{ color: 'rgba(246,243,236,0.35)' }}>{runtime}s · 1920×1080 · CINEMATIC</span>
        </div>
      </div>
    );
  }

  function Timecode({ runtime = 30 }) {
    const t = useTime();
    const cur = Math.min(t, runtime);
    const secs = String(Math.floor(cur)).padStart(2, '0');
    const ms = String(Math.floor((cur % 1) * 100)).padStart(2, '0');
    const totalSecs = String(runtime).padStart(2, '0');
    return (
      <div style={{
        position: 'absolute', bottom: 80, left: 60, zIndex: 12,
        fontFamily: 'IBM Plex Mono, monospace', fontSize: 11, letterSpacing: '0.18em',
        color: 'rgba(246,243,236,0.55)',
      }}>
        <span style={{ color: GOLD_S }}>● REC</span>
        &nbsp;&nbsp;TC&nbsp;00:00:<span style={{ color: PAPER }}>{secs}.{ms}</span>
        &nbsp;&nbsp;/&nbsp;&nbsp;00:00:{totalSecs}.00
      </div>
    );
  }

  function CornerBrand() {
    return (
      <div style={{
        position: 'absolute', bottom: 80, right: 60, zIndex: 12,
        fontFamily: 'IBM Plex Mono, monospace', fontSize: 11, color: 'rgba(246,243,236,0.4)',
        letterSpacing: '0.18em', textAlign: 'right',
      }}>
        ⓒ 2026 TAEKWANG<br/>
        <span style={{ color: 'rgba(246,243,236,0.25)' }}>BUSINESS REG. 122-81-95636</span>
      </div>
    );
  }

  // Outro scene — accepts start/end + slogan
  function Outro({ start, end, slogan = '빛을 설계하고, 공간의 품격을 완성합니다.' }) {
    return (
      <Sprite start={start} end={end}>
        {({ localTime, duration }) => {
          const ease = Easing.easeOutCubic;
          const tIn = clamp(localTime / 0.5, 0, 1);
          const op = ease(tIn);
          const linePct = clamp((localTime - 0.3) / 0.6, 0, 1);
          return (
            <div style={{
              position: 'absolute', inset: 0, opacity: op,
              display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
              zIndex: 6,
            }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 18, marginBottom: 18 }}>
                <svg width="34" height="28" viewBox="0 0 100 83.15" fill={PAPER} fillRule="evenodd"><path d="M82.39 0L78.71 0.35L75.17 1.04L69.97 2.5L61.65 5.76L55.69 8.67L12.69 8.67L12.34 9.71L7.28 38.56L16.3 38.56L16.3 38.83L12.14 43.2L7.56 49.24L3.81 55.41L1.53 60.4L0.28 64.84L0 68.59L0.28 70.74L0.49 70.8L0.97 72.61L2.22 74.69L3.74 76.21L5.62 77.46L7.84 78.29L11.3 78.92L14.49 78.99L17.68 78.71L22.82 77.67L23.58 77.32L24.2 77.32L24.2 77.12L25.73 76.84L25.8 76.63L27.12 76.42L25.87 82.8L26.07 83.15L51.32 83.08L51.6 82.52L54.16 67.2L54.79 64.49L77.67 82.94L87.1 83.08L87.1 82.66L75.94 73.72L58.67 59.36L58.88 59.02L65.67 53.95L71.29 49.17L75.1 45.63L82.04 38.42L94.73 38.42L95.15 35.78L94.94 35.37L85.37 35.51L84.81 35.3L89.04 29.61L96.12 29.54L96.6 26.56L91.12 26.56L91.12 26.21L92.37 24.55L94.11 20.74L97.78 20.6L98.27 17.82L95.35 17.82L96.12 15.12L96.39 11.93L99.38 11.93L99.65 11.37L100 8.95L96.32 8.74L95.84 6.87L94.52 4.51L93.07 2.98L91.4 1.8L89.74 1.04L87.31 0.35ZM82.18 6.17L84.4 6.17L87.38 6.8L89.11 7.84L89.94 8.67L89.81 8.88L71.29 8.81L73.02 8.04L76.77 7ZM64.15 11.93L90.43 11.93L90.43 12.27L89.94 15.12L89.11 17.61L63.11 17.61ZM62.55 20.8L87.59 20.87L83.98 26.56L61.58 26.56ZM61.1 29.68L81.14 29.68L81.41 30.1L79.13 32.73L79.06 33.08L78.5 33.43L76.84 35.37L60.06 35.37L59.99 34.88L60.82 30.17ZM59.36 38.49L73.93 38.63L65.19 46.74L56.66 53.4ZM26.28 38.56L33.77 38.63L28.29 69.49L22.47 71.43L16.16 72.68L11.3 72.68L8.6 71.91L7.84 71.43L6.73 70.11L6.24 68.31L6.24 66.64L6.52 64.7L7.14 62.55L9.22 58.04L12.83 52.29L18.38 45.28L24.76 38.63Z"/></svg>
              </div>
              <div className="serif" style={{ fontSize: 96, color: PAPER, letterSpacing: '0.16em', lineHeight: 1 }}>TAEKWANG</div>
              <div className="serif-it" style={{ fontSize: 24, color: GOLD_S, marginTop: 8, letterSpacing: '0.3em' }}>SAFETY GLASS</div>
              <div style={{ width: 240, height: 1, background: GOLD, margin: '28px 0', transform: `scaleX(${linePct})`, transformOrigin: 'center' }}></div>
              <div className="serif-ko" style={{ fontSize: 22, color: 'rgba(246,243,236,0.85)', letterSpacing: '-0.02em', marginBottom: 32 }}>
                {slogan}
              </div>
              <div style={{ display: 'flex', gap: 28, alignItems: 'center', opacity: clamp((localTime - 0.9) / 0.5, 0, 1) }}>
                <div className="mono" style={{ fontSize: 12, color: 'rgba(246,243,236,0.6)', letterSpacing: '0.18em' }}>
                  T. <span style={{ color: PAPER }}>032-555-7392</span>
                </div>
                <div style={{ width: 1, height: 14, background: 'rgba(246,243,236,0.2)' }}></div>
                <div className="mono" style={{ fontSize: 12, color: 'rgba(246,243,236,0.6)', letterSpacing: '0.18em' }}>
                  <span style={{ color: PAPER }}>TAEKWANGGLASS.COM</span>
                </div>
              </div>
            </div>
          );
        }}
      </Sprite>
    );
  }

  // Title — fade in slogan + product en/ko
  function TitleScene({ start, end, filmNo, eyebrow, big, slogan }) {
    return (
      <Sprite start={start} end={end}>
        {({ localTime, duration }) => {
          const ease = Easing.easeOutCubic;
          const tIn = clamp(localTime / 0.6, 0, 1);
          const tOut = clamp((localTime - (duration - 0.6)) / 0.6, 0, 1);
          const op = (localTime < 0.6) ? ease(tIn) : (localTime > duration - 0.6 ? 1 - ease(tOut) : 1);
          const y = (localTime < 0.6) ? (1 - ease(tIn)) * 20 : 0;
          const linePct = clamp((localTime - 0.4) / 1.0, 0, 1);
          return (
            <div style={{
              position: 'absolute', inset: 0,
              display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
              opacity: op, transform: `translateY(${y}px)`, zIndex: 6,
            }}>
              <div className="serif-it" style={{ color: GOLD_S, fontSize: 22, marginBottom: 24, letterSpacing: '0.04em' }}>— Film N° {filmNo}</div>
              <div className="serif" style={{ fontSize: 144, lineHeight: 1, letterSpacing: '-0.025em', color: PAPER }}>
                {big}<span style={{ fontStyle: 'italic', color: GOLD_S }}>.</span>
              </div>
              <div style={{ width: 320, height: 1, background: GOLD, margin: '32px 0', transform: `scaleX(${linePct})`, transformOrigin: 'left' }}></div>
              <div className="serif-ko" style={{ fontSize: 24, color: 'rgba(246,243,236,0.75)', letterSpacing: '-0.02em' }}>{slogan}</div>
            </div>
          );
        }}
      </Sprite>
    );
  }

  return { GOLD, GOLD_S, PAPER, Letterbox, FrameSlot, FilmHeader, Timecode, CornerBrand, Outro, TitleScene };
})();

window.FilmShell = FilmShell;
