import getParameterByName from "./lowebutil"
import { MyMusic } from "./loweb"

class MyplayList implements MyMusic {
  show_myplaylist() {
    return {
      success: function (fn) {
        var playlists = localStorage.getObject('playerlists');
        if (playlists == null) {
          playlists = [];
        }
        var result = [];
        for (var i = 0; i < playlists.length; i++) {
          var playlist_id = playlists[i];
          var playlist = localStorage.getObject(playlist_id);
          if (playlist != null) {
            result.push(playlist)
          }
        }
        return fn({ 'result': result });
      }
    }
  }

  get_playlist(url, hm, se) {
    var list_id = getParameterByName('list_id', url);
    return {
      success: function (fn) {
        var playlist = localStorage.getObject(list_id);
        fn(playlist);
      }
    };
  }

  private guid() {
    function s4() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
  }

  save_myplaylist(playlist) {
    var playlists = localStorage.getObject('playerlists');
    if (playlists == null) {
      playlists = [];
    }
    // update listid
    var playlist_id = 'myplaylist_' + this.guid();
    playlist.info.id = playlist_id;
    playlist.is_mine = 1;
    playlists.push(playlist_id);
    localStorage.setObject('playerlists', playlists);
    localStorage.setObject(playlist_id, playlist);
  }

  remove_myplaylist(playlist_id: string) {
    var playlists = localStorage.getObject('playerlists');
    if (playlists == null) {
      return;
    }
    var newplaylists = [];
    for (var i = 0; i < playlists.length; i++) {
      if (playlists[i] == playlist_id) {
        continue;
      }
      newplaylists.push(playlists[i]);
    }
    localStorage.removeItem(playlist_id);
    localStorage.setObject('playerlists', newplaylists);
  }

  add_myplaylist(playlist_id, track) {
    var playlist = localStorage.getObject(playlist_id);
    if (playlist == null) {
      return;
    }
    playlist.tracks.push(track);
    localStorage.setObject(playlist_id, playlist);
  }

  remove_from_myplaylist(playlist_id, track_id) {
    var playlist = localStorage.getObject(playlist_id);
    if (playlist == null) {
      return;
    }
    var newtracks = [];
    for (var i = 0; i < playlist.tracks.length; i++) {
      if (playlist.tracks[i].id == track_id) {
        continue;
      }
      newtracks.push(playlist.tracks[i]);
    }
    playlist.tracks = newtracks;
    localStorage.setObject(playlist_id, playlist);
  }

  create_myplaylist(playlist_title, track) {
    var info = {
      'cover_img_url': 'images/mycover.jpg',
      'title': playlist_title,
      'id': '',
      'source_url': ''
    };

    var playlist = {
      is_mine: 1,
      info: info,
      tracks: [track]
    };

    this.save_myplaylist(playlist);
  }

  edit_myplaylist(playlist_id, title, cover_img_url) {
    var playlist = localStorage.getObject(playlist_id);
    if (playlist == null) {
      return;
    }
    playlist.info.title = title;
    playlist.info.cover_img_url = cover_img_url;
    localStorage.setObject(playlist_id, playlist);
  }

}

export { MyplayList }