import { invoke } from '@tauri-apps/api/core';
import type {
  LibraryInfo,
  OkResponse,
  PrefsResponse,
  RecentsResponse,
  NoteResponse,
  VideoMetaResponse,
  SubtitleResponse,
  AvailableSubsResponse,
  EmbeddedSubsResponse,
} from './types';

export const api = {
  selectFolder: () =>
    invoke<LibraryInfo>('select_folder'),

  openFolderPath: (folder: string) =>
    invoke<LibraryInfo>('open_folder_path', { folder }),

  getRecents: () =>
    invoke<RecentsResponse>('get_recents'),

  removeRecent: (path: string) =>
    invoke<OkResponse>('remove_recent', { path }),

  getLibrary: () =>
    invoke<LibraryInfo>('get_library'),

  setCurrent: (index: number, timecode: number = 0) =>
    invoke<OkResponse>('set_current', { index, timecode }),

  tickProgress: (index: number, currentTime: number, duration: number | null, playing: boolean) =>
    invoke<OkResponse>('tick_progress', { index, currentTime, duration, playing }),

  setFolderVolume: (volume: number) =>
    invoke<OkResponse>('set_folder_volume', { volume }),

  setFolderAutoplay: (enabled: boolean) =>
    invoke<OkResponse>('set_folder_autoplay', { enabled }),

  setFolderRate: (rate: number) =>
    invoke<OkResponse>('set_folder_rate', { rate }),

  setOrder: (fids: string[]) =>
    invoke<OkResponse>('set_order', { fids }),

  startDurationScan: () =>
    invoke<OkResponse>('start_duration_scan'),

  getPrefs: () =>
    invoke<PrefsResponse>('get_prefs'),

  setPrefs: (patch: Record<string, unknown>) =>
    invoke<OkResponse>('set_prefs', { patch }),

  setAlwaysOnTop: (enabled: boolean) =>
    invoke<OkResponse>('set_always_on_top', { enabled }),

  saveWindowState: () =>
    invoke<OkResponse>('save_window_state'),

  getNote: (fid: string) =>
    invoke<NoteResponse>('get_note', { fid }),

  setNote: (fid: string, note: string) =>
    invoke<OkResponse>('set_note', { fid, note }),

  getCurrentVideoMeta: () =>
    invoke<VideoMetaResponse>('get_current_video_meta'),

  getCurrentSubtitle: () =>
    invoke<SubtitleResponse>('get_current_subtitle'),

  getEmbeddedSubtitles: () =>
    invoke<EmbeddedSubsResponse>('get_embedded_subtitles'),

  extractEmbeddedSubtitle: (trackIndex: number) =>
    invoke<SubtitleResponse>('extract_embedded_subtitle', { trackIndex }),

  getAvailableSubtitles: () =>
    invoke<AvailableSubsResponse>('get_available_subtitles'),

  loadSidecarSubtitle: (filePath: string) =>
    invoke<SubtitleResponse>('load_sidecar_subtitle', { filePath }),

  chooseSubtitleFile: () =>
    invoke<SubtitleResponse>('choose_subtitle_file'),

  resetWatchProgress: () =>
    invoke<OkResponse>('reset_watch_progress'),
};
