package fi.cie.chiru.servicefusionar.serviceApi;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import fi.cie.chiru.servicefusionar.groovesharkService.Grooveshark;
import fi.cie.chiru.servicefusionar.groovesharkService.PlaylistXmlParser;

public class MusicManager
{
    private final static String LOG_TAG = "GroovesharkMusicService";
    
    private Grooveshark grooveshark = null;
    private ServiceManager serviceManager = null;
	private InfoBubble infobubble;
	boolean musicPlaylistDownloaded;


    public MusicManager(ServiceManager serviceManager)
    {
    	musicPlaylistDownloaded = false;
    	Log.i(LOG_TAG, "Starting MusicManager");
    	this.serviceManager = serviceManager;
    	
		String URL = "http://www.djonline.fi/playing/?id=487";
		
		new XmlDownloader() { 
	        protected void onPostExecute(String xmlData) {
	    		PlaylistXmlParser parser = new PlaylistXmlParser();
//	    		parser.parse(xmlData);
	    		fillMusicPlaylist(parser.parse(xmlData));
	        }
	    }.execute(URL); 

	    StartGrooveshark();
    }
    
    public void StartGrooveshark()
    {
    	if (grooveshark == null)
    		grooveshark = new Grooveshark();

    }
    
    public void StopPlaying(String serviceApplicationName)
    {
    	grooveshark.StopPlaying();
    }
    
	public void showPlaylist(String serviceApplicationName) 
	{
		if(!musicPlaylistDownloaded)
			return;
			//fillMusicPlaylist();
    	
    	infobubble.visible();
		
	}
	
	public void playSong(String song)
	{
		grooveshark.SearchSong(song, 1);
		Log.d("MusicManager", "start playing song: " + song);
	}
	
	public InfoBubble getInfoBubble()
	{
	    return infobubble;
	}
	
	private void fillMusicPlaylist(List<String> playlist)
	{
		playlist.add(0, "Pink Floyd - Comfortably Numb");
		playlist.add(0, "Steven Wilson - Luminol");
		playlist.add(0, "Esbjörn Svensson - Seven days of Falling");
		int longestTitle = getLongestSongTitleLen(playlist);
		
		List<String> tempList = new ArrayList<String>();
		for (int i = 0; i < playlist.size(); i++)
		{
			String song = playlist.get(i);
			song = song + fillWhiteSpace(longestTitle - song.length());
			tempList.add(song);
		}
		
		
		infobubble = new InfoBubble(serviceManager);

 		if(infobubble.setInfoBubbleApplication("MusicInfobubble"))
 		    infobubble.populateItems(tempList, "MusicManager");
 		
 		musicPlaylistDownloaded = true;
	}
	
    private String fillWhiteSpace(int number)
    {
    	String whitespace = new String();
    	
    	for(int j=0; j<number; j++)
    		whitespace += " ";
    	
    	return whitespace;
    }
    
    private int getLongestSongTitleLen(List<String> song)
    {
    	if (song == null)
    	{
    		return 0;
    	}
    	
    	int len = 0;
    	for(int k=0; k<song.size(); k++)
    	{
    	    if(song.get(k).length() > len)
    	    	len = song.get(k).length();
    	}
    	
    	return len;
    }
	
}

@@ -722,16 +722,55 @@
 ;%0A    %09%0A
+    %09// URL to Otto K's DjOnline site.%0A
 %09%09String
@@ -965,40 +965,8 @@
 ();%0A
-//%09    %09%09parser.parse(xmlData);%0A
 %09   
@@ -1190,17 +1190,16 @@
 hark();%0A
-%0A
     %7D%0A  
@@ -1416,34 +1416,8 @@
 rn;%0A
-%09%09%09//fillMusicPlaylist();%0A
     
@@ -1445,19 +1445,16 @@
 ible();%0A
-%09%09%0A
 %09%7D%0A%09%0A%09pu
@@ -1805,24 +1805,26 @@
 uminol%22);%0A%09%09
+//
 playlist.add
@@ -1872,16 +1872,110 @@
 ling%22);%0A
+%09%09//playlist.add(0, %22New Order - Crystal%22);%0A%09%09playlist.add(0, %22Rise Against - Make It Stop%22);%0A
 %09%09int lo
@@ -2074,16 +2074,74 @@
 ring%3E();
+%0A%0A%09%09// Fill in white space to get all titles in same level
 %0A%09%09for (
@@ -2215,16 +2215,188 @@
 get(i);%0A
+%09%09%09%0A%09%09%09// Sometimes description in an rss feed is formatted as: %22Current: OFF%22 instead of %22Current: artist - song%22  %0A%09%09%09if (song.equalsIgnoreCase(%22OFF%22))%0A%09%09%09%09continue;%0A%09%09%09%0A
 %09%09%09song 
@@ -2480,19 +2480,16 @@
 %0A%09%09%7D%0A%09%09%0A
-%09%09%0A
 %09%09infobu
@@ -2693,16 +2693,146 @@
 e;%0A%09%7D%0A%09%0A
+%09/* %0A%09 * Returns string containing only wanted number of white space.%0A%09 * This is used to level strings in an info bubble.  %0A%09 */%0A
     priv
@@ -3015,24 +3015,108 @@
 %0A    %7D%0A    %0A
+    /*%0A     * Get length of the longest string in the received list object.%0A     */%0A
     private 
@@ -3401,14 +3401,12 @@
 en;%0A
+
     %7D%0A
-%09%0A
 %7D%0A

Fix playlist formatting in one special case. Add some commets to the code.