var SpotifyWebApi = require('spotify-web-api-node');
var Player = require('player');

const searchWords = "shape of you artist:Ed Sheeran";
//"sugar artist:Maroon 5";
//"I don't wanna live forever";

var logger;
var spotifyApi;
var player = new Player();

var SpotifyPlayer = function(option, log) {
  this.logger = log;
  this.config = option;

  if (!logger) {
    logger = require('humix-logger').createLogger('spotify-player', { consoleLevel: 'debug', filename: 'spotify-player.log' });
  }

  logger.info('SpotifyPlayer instance created.');
};

SpotifyPlayer.prototype.init = function() {
  // credentials are optional
  this.spotifyApi = new SpotifyWebApi({
    clientId : this.config.clientId,
    clientSecret : this.config.clientSecret,
    //  redirectUri : 'http://www.example.com/callback'
  });

  // Retrieve an access token
  this.spotifyApi.clientCredentialsGrant()
    .then(function(data) {
      logger.info('The access token expires in ' + data.body['expires_in']);
      logger.info('The access token is ' + data.body['access_token']);

      // Save the access token so that it's used in future calls
      this.spotifyApi.setAccessToken(data.body['access_token']);
    }, function(err) {
      logger.info('Something went wrong when retrieving an access token', err.message);
  });

  // event: on playing 
  player.on('playing', function(item) {
    logger.info('im playing... src: ' + item);
  });
  
  // event: on playend 
  player.on('playend', function(item) {
    // return a playend item 
    logger.info('src: ' + item + ' play done, switching to next one ...');

  });
  
  // event: on error 
  player.on('error', function(err) {
    // when error occurs 
    logger.info(err);
  });
}

SpotifyPlayer.prototype.playSong = function(songName, artistName) {
  
  var self = this;

  var query = songName;
  if (artistName) {
      query += ' artist:' + artistName;
  }

  self.spotifyApi.search(query, ['track'], {limit: 5, offset: 1})
    .then(function(data) {
      logger.info('====== Track for "' + query + '" ======\n');
      if (data.body.tracks.items.length > 0) {
        var trackObj = data.body.tracks.items[0];
        
        logger.info(' Album: ' + trackObj.album.name + '\n');
        logger.info(' Artist: ' + trackObj.artists[0].name + '\n');
        logger.info(' Track: ' + trackObj.name + '\n');

        player.add(trackObj.preview_url);
        player.play();
      }
    }, function(err) {
      logger.error(err);
    });
};

SpotifyPlayer.prototype.pausePlaying = function() {
  var self = this;

  self.player.pause();
};

SpotifyPlayer.prototype.stopPlaying = function() {
  var self = this;

  self.player.stop();
};

// Search artist
/*
spotifyApi.search("Ed Sheeran", ['artist'], { limit : 5, offset : 1 })
  .then(function(data) {
    console.log('Artist for "Ed Shreen": ', data.body);
  }, function(err) {
    console.error(err);
  });*/

// Search Track
/*

*/
// Get Ed Sheeran' albums
/*
spotifyApi.getArtistAlbums('607oAIdIe0j0JCsch5U3nD')
  .then(function(data) {
    console.log('Artist albums', data.body);
  }, function(err) {
    console.error(err);
  });
*/

module.exports = SpotifyPlayer;
@@ -2045,18 +2045,16 @@
 %22 ======
-%5Cn
 ');%0A    
@@ -2203,31 +2203,24 @@
 j.album.name
- + '%5Cn'
 );%0A        l
@@ -2268,23 +2268,16 @@
 %5B0%5D.name
- + '%5Cn'
 );%0A     
@@ -2321,15 +2321,8 @@
 name
- + '%5Cn'
 );%0A%0A

Remove newlien for humix-logger