import { Application } from 'pixi.js';
import { loadArt } from './render/textures.ts';
import { Game } from './game/game.ts';

function showError(message: string): void {
  const el = document.createElement('pre');
  el.textContent = message;
  el.style.cssText =
    'position:fixed;inset:0;margin:0;padding:24px;color:#f1e3c2;background:#1a1110;font:14px/1.5 monospace;white-space:pre-wrap;overflow:auto;';
  document.body.appendChild(el);
}

async function boot(): Promise<void> {
  const app = new Application();
  await app.init({
    resizeTo: window,
    background: '#0b0708',
    antialias: false,
    roundPixels: true,
    resolution: Math.min(window.devicePixelRatio || 1, 2),
    autoDensity: true,
  });
  document.body.appendChild(app.canvas);

  const art = await loadArt();
  const game = new Game(app, art);

  app.ticker.add((ticker) => {
    // Clamp large deltas (tab switches) so physics and typing never jump.
    game.update(Math.min(ticker.deltaMS / 1000, 1 / 20));
  });
}

boot().catch((err: unknown) => {
  console.error(err);
  showError(`EasternWell failed to start.\n\n${err instanceof Error ? err.stack ?? err.message : String(err)}`);
});
