import { useCallback } from "react";
import type { CaptureOptions, CaptureResult } from "../../../types/global";
import { captureSelectedSourceAction, exportCurrentGoodAction, loadStoredArtifactSnapshotAction, refreshCaptureSourcesAction, runDemoScanAction, showOverlayAction } from "../services/appControllerService";
import type { AppControllerContext } from "../services/appControllerService";

interface AppControllerActionsInput {
  appControllerContext: AppControllerContext;
}

export interface AppControllerActionsResult {
  loadStoredArtifactSnapshot: () => Promise<void>;
  refreshCaptureSources: () => Promise<void>;
  captureSelectedSource: (delayMs?: number, focusGenshin?: boolean, options?: CaptureOptions) => Promise<CaptureResult | null>;
  runDemoScan: () => Promise<void>;
  exportCurrentGood: () => Promise<void>;
  showOverlay: () => Promise<void>;
}

export function useAppControllerActions({
  appControllerContext,
}: AppControllerActionsInput): AppControllerActionsResult {
  const loadStoredArtifactSnapshot = useCallback(async () => {
    await loadStoredArtifactSnapshotAction(appControllerContext);
  }, [appControllerContext]);

  const refreshCaptureSources = useCallback(async () => {
    await refreshCaptureSourcesAction(appControllerContext);
  }, [appControllerContext]);

  const captureSelectedSource = useCallback(async (delayMs = 0, focusGenshin = false, options?: CaptureOptions) => {
    return captureSelectedSourceAction(appControllerContext, delayMs, focusGenshin, options);
  }, [appControllerContext]);

  const runDemoScan = useCallback(async () => {
    await runDemoScanAction(appControllerContext, captureSelectedSource);
  }, [appControllerContext, captureSelectedSource]);

  const exportCurrentGood = useCallback(async () => {
    await exportCurrentGoodAction(appControllerContext);
  }, [appControllerContext]);

  const showOverlay = useCallback(async () => {
    await showOverlayAction(appControllerContext);
  }, [appControllerContext]);

  return {
    loadStoredArtifactSnapshot,
    refreshCaptureSources,
    captureSelectedSource,
    runDemoScan,
    exportCurrentGood,
    showOverlay,
  };
}
