{"size": 2821, "ext": "js", "max_stars_repo_path": "src/services/StorageHandler.js", "max_stars_repo_name": "Tomas-Sereikis/youtube-music-app", "max_stars_repo_stars_event_min_datetime": "2016-02-18T13:01:58.000Z", "max_stars_repo_stars_event_max_datetime": "2018-04-07T07:53:30.000Z", "max_issues_repo_path": "src/services/StorageHandler.js", "max_issues_repo_name": "Tomas-Sereikis/youtube-music-app", "max_issues_repo_issues_event_min_datetime": "2016-05-30T10:44:47.000Z", "max_issues_repo_issues_event_max_datetime": "2016-05-30T10:44:47.000Z", "max_forks_repo_path": "src/services/StorageHandler.js", "max_forks_repo_name": "Tomas-Sereikis/youtube-music-app", "max_forks_repo_forks_event_min_datetime": "2016-03-18T18:21:53.000Z", "max_forks_repo_forks_event_max_datetime": "2018-05-12T08:43:55.000Z"}
{"max_stars_count": 15, "max_issues_count": 1, "max_forks_count": 9, "avg_line_length": 21.534351145, "max_line_length": 82, "alphanum_fraction": 0.6678482808}
'use strict';

import JsonStorage from './JsonStorage';
import FileSystem from 'react-native-fs';

/**
 * Get storage path
 * @param {string} [filename]
 * @returns {*}
 */
function downloadsPath(filename = '') {
  return `${FileSystem.DocumentDirectoryPath}/downloads/${filename}`;
}

/**
 * Get content in downloads directory
 * @returns {Promise.<Array>}
 */
function downloads() {
  return FileSystem.readdir(downloadsPath()).then(response => {
    return response;
  }, e => {
    return Promise.resolve([]);
  });
}

/**
 * Download video from url
 * @param {string} audioUrl
 * @param {string} videoId
 * @param {Function} progressCallback
 */
function downloadVideo(audioUrl, videoId, progressCallback) {
  return FileSystem.mkdir(downloadsPath()).then(e => {
    var lastProgress;
    var filename = `${videoId}.mp3`;
    return FileSystem.downloadFile(audioUrl, downloadsPath(filename), null, e => {
      var progress = Math.floor(e.bytesWritten / e.contentLength * 100);
      if (progress !== lastProgress) {
        lastProgress = progress;
        progressCallback({progress});
      }
    });
  });
}

/**
 * @param {string} thumbnailUrl
 * @param {string} videoId
 * @returns {Promise}
 */
function downloadThumbnail(thumbnailUrl, videoId) {
  var filename = `${videoId}.jpg`;
  return FileSystem.mkdir(downloadsPath()).then(e => {
    return FileSystem.downloadFile(thumbnailUrl, downloadsPath(filename));
  });
}

function unlink(filepath) {
  return FileSystem.unlink(filepath);
}

/**
 * @param videoId
 * @returns {*}
 */
function videoPath(videoId) {
  return downloadsPath(`${videoId}.mp3`);
}

/**
 * @param videoId
 * @returns {*}
 */
function thumbnailPath(videoId) {
  return downloadsPath(`${videoId}.jpg`);
}

/**
 * @returns {Promise}
 */
function getVideos() {
  return JsonStorage.getItem('videos', e => []);
}

/**
 * Get video content
 * @param videoId
 * @returns {Promise}
 */
function getVideoContent(videoId) {
  return JsonStorage.getItem(`video.${videoId}`);
}

/**
 * Set video content
 * @param {string} videoId
 * @param {Object} content
 * @returns {Promise.<Object|null>}
 */
function setVideoContent(videoId, content) {
  return JsonStorage.setItem(`video.${videoId}`, content)
    .then(getVideos)
    .then(res => res.concat(content))
    .then(res => JsonStorage.setItem('videos', res));
}

/**
 * Remove video
 * @param videoId
 * @returns {Promise}
 */
function removeVideoContent(videoId) {
  return JsonStorage.removeItem(`video.${videoId}`)
    .then(getVideos)
    .then(res => res.filter(video => video.videoId !== videoId))
    .then(res => JsonStorage.setItem('videos', res));
}

export default {
  unlink,
  downloads,
  downloadVideo,
  downloadThumbnail,
  videoPath,
  thumbnailPath,
  getVideos,
  getVideoContent,
  setVideoContent,
  removeVideoContent
};