import { Task } from "@lit/task";

import { EF_INTERACTIVE } from "../../../EF_INTERACTIVE";
import { EF_RENDERING } from "../../../EF_RENDERING";
import type { VideoRendition } from "../../../transcoding/types";
import type { EFVideo } from "../../EFVideo";
import {
  type MediaBufferConfig,
  type MediaBufferState,
  manageMediaBuffer,
} from "../shared/BufferUtils";
import {
  getLatestMediaEngine,
  getVideoRendition,
} from "../tasks/makeMediaEngineTask";

/**
 * Configuration for video buffering - extends the generic interface
 */
export interface VideoBufferConfig extends MediaBufferConfig {}

/**
 * State of the video buffer - uses the generic interface
 */
export interface VideoBufferState extends MediaBufferState {}

type VideoBufferTask = Task<readonly [number], VideoBufferState>;
export const makeVideoBufferTask = (host: EFVideo): VideoBufferTask => {
  let currentState: VideoBufferState = {
    currentSeekTimeMs: 0,
    activeRequests: new Set(),
    cachedSegments: new Set(),
    requestQueue: [],
  };

  return new Task(host, {
    autoRun: EF_INTERACTIVE, // Start buffering automatically when media is ready
    args: () => [host.desiredSeekTimeMs] as const,
    onError: (error) => {
      console.error("videoBufferTask error", error);
    },
    onComplete: (value) => {
      currentState = value;
    },
    task: async ([seekTimeMs], { signal }) => {
      // Use EFMedia properties directly - no hardcoded duplication!
      const currentConfig: VideoBufferConfig = {
        bufferDurationMs: host.videoBufferDurationMs,
        maxParallelFetches: host.maxVideoBufferFetches,
        enableBuffering: host.enableVideoBuffering && !EF_RENDERING,
      };

      return manageMediaBuffer<VideoRendition>(
        seekTimeMs,
        currentConfig,
        currentState,
        (host as any).intrinsicDurationMs || 10000,
        signal,
        {
          computeSegmentId: async (timeMs, rendition) => {
            // Use media engine's computeSegmentId
            const mediaEngine = await getLatestMediaEngine(host, signal);
            return mediaEngine.computeSegmentId(timeMs, rendition);
          },
          fetchSegment: async (segmentId, rendition) => {
            const mediaEngine = await getLatestMediaEngine(host, signal);
            return mediaEngine.fetchMediaSegment(segmentId, rendition);
          },
          getRendition: async () => {
            // Get real video rendition from media engine
            const mediaEngine = await getLatestMediaEngine(host, signal);
            return getVideoRendition(mediaEngine);
          },
          logError: console.error,
        },
      );
    },
  });
};
