// Kolos Capital — landing app.
//
// The fastroads engine is now inlined into the parent document (see
// index.html), so we talk to its audio singleton directly via
// window.__kolosAudio. No more postMessage bridge.

const { useEffect, useState } = React;

// ── Audio toggle icon ────────────────────────────────────────────────────
function AudioIcon({ muted }) {
  if (muted) {
    return (
      <svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
        <path d="M3 6V10H5L8 12.5V3.5L5 6H3Z" fill="currentColor" />
        <path d="M10.5 6L13.5 9M13.5 6L10.5 9" stroke="currentColor" strokeWidth="0.9" fill="none" strokeLinecap="round" />
      </svg>
    );
  }
  return (
    <svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
      <path d="M3 6V10H5L8 12.5V3.5L5 6H3Z" fill="currentColor" />
      <path d="M10.5 5.5C11.5 6.5 11.5 9.5 10.5 10.5" stroke="currentColor" strokeWidth="0.9" fill="none" strokeLinecap="round" />
      <path d="M12 4C13.5 5.5 13.5 10.5 12 12" stroke="currentColor" strokeWidth="0.9" fill="none" strokeLinecap="round" />
    </svg>
  );
}

// ── Audio helpers (in-process, no iframe) ────────────────────────────────
function applyAudio(muted) {
  // Engine may not have assigned __kolosAudio yet; retry briefly.
  var tries = 0;
  (function tick() {
    var a = window.__kolosAudio;
    if (a) {
      try {
        if (!a.hasInit && typeof a.init === 'function') a.init();
        if (muted && typeof a.mute === 'function') a.mute();
        else if (!muted && typeof a.unmute === 'function') a.unmute();
        var ctx = a.listener && a.listener.context;
        if (!muted && ctx && ctx.state !== 'running' && ctx.resume) ctx.resume();
      } catch (e) {}
      return;
    }
    if (++tries < 80) setTimeout(tick, 200);
  })();
}

// ── App ──────────────────────────────────────────────────────────────────
function App() {
  // Audio defaults OFF; user clicks the bottom-right toggle to enable.
  const [audioOn, setAudioOn] = useState(false);

  // Apply mute state whenever the toggle changes.
  useEffect(() => {
    applyAudio(!audioOn);
  }, [audioOn]);

  // Audio unlock: any first user gesture in this window resumes the
  // AudioContext. Because the engine lives in this same document, the
  // gesture counts as activation directly — no cross-frame dance needed.
  useEffect(() => {
    const unlock = () => {
      try {
        const audio = window.__kolosAudio;
        const ctx = audio && audio.listener && audio.listener.context;
        if (ctx && ctx.state !== 'running' && typeof ctx.resume === 'function') {
          ctx.resume();
        }
      } catch (e) {}
    };
    window.addEventListener('pointerdown', unlock, true);
    window.addEventListener('keydown', unlock, true);
    window.addEventListener('touchstart', unlock, true);
    return () => {
      window.removeEventListener('pointerdown', unlock, true);
      window.removeEventListener('keydown', unlock, true);
      window.removeEventListener('touchstart', unlock, true);
    };
  }, []);

  return (
    <main className="stage" data-screen-label="01 Holding Page">
      {/* Top-left: signature lockup */}
      <div className="corner-tl">
        <Logo />
      </div>

      {/* Centre-left: editorial tagline */}
      <p className="tagline">
        Enjoy the ride.<br />
        Earn on autopilot.
      </p>

      {/* Bottom-left: contact */}
      <div className="corner-bl">
        <a className="btn btn-contact" href="mailto:info@kolos.capital">
          <span>Contact</span>
          <svg viewBox="0 0 14 14" width="12" height="12" aria-hidden="true">
            <path d="M3 11L11 3M11 3H5M11 3V9" stroke="currentColor" strokeWidth="1" fill="none" strokeLinecap="square"></path>
          </svg>
        </a>
      </div>

      {/* Bottom-right: discreet audio toggle */}
      <button
        className="btn btn-audio"
        aria-label={audioOn ? 'Mute ambient audio' : 'Unmute ambient audio'}
        aria-pressed={audioOn}
        onClick={() => setAudioOn(v => !v)}
      >
        <AudioIcon muted={!audioOn} />
      </button>
    </main>
  );
}

ReactDOM.createRoot(document.getElementById('app-root')).render(<App />);
