import { Injectable } from '@angular/core';
import { ServiceHelper } from 'app/helper/service-helper';

@Injectable({
  providedIn: 'root'
})
export class SpotifyService {

  // protected _playlists: Array<Object> = [];
  // protected spotifyWebApi = require('spotify-web-api-node');
  // constructor(
  //   protected offsetHelper: ServiceHelper.OfssetHelper,
  // ) { }
  // public async setUserPlaylists(): Promise<Object[]> {
  //   return new Promise<Object[]>(
  //     (resolve, reject) => {
  //       // get All Playlists of user

  //       this.getAllUserPlaylists().then(
  //         (playlists) => {
  //           this.resolvePlaylists(playlists).then(resolvedPlaylists => {
  //             this._playlists = resolvedPlaylists as Object[];
  //             resolve(this._playlists);
  //           });

  //         },
  //         (err) => {
  //           reject(err);
  //         });

  //     });

  // }


  // public get playlists(): Array<Object> {
  //   return this._playlists;
  // }
  // public getPlaylistById(playlistId: string): Object | null {
  //   return this._playlists.find((playlist) => {
  //     return playlist['id'] === playlistId;
  //   });
  // }

  // protected enlistPlaylistInQueue(playlist: Object) {
  //   console.debug(`Too many Requests save ${playlist} in Queue`);
  //   // TODO enlist Playlist in DB
  // }

  // protected async getAllUserPlaylists(pagingObject?: Object): Promise<Array<Object>> {
  //   return new Promise<Array<Object>>(async (resolve, reject) => {
  //     let playlists = new Array<Object>();
  //     this.spotifyWebApi.getUserPlaylists({ offset: !!pagingObject ? pagingObject['offset'] : 0 }).then(
  //       async (paginatePlaylistsResponse) => {
  //         if (!paginatePlaylistsResponse) {
  //           reject(`Can't get Playlists of current user, response empty.`);
  //         }
  //         playlists = paginatePlaylistsResponse.body.items;
  //         pagingObject = paginatePlaylistsResponse.body;
  //         if (paginatePlaylistsResponse.body.next) {
  //           const offset = this.offsetHelper.getNextOffsetFromUrl(paginatePlaylistsResponse.body.next);
  //           if (offset === -1) {
  //             resolve(playlists);
  //           }
  //           pagingObject['offset'] = offset;
  //           this.getAllUserPlaylists(pagingObject).then((paginatePlaylists) => {
  //             playlists = playlists.concat(paginatePlaylists);
  //             resolve(playlists);
  //           },
  //             (err) => {
  //               reject(err);
  //             });
  //           return;
  //         }
  //         resolve(playlists);
  //       },
  //       (err) => {
  //         if (err.statusCode === ServiceHelper.SPOTIFY_STAUS_CODES.TOO_MANY) {
  //           playlists.forEach(playlist => {
  //             this.enlistPlaylistInQueue(playlist)
  //           });
  //           resolve();
  //           return;
  //         }
  //         reject(`Can't get Playlists of current user ${err}`);
  //       }
  //     );
  //   });
  // }

  // protected async resolvePlaylists(playlists: Array<Object>): Promise<Object> {
  //   const getPlaylistTracksPromises = Array<Promise<Object>>();
  //   for (const playlist of playlists) {
  //     getPlaylistTracksPromises.push(
  //       this.resolvePlaylist(playlist)
  //     );
  //   }
  //   return Promise.all(getPlaylistTracksPromises);
  // }

  // protected resolvePlaylist(
  //   playlist,
  //   pagingObject?
  // ): Promise<Object> {
  //   return new Promise<Object>(async (resolve, reject) => {
  //     this.spotifyWebApi.getPlaylistTracks(playlist.id, { offset: !!pagingObject ? pagingObject.offset : 0 }).then(
  //       (tracksResponse) => {
  //         const fullPlaylist = ServiceHelper.GenericHelper.deepClone(playlist);
  //         if (!playlist) {
  //           resolve(fullPlaylist);
  //           return;
  //         }
  //         fullPlaylist.tracks = tracksResponse.body;
  //         pagingObject = tracksResponse.body;
  //         if (tracksResponse.body.next) {
  //           const offset = this.offsetHelper.getNextOffsetFromUrl(tracksResponse.body.next);
  //           if (offset === -1) {
  //             resolve(fullPlaylist);
  //             return;
  //           }
  //           pagingObject.offset = offset;
  //           this.resolvePlaylist(fullPlaylist, pagingObject).then(
  //             (fullTracksResponse) => {
  //               if (fullTracksResponse) {
  //                 fullPlaylist.tracks.items = fullPlaylist.tracks.items.concat(fullTracksResponse['tracks'].items);
  //               }
  //               resolve(fullPlaylist);
  //             },
  //             (err) => {
  //               reject(err);
  //             });
  //           return;
  //         }
  //         resolve(fullPlaylist);
  //       }, (err) => {
  //         if (err.statusCode === ServiceHelper.SPOTIFY_STAUS_CODES.TOO_MANY) {
  //           this.enlistPlaylistInQueue(playlist)
  //           resolve();
  //           return;
  //         }
  //         reject(`Can't get Playlist: ${playlist.name}. ${err}`);
  //       })
  //   });
  // }


}
