// Class for handling playlists
// REQUIRES: queue.js
function Playlist()
{
    // queue of vidId's
    this.queue = new Queue();
    
    // hashmap of ids->title
    this.vidIdToTitleMap = [];

  //pointer to the currently selected element
  var currentSelection = 0;
    
    this.addVideo = function(vidId, title)
	    {
			// add the vid id to the queue
			this.queue.enqueue(vidId);
			
			// also insert a vidId=>title in the map if doesnt already exist
			if (!this.vidIdToTitleMap[vidId])
			{
			    this.vidIdToTitleMap[vidId] = title;
			}
	    }
	        
    // queueNumber is the line number (0 is next item to be played)
    // of the item to be deleted. We cannot pass in an ID here 
    // because the same item can be added multiple times.
    this.removeVideo = function(queueNumber)
	    {
            var size = this.queue.getSize();
			for (var i=0; i<size; i++) {
			    if (i != queueNumber) {
			        this.queue.enqueue(this.queue.dequeue());
			    } else {
			        // don't enqueue the one that is to be 
			        // removed
			        this.queue.dequeue();
			    }
			}
	    }

    // queueNumber is the line number (0 is next item to be played)
    // of the item to be played.
    this.selectVideo = function(queueNumber)
   {
	var video = [];
	var size = this.queue.getSize();
	for (var i=0; i<size; i++) {
		
		if (i == queueNumber) {
			video = this.getNextVideo();
			this.currentSelection = i;
	    	} 
		var vidId = this.queue.dequeue();
		this.queue.enqueue(vidId);
	}
	return video;
    }

	//increments selection pointer and selects the next video in the list
	this.selectNextVideo = function(){
		var size = this.queue.getSize();
		this.currentSelection++;
		if(this.currentSelection >= size){
			this.currentSelection = 0;
		}
		return 	this.selectVideo(this.currentSelection);
	}

	//decrements selection pointer and selects the previous video in the list
	this.selectPreviousVideo = function(){
		var size = this.queue.getSize();
		this.currentSelection--;
		if(this.currentSelection < 0){
			this.currentSelection = size-1;
		}else if(this.currentSelection >= size){
			this.currentSelection = 0;
		}
			
		return 	this.selectVideo(this.currentSelection);
	}
    
    this.clear = function ()
	    {
	        while(this.queue.isEmpty() == false) {
	            this.queue.dequeue();
	        }
	    }
	    
    this.getNumVideos = function()
	    {
	       return this.queue.getSize();
	    }
    
    this.isEmpty = function()
        {
            return this.queue.isEmpty();
        }
    
    // this removes the video at the front of the line
    this.removeNextVideo = function()
    {
        if(this.isEmpty() == false) {
            this.removeVideo(0);
        }
    }
    
    // returns an array{"vidId"=><vidId>, "title"=><title>}
    this.getNextVideo = function()
	    {
	        var video = [];
	        video['vidId'] = this.getNextVidId();
	        video['title'] = this.getNextTitle();
	        
	        return video;
	    }
    
    this.getNextVidId = function()
	    {
	        return this.queue.getOldestElement();
	    }
    
    this.getNextTitle = function()
	    {
	        var vidId = this.getNextVidId();
	        return this.vidIdToTitleMap[vidId];
	    }
    
    this.getAllVideos = function()
	    {
            var allVideos = [];
            var size = this.queue.getSize();
            
            for(var i=0; i<size; i++) {            
                // traverse the queue and get the vidIds and titles
                vidId = this.queue.dequeue();
                this.queue.enqueue(vidId);
                title = this.vidIdToTitleMap[vidId];
                
                var video = [];
                video['vidId'] = vidId;
                video['title'] = title;
                
                allVideos[i] = video;
            }
            
            return allVideos;
	    }
}
