import { useMemo, useState } from "react";
import type { AppSnapshot } from "../../../types/domain";
import type { CaptureResult, CaptureSourceInfo } from "../../../types/global";
import { createRendererRepositories } from "../../../infrastructure/repositories/rendererBridgeRepositories";
import { createAppMetrics } from "../services/appMetricsService";
import type { AppControllerResult } from "../types";
import { createInitialSnapshot, createAppControllerContext } from "../services/appControllerService";
import { useAppControllerActions } from "./useAppControllerActions";
import { useAppControllerLifecycle } from "./useAppControllerLifecycle";
import { type NavigationId } from "../../layout/types";

export function useAppControllerState(): AppControllerResult {
  const isOverlay = new URLSearchParams(window.location.search).get("overlay") === "1";
  const repositories = useMemo(() => createRendererRepositories(), []);

  const [snapshot, setSnapshot] = useState<AppSnapshot>(createInitialSnapshot);
  const [activeView, setActiveView] = useState<NavigationId>("scan");
  const [isScanning, setIsScanning] = useState(false);
  const [captureSources, setCaptureSources] = useState<CaptureSourceInfo[]>([]);
  const [selectedSourceId, setSelectedSourceId] = useState("");
  const [latestCapture, setLatestCapture] = useState<CaptureResult | null>(null);
  const [topbarStatus, setTopbarStatus] = useState(
    repositories
      ? "Electron bridge connected. Refresh sources after Genshin is open."
      : "Electron bridge missing. Open the Electron app window, not the browser preview URL.",
  );
  const appControllerContext = useMemo(
    () =>
      createAppControllerContext(repositories, {
        isOverlay,
        selectedSourceId,
        snapshot,
        setCaptureSources,
        setSelectedSourceId,
        setTopbarStatus,
        setSnapshot,
        setLatestCapture,
        setIsScanning,
      }),
    [
      repositories,
      isOverlay,
      selectedSourceId,
      snapshot,
      setCaptureSources,
      setSelectedSourceId,
      setTopbarStatus,
      setSnapshot,
      setLatestCapture,
      setIsScanning,
    ],
  );

  const bridgeReady = Boolean(repositories?.isAvailable);
  const canExportGood = Boolean(repositories?.canExportGood);
  const canShowOverlay = Boolean(repositories?.canShowOverlay);
  const { cards: metricCards } = useMemo(() => createAppMetrics(snapshot), [snapshot]);

  useAppControllerLifecycle({ appControllerContext });

  const {
    loadStoredArtifactSnapshot,
    refreshCaptureSources,
    captureSelectedSource,
    runDemoScan,
    exportCurrentGood,
    showOverlay,
  } = useAppControllerActions({ appControllerContext });

  return {
    isOverlay,
    snapshot,
    activeView,
    setActiveView,
    isScanning,
    captureSources,
    selectedSourceId,
    setSelectedSourceId,
    latestCapture,
    topbarStatus,
    bridgeReady,
    canExportGood,
    canShowOverlay,
    metricCards,
    loadStoredArtifactSnapshot,
    refreshCaptureSources,
    captureSelectedSource,
    runDemoScan,
    exportCurrentGood,
    showOverlay,
  };
}
