// nostromo.jsx
// LOADING NOSTROMO boot screen — shown ONLY when switching INTO MU/TH/UR mode
// (the `mother`/`nostromo` console command). Imitates a cold CRT warming up:
//
//   glitch  (0–360ms)  — cold-start tearing: RGB split, horizontal jitter,
//                        heavy noise, image barely breaking through black.
//   warm    (360–1900) — slow phosphor ramp: brightness/contrast climb from
//                        near-black, a bright scan sweep rolls down once, gentle
//                        vertical roll settles. The "slow tube response" look.
//   fade    (1900–2400) — picture stabilises, whole overlay fades to reveal the
//                        now green-themed site beneath.
//
// Exit from MU/TH/UR is a HARD cut (handled in enhance.jsx — no overlay), per
// brief. A 2s audio sting (assets/audio/nostromo.*) plays at boot via the SFX
// engine, so it respects the mute state.

(function () {
  const { useState, useEffect, useRef } = React;

  function NostromoBoot() {
    const [phase, setPhase] = useState(null); // null | 'glitch' | 'warm' | 'fade'
    const timers = useRef([]);

    useEffect(() => {
      function boot() {
        // reset any in-flight run
        timers.current.forEach(clearTimeout);
        timers.current = [];
        setPhase('glitch');
        if (window.Audio && window.Audio.playSample) window.Audio.playSample('nostromo');
        timers.current.push(setTimeout(() => setPhase('warm'), 360));
        timers.current.push(setTimeout(() => setPhase('fade'), 1900));
        timers.current.push(setTimeout(() => setPhase(null), 2400));
      }
      window.addEventListener('dys:nostromo-boot', boot);
      return () => {
        window.removeEventListener('dys:nostromo-boot', boot);
        timers.current.forEach(clearTimeout);
      };
    }, []);

    if (!phase) return null;

    const res = (id, path) => (window.__res ? window.__res(id, path) : path);
    return (
      <div className={`nostromo-boot is-${phase}`} aria-hidden="true">
        <div className="nostromo-boot__screen">
          {/* triple-layered image for the cold-start RGB channel split */}
          <img className="nostromo-boot__img nostromo-boot__img--r" src={res('nostromoLoading', 'assets/art/nostromo-loading.png')} alt="" />
          <img className="nostromo-boot__img nostromo-boot__img--b" src={res('nostromoLoading', 'assets/art/nostromo-loading.png')} alt="" />
          <img className="nostromo-boot__img nostromo-boot__img--main" src={res('nostromoLoading', 'assets/art/nostromo-loading.png')} alt="" />
          <div className="nostromo-boot__scan"></div>
          <div className="nostromo-boot__sweep"></div>
          <div className="nostromo-boot__noise"></div>
          <div className="nostromo-boot__vignette"></div>
        </div>
        <div className="nostromo-boot__label">
          <span className="nostromo-boot__label-txt">LOADING MU/TH/UR 6000</span>
          <span className="nostromo-boot__label-dots"></span>
        </div>
      </div>
    );
  }

  Object.assign(window, { NostromoBoot });
})();
