'use strict';
const playPause = require('./playpause');
const commonFunctions = require('./commonFunctions');
const Promise = require('bluebird');
const debug = require('debug')('helpers:playlist');


function playPlaylist(player, playlistName, timeout) {
    let trackChanged;
    let changeStateResult;
    const promiseTimeout = timeout || 20000;

    function onTransportStateChange(status) {
        debug(`status changed in onTransportStateChange ${commonFunctions.returnFullObject(status)}`);
        if (trackChanged instanceof Function) {
            trackChanged();
        }
    }

    return Promise.resolve()
      .then(() => {
          debug('calling playPause.pause()');

          return playPause.pause(player, promiseTimeout);
      })
      .then(() => {
          debug('calling player.coordinator.replaceWithPlaylist()');
          player.on('transport-state', onTransportStateChange);

          return player.coordinator.replaceWithPlaylist(playlistName);
      })
      .then((result) => {
          changeStateResult = result;
          debug('waiting for state change');

          return new Promise((resolve) => {
              trackChanged = resolve;
          });
      })
      .timeout(promiseTimeout)
      .then(() => {
          return commonFunctions.checkReturnStatus(changeStateResult);
      })
      .then(() => {
          debug('calling playPause.play()');

          return playPause.play(player, promiseTimeout);
      })
      .catch(Promise.TimeoutError, (error) => {
          throw new Error(`timeout waiting for state change : ${error}`);
      })
      .catch((error) => {
          debug(`error in playFavourite() :  ${commonFunctions.returnFullObject(error)}`);

          throw error;
      })
      .finally(() => {
          player.removeListener('transport-state', onTransportStateChange);
      });
}

module.exports = {
    playPlaylist
};

@@ -344,16 +344,166 @@
 20000;%0A%0A
+    if (!playlistName) %7B%0A        return Promise.resolve()%0A        .then(() =%3E %7B%0A            throw new Error('Playlist not found');%0A        %7D);%0A    %7D%0A%0A
     func

handle no playlist passed in