{"size": 3287, "ext": "js", "max_stars_repo_path": "services/spotify.js", "max_stars_repo_name": "behagoras/express-api", "max_stars_repo_stars_event_min_datetime": "2019-10-21T23:00:19.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-08T18:25:24.000Z", "max_issues_repo_path": "services/spotify.js", "max_issues_repo_name": "behagoras/express-api", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "services/spotify.js", "max_forks_repo_name": "behagoras/express-api", "max_forks_repo_forks_event_min_datetime": "2019-11-06T17:30:47.000Z", "max_forks_repo_forks_event_max_datetime": "2019-11-06T17:30:47.000Z"}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 29.3482142857, "max_line_length": 118, "alphanum_fraction": 0.5932461211}
require('dotenv').config();
const Spotify = require('node-spotify-api')
const { config } = require('../config/index')

const SONG_PREVIEW_TIME = 30000;

class SpotifyService {
  constructor() {
    this.spotify = new Spotify({
      id: config.spotifyClientID,
      secret: config.spotifyClientSecret
    })
  }

  async getTrack(query, limit = 1) {
    if (!query) return;
    const response = await this.spotify.search({ type: 'track', query, limit })

    const obj = response.tracks.items.map(item => {
      let artists;
      item.artists.map(artist => {
        artists = !artists ? artists = artist.name : `${artists  }, ${artist.name}`
        return artists;
      })
      return {
        name: item.name,
        preview: item.preview_url,
        artist: artists
      };
    })

    // eslint-disable-next-line consistent-return
    return obj;
  }

  // async getArtist() {
    
  // }

  async getDiscover() {
    const response = await this.spotify.request('https://api.spotify.com/v1/playlists/37i9dQZF1DX68aCfKW9xMy')
    
    const obj = response.tracks.items.map(item => {
      let artists;
      // let albums;
      // console.log (item);
      item.track.artists.map(artist => {
        artists = !artists ? artists = artist.name : `${artists  }, ${artist.name}`;
        return artists;
    })
      // item.track.album.map(album => albums = !albums ? albums = album.name : `${albums  }, ${album.name}`)
      // item.track.artists.map(artist => artists = !artists ? artists = artist.name : `${artists  }, ${artist.name}`)
      // item.track.artists.map(artist => artists = !artists ? artists = artist.name : `${artists  }, ${artist.name}`)
      const getStringFromMs=(ms)=>{

        const SECONDS_TO_MS=1000;
        const MINUTES_TO_MS=SECONDS_TO_MS*60;
        const HOURS_TO_MS=MINUTES_TO_MS*60;

        let hours = Math.floor(ms/HOURS_TO_MS);
        hours= hours>=10 ? hours: `0${hours}`; 
        let minutes = Math.floor(ms%HOURS_TO_MS/MINUTES_TO_MS);
        minutes= minutes>=10 ? minutes: `0${minutes}`; 
        let seconds = Math.floor(ms%HOURS_TO_MS%MINUTES_TO_MS/SECONDS_TO_MS);
        seconds= seconds>=10 ? seconds: `0${seconds}`; 

        

        // eslint-disable-next-line radix
        return parseInt(hours) ? `${hours}:${minutes}:${seconds}` : `${minutes}:${seconds}`;
      }
        return {
          _id:item.track.href,
          artist: artists,
          name: item.track.name,
          preview: item.track.preview_url,
          images:item.track.album.images,
          album:item.track.album.name,
          durationMs:SONG_PREVIEW_TIME,
          durationStr:getStringFromMs(SONG_PREVIEW_TIME),
        };
            
    })

    return obj;
  }

  async getPlaylist() {
    const response = await this.spotify.request('https://api.spotify.com/v1/playlists/37i9dQZF1DX68aCfKW9xMy')
    
    const obj = response.tracks.items.map(item => {
      let artists;
      item.artists.map(artist => {
        artists = !artists ? artists = artist.name : `${artists  }, ${artist.name}`
        return artists;
      })
      return {
        name: item.name,
        preview: item.preview_url,
        artist: artists
      };
    })

    return obj;
  }

  // async getAlbums() {

  // }
}

module.exports = SpotifyService;