angular.module('mopidy-mobile.tracklist', [
  'ionic',
  'mopidy-mobile.connection',
  'mopidy-mobile.ui'
])

.config(function($stateProvider) {
  $stateProvider.state('tabs.tracklist', {
    url: '/tracklist',
    views: {
      'tracklist': {
        templateUrl: 'templates/tracklist.html',
        controller: 'TracklistCtrl',
        resolve: {
          currentTlTrack: function(connection) {
            return connection(function(mopidy) {
              return mopidy.playback.getCurrentTlTrack();
            });
          },
          options: function(connection) {
            return connection(function(mopidy) {
              return mopidy.tracklist.getOptions();
            });
          },
          tlTracks: function(connection) {
            return connection(function(mopidy) {
              return mopidy.tracklist.getTlTracks();
            });
          }
        }
      }
    }
  });
})

.controller('TracklistCtrl', function($scope, $log, connection, popup, menu, currentTlTrack, options, tlTracks) {
  var handlers = {
    'event:optionsChanged': function() {
      connection(function(mopidy) {
        return mopidy.tracklist.getOptions();
      }).then(function(options) {
        $scope.options = options;
      });
    },
    'event:trackPlaybackEnded': function() {
      $scope.currentTlTrack = null;
    },
    'event:trackPlaybackStarted': function(event) {
      $scope.currentTlTrack = event.tl_track;
    },
    'event:tracklistChanged': function() {
      // TODO: all...
      connection(function(mopidy) {
        return mopidy.playback.getCurrentTlTrack();
      }).then(function(tlTrack) {
        $scope.currentTlTrack = tlTrack;
      });
      connection(function(mopidy) {
        return mopidy.tracklist.getTlTracks();
      }).then(function(tlTracks) {
        $scope.tlTracks = tlTracks;
      });
    }
  };
  connection.on(handlers);

  $scope.popover = menu([
    {
      text: 'Clear',
      click: 'popover.hide() && clear()',
      disabled: '!tlTracks.length',
      hellip: true
    },
    {
      text: 'Save as',
      click: 'popover.hide() && save()',
      disabled: '!tlTracks.length',
      hellip: true
    }
  ], {
    scope: $scope
  });

  angular.extend($scope, {
    currentTlTrack: currentTlTrack,
    options: options,
    tlTracks: tlTracks,
    clear: function() {
      popup.confirm('Clear Tracklist').then(function(result) {
        if (result) {
          connection(function(mopidy) {
            return mopidy.tracklist.clear();
          });
        }
      });
    },
    getTracks: function() {
      return $scope.tlTracks.map(function(tlTrack) { return tlTrack.track; });
    },
    index: function(tlTrack) {
      var tlid = tlTrack.tlid;
      var tlTracks = $scope.tlTracks;
      for (var i = 0, length = tlTracks.length; i !== length; ++i) {
        if (tlTracks[i].tlid === tlid) {
          return i;
        }
      }
      return -1;
    },
    play: function(tlTrack) {
      connection(function(mopidy) {
        return mopidy.playback.play({
          tl_track: angular.copy(tlTrack)
        });
      });
    },
    remove: function(tlTrack) {
      connection(function(mopidy) {
        return mopidy.tracklist.remove({criteria: {tlid: [tlTrack.tlid]}});
      });
    },
    save: function() {
      popup.prompt('Playlist Name').then(function(name) {
        if (name) {
          connection(function(mopidy) {
            return mopidy.playlists.create({name: name}).then(function(playlist) {
              playlist.tracks = $scope.getTracks();
              return mopidy.playlists.save({playlist: playlist});
            });
          }).then(function() {
            popup.alert('Playlist saved');
          });
        }
      });
    },
    setOptions: function(params) {
      connection(function(mopidy) {
        return mopidy.tracklist.setOptions(params);
      });
    },
    getThumbnailURI: function(track) {
      if (track.album && track.album.images && track.album.images.length) {
        return connection.resolveURI(track.album.images[0]);
      } else {
        return 'images/thumbnail.png';
      }
    }
  });

  $scope.$on('$destroy', function() {
    connection.off(handlers);
    $scope.popover.remove();
  });
});

@@ -2041,24 +2041,122 @@
    %7D,%0A    %7B%0A
+      text: 'Add URL',%0A      click: 'popover.hide() && addURL()',%0A      hellip: true%0A    %7D,%0A    %7B%0A
       text: 
@@ -2409,16 +2409,357 @@
 Tracks,%0A
+    addURL: function() %7B%0A      popup.prompt('Stream URL').then(function(url) %7B%0A        if (url) %7B%0A          connection(function(mopidy) %7B%0A            return mopidy.tracklist.add(%7Buri: url%7D).then(function(tlTracks) %7B%0A              return mopidy.playback.play(%7Btl_track: tlTracks%5B0%5D%7D);%0A            %7D);%0A          %7D);%0A        %7D%0A      %7D);%0A    %7D,%0A
     clea

Fix #17: Add URLs to Tracklist.