"use client";

import { useCallback, useState } from "react";
import type { Game, SystemId } from "@/lib/types";
import { getConsoleTheme } from "@/lib/consoleThemes";
import { ConsoleBoot } from "./ConsoleBoot";
import { XmbShell } from "./XmbShell";
import { CartridgeShelf } from "./CartridgeShelf";
import { HandheldShell } from "./HandheldShell";
import { ArcadeShell } from "./ArcadeShell";

export function ConsoleShell({
  systemId,
  games,
}: {
  systemId: SystemId;
  games: Game[];
}) {
  const theme = getConsoleTheme(systemId);
  const [ready, setReady] = useState(false);
  const onBootDone = useCallback(() => setReady(true), []);

  if (!ready) {
    return (
      <ConsoleBoot
        kind={theme.kind}
        onDone={onBootDone}
        durationMs={
          theme.kind === "ps3-xmb" || theme.kind === "psp-xmb" ? 1700 : 1200
        }
      />
    );
  }

  switch (theme.kind) {
    case "ps3-xmb":
      return <XmbShell games={games} variant="ps3" title={theme.label} />;
    case "psp-xmb":
      return <XmbShell games={games} variant="psp" title={theme.label} />;
    case "arcade":
      return <ArcadeShell games={games} />;
    case "gb":
    case "gbc":
    case "gba":
    case "nds":
    case "gg":
      return (
        <HandheldShell games={games} kind={theme.kind} label={theme.label} />
      );
    case "nes":
    case "snes":
    case "n64":
    case "genesis":
    case "sms":
    case "saturn":
    case "segacd":
    case "atari2600":
    case "vb":
      return (
        <CartridgeShelf
          games={games}
          kind={theme.kind}
          label={theme.label}
          tagline={theme.tagline}
        />
      );
    default:
      return (
        <CartridgeShelf
          games={games}
          kind="nes"
          label={theme.label}
          tagline={theme.tagline}
        />
      );
  }
}
