import type { SliceFacts } from "../sliceController";
import type { GameSliceSession, SliceEvent, SliceFittedModule, SliceTarget } from "../types";
import type { SystemPointOfInterest, Vec3 } from "../../r3f/shared/types";
import type { LocalWaypoint } from "../../r3f/movement/movementState";
import { metricLabel, slicePanel } from "./sliceStyles";
import { SliceProgressBar } from "./SliceProgressBar";
import { SliceStationStage } from "./SliceStationStage";
import { SliceFlightStage } from "./SliceFlightStage";
import { SliceMiningStage } from "./SliceMiningStage";
import { SliceServicesStage } from "./SliceServicesStage";
import { SliceCombatStage } from "./SliceCombatStage";

function OperationStage({ session, progress }: { session: GameSliceSession; progress: number }) {
  const operation = session.activeOperation;
  const title = operation?.kind === "undocking" ? "Undocking" : operation?.kind === "docking" ? "Docking" : "Ship Operation";
  const detail = operation?.kind === "docking" ? `Approaching ${operation.stationName}.` : "Station traffic control is sequencing the ship.";
  return (
    <section className="slice-loop-panel" style={{ ...slicePanel, height: "100%", minHeight: 0, padding: 18, display: "grid", placeItems: "center", boxSizing: "border-box" }}>
      <div style={{ width: "min(560px, 100%)", textAlign: "center" }}>
        <div style={metricLabel}>Traffic Control</div>
        <h1 style={{ margin: "8px 0", color: "#f8fafc", fontSize: 30, letterSpacing: 0 }}>{title}</h1>
        <p style={{ color: "#94a3b8" }}>{detail}</p>
        <SliceProgressBar value={progress} />
      </div>
    </section>
  );
}

export function SliceStage({
  session,
  facts,
  operationProgress,
  emit,
  onOpenService,
  onCloseService,
  onRefine,
  onUpdateFit,
  onSell,
  onPoiTarget,
  onSystemPoiTarget,
  onSelectTarget,
  onManualTarget,
  onApproachTarget,
  onDockAtTarget,
  onMineTarget,
}: {
  session: GameSliceSession;
  facts: SliceFacts;
  operationProgress: number;
  emit: (event: SliceEvent) => void;
  onOpenService: (service: "refining" | "fitting" | "market") => void;
  onCloseService: () => void;
  onRefine: (quantity: number) => void;
  onUpdateFit: (modules: SliceFittedModule[]) => void;
  onSell: (item: string, quantity: number, unitPrice: number) => void;
  onPoiTarget: (poi: SystemPointOfInterest, position: Vec3) => void;
  onSystemPoiTarget: (systemId: string, poi: SystemPointOfInterest, position: Vec3) => void;
  onSelectTarget: (target: SliceTarget) => void;
  onManualTarget: (waypoint: LocalWaypoint) => void;
  onApproachTarget: (target: SliceTarget) => void;
  onDockAtTarget: (target: SliceTarget) => void;
  onMineTarget: (target: SliceTarget) => void;
}) {
  if (session.mode === "services") {
    return <SliceServicesStage session={session} onClose={onCloseService} onRefine={onRefine} onUpdateFit={onUpdateFit} onSell={onSell} />;
  }
  if (session.mode === "mining") return <SliceMiningStage session={session} progress={operationProgress} />;
  if (session.mode === "undocking" || session.mode === "docking") return <OperationStage session={session} progress={operationProgress} />;
  if (session.mode === "combat") return <SliceCombatStage emit={emit} />;
  if (session.mode === "station") return <SliceStationStage session={session} facts={facts} onOpenService={onOpenService} />;
  return <SliceFlightStage session={session} operationProgress={operationProgress} onPoiTarget={onPoiTarget} onSystemPoiTarget={onSystemPoiTarget} onSelectTarget={onSelectTarget} onManualTarget={onManualTarget} onApproachTarget={onApproachTarget} onDockAtTarget={onDockAtTarget} onMineTarget={onMineTarget} />;
}
