/**
 * 视频关键帧提取工具类型定义
 * 遵循 Tauri 开发规范的类型系统设计
 */

// 帧提取类型
export enum FrameType {
  /** 第一帧 */
  First = 'First',
  /** 最后一帧 */
  Last = 'Last',
  /** 自定义时间点 */
  Custom = 'Custom',
  /** 多个时间点 */
  Multiple = 'Multiple',
}

// 时间点配置
export type TimePoint = 
  | { type: 'seconds'; value: number }      // 秒数
  | { type: 'percentage'; value: number }   // 百分比 (0.0-1.0)
  | { type: 'frame'; value: number };       // 帧数

// 图片格式
export enum ImageFormat {
  Jpg = 'jpg',
  Png = 'png',
  WebP = 'webp',
  Bmp = 'bmp',
}

// 图片质量设置
export interface ImageQuality {
  format: ImageFormat;
  quality: number; // 1-100 for JPEG, 0-9 for PNG
  compression_level?: number; // WebP compression level
}

// 输出尺寸配置
export interface OutputSize {
  width?: number;
  height?: number;
  maintain_aspect_ratio: boolean;
  scale_filter?: 'lanczos' | 'bicubic' | 'bilinear' | 'nearest';
}

// 帧提取配置
export interface FrameExtractionConfig {
  frame_type: FrameType;
  time_points?: TimePoint[];
  custom_time?: number; // 自定义时间点（秒）
  output_format: ImageQuality;
  output_size?: OutputSize;
  output_directory: string;
  filename_pattern: string; // 支持变量: {name}, {timestamp}, {frame_type}, {index}
  overwrite_existing: boolean;
}

// 视频文件信息
export interface VideoFileInfo {
  path: string;
  filename: string;
  size: number;
  duration: number;
  width: number;
  height: number;
  fps: number;
  format: string;
  is_valid: boolean;
  error_message?: string;
}

// 提取任务状态
export enum ExtractionTaskStatus {
  Pending = 'Pending',
  Running = 'Running',
  Completed = 'Completed',
  Failed = 'Failed',
  Cancelled = 'Cancelled',
}

// 单个帧提取结果
export interface FrameExtractionResult {
  video_path: string;
  frame_type: FrameType;
  timestamp: number;
  output_path: string;
  success: boolean;
  error_message?: string;
  processing_time_ms: number;
  file_size: number;
}

// 批量提取任务
export interface BatchExtractionTask {
  task_id: string;
  video_files: VideoFileInfo[];
  config: FrameExtractionConfig;
  status: ExtractionTaskStatus;
  progress: BatchExtractionProgress;
  results: FrameExtractionResult[];
  created_at: string;
  started_at?: string;
  completed_at?: string;
  error_message?: string;
}

// 批量提取进度
export interface BatchExtractionProgress {
  total_files: number;
  processed_files: number;
  failed_files: number;
  current_file?: string;
  current_frame_type?: FrameType;
  progress_percentage: number;
  estimated_remaining_time?: number; // 秒
}

// 提取统计信息
export interface ExtractionStatistics {
  total_videos_processed: number;
  total_frames_extracted: number;
  total_processing_time_ms: number;
  success_rate: number;
  average_processing_time_per_video: number;
  file_size_distribution: {
    format: ImageFormat;
    count: number;
    total_size: number;
    average_size: number;
  }[];
}

// 预设配置
export interface FrameExtractionPreset {
  id: string;
  name: string;
  description: string;
  config: FrameExtractionConfig;
  is_default: boolean;
  created_at: string;
}

// 文件夹扫描请求
export interface FolderScanRequest {
  folder_path: string;
  recursive: boolean;
  supported_formats: string[];
}

// 帧提取请求
export interface FrameExtractionRequest {
  video_files: string[];
  config: FrameExtractionConfig;
}

// 预览请求
export interface FramePreviewRequest {
  video_path: string;
  timestamp: number;
  output_size?: OutputSize;
}

// 预览结果
export interface FramePreviewResult {
  success: boolean;
  preview_data?: string; // Base64 encoded image
  error_message?: string;
  timestamp: number;
  original_size: [number, number];
  preview_size: [number, number];
}

// 默认配置
export const DEFAULT_FRAME_EXTRACTION_CONFIG: FrameExtractionConfig = {
  frame_type: FrameType.First,
  output_format: {
    format: ImageFormat.Jpg,
    quality: 85,
  },
  output_size: {
    maintain_aspect_ratio: true,
    scale_filter: 'lanczos',
  },
  output_directory: '',
  filename_pattern: '{name}_{frame_type}',
  overwrite_existing: false,
};

// 支持的视频格式
export const SUPPORTED_VIDEO_FORMATS = [
  'mp4', 'avi', 'mov', 'mkv', 'wmv', 'flv', 'webm', 'm4v', '3gp', 'ts', 'mts'
];

// 预设配置列表
export const FRAME_EXTRACTION_PRESETS: FrameExtractionPreset[] = [
  {
    id: 'first-frame-hq',
    name: '第一帧高质量',
    description: '提取视频第一帧，高质量JPEG格式',
    config: {
      ...DEFAULT_FRAME_EXTRACTION_CONFIG,
      frame_type: FrameType.First,
      output_format: { format: ImageFormat.Jpg, quality: 95 },
    },
    is_default: true,
    created_at: new Date().toISOString(),
  },
  {
    id: 'last-frame-png',
    name: '最后一帧',
    description: '提取视频最后一帧',
    config: {
      ...DEFAULT_FRAME_EXTRACTION_CONFIG,
      frame_type: FrameType.Last,
      output_format: { format: ImageFormat.Jpg, quality: 95 },
    },
    is_default: false,
    created_at: new Date().toISOString(),
  },
  {
    id: 'middle-frame-webp',
    name: '中间帧',
    description: '提取视频中间帧',
    config: {
      ...DEFAULT_FRAME_EXTRACTION_CONFIG,
      frame_type: FrameType.Custom,
      custom_time: 0, // 将在运行时计算为视频长度的50%
      output_format: { format: ImageFormat.Jpg, quality: 95 },
    },
    is_default: false,
    created_at: new Date().toISOString(),
  },
];
