import { ipcMain } from "electron";
import type {
  BooleanResult,
  FocusGenshinResult,
  RuntimeInfo,
  SaveSnapshotResult,
  ScannerStatusPayload,
} from "../../src/types/global.js";
import type { AppSnapshot } from "../../src/types/domain.js";

interface AppCommandDependencies {
  focusMainWindow: () => BooleanResult;
  moveMainWindowOffGenshin: () => Promise<void>;
  focusGenshinForScanStart: () => Promise<FocusGenshinResult>;
  publishScannerStatus: (status: ScannerStatusPayload) => Promise<BooleanResult>;
  getRuntimeInfo: () => Promise<RuntimeInfo>;
  loadSnapshot: () => Promise<AppSnapshot | null>;
  saveSnapshot: (snapshot: AppSnapshot) => Promise<SaveSnapshotResult>;
  runMockScan: () => Promise<AppSnapshot | null>;
  showOverlay: () => Promise<BooleanResult>;
  hideOverlay: () => Promise<BooleanResult>;
}

export function registerAppHandlers({
  focusMainWindow,
  moveMainWindowOffGenshin,
  focusGenshinForScanStart,
  publishScannerStatus,
  getRuntimeInfo,
  loadSnapshot,
  saveSnapshot,
  runMockScan,
  showOverlay,
  hideOverlay,
}: AppCommandDependencies) {
  ipcMain.handle("app:focusMainWindow", async () => focusMainWindow());
  ipcMain.handle("automation:focusGenshin", async () => {
    await moveMainWindowOffGenshin();
    return focusGenshinForScanStart();
  });
  ipcMain.handle("scanner:publishStatus", async (_event, status: ScannerStatusPayload) => {
    await publishScannerStatus(status);
    return { ok: true };
  });
  ipcMain.handle("app:getRuntimeInfo", async () => getRuntimeInfo());
  ipcMain.handle("snapshot:load", async () => loadSnapshot());
  ipcMain.handle("snapshot:save", async (_event, snapshot: AppSnapshot) => saveSnapshot(snapshot));
  ipcMain.handle("scan:runMock", async () => runMockScan());
  ipcMain.handle("overlay:show", () => showOverlay());
  ipcMain.handle("overlay:hide", () => hideOverlay());
}
