"use client";

import { useCallback, useState } from "react";
import type { Game } from "@/lib/types";
import type { UiSkinDef } from "@/lib/uiSkins";
import { getConsoleTheme } from "@/lib/consoleThemes";
import { ConsoleBoot } from "@/components/console/ConsoleBoot";
import { XmbShell } from "@/components/console/XmbShell";
import { CartridgeShelf } from "@/components/console/CartridgeShelf";
import { HandheldShell } from "@/components/console/HandheldShell";
import { ArcadeShell } from "@/components/console/ArcadeShell";

/** Fullscreen console cabinet as the player's chosen home UI. */
export function CabinetShell({
  skin,
  games,
  skipBoot = false,
}: {
  skin: UiSkinDef;
  games: Game[];
  skipBoot?: boolean;
}) {
  const themeSystem = skin.themeSystem ?? "nes";
  const theme = getConsoleTheme(themeSystem);
  const [ready, setReady] = useState(skipBoot);
  const onBootDone = useCallback(() => setReady(true), []);

  if (skin.mode !== "console") return null;

  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="HollowCade" />;
    case "psp-xmb":
      return <XmbShell games={games} variant="psp" title="HollowCade" />;
    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} />
      );
    default:
      return (
        <CartridgeShelf
          games={games}
          kind={theme.kind}
          label={theme.label}
          tagline="Your HollowCade library"
        />
      );
  }
}
