import RNFetchBlob from "rn-fetch-blob";

export enum SystemDir {
  Pictures,
  Downloads
}

export const getSystemDir = (type: SystemDir) => {
  let directory;
  switch (type) {
    case SystemDir.Pictures:
      directory = RNFetchBlob.fs.dirs.PictureDir;
      break;
    case SystemDir.Downloads:
      directory = RNFetchBlob.fs.dirs.DownloadDir;
      break;
  }
  return directory;
}

export const downloadFile = (fileName: string, url: string, directory: string) => {
  let options = {
    fileCache: true,
    addAndroidDownloads: {
      useDownloadManager: true,
      notification: true,
      path: `${directory}/${fileName}`,
      description: `Downloading file ${fileName}`
    }
  }
  RNFetchBlob.config(options)
    .fetch("GET", url)
    .catch(err => {
      console.error("Download failed:", err);
    });
}