package com.namelessdev.mpdroid;

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

import org.a0z.mpd.Directory;
import org.a0z.mpd.MPD;
import org.a0z.mpd.MPDServerException;
import org.a0z.mpd.Music;

import com.namelessdev.mpdroid.R;
import com.namelessdev.mpdroid.R.layout;
import com.namelessdev.mpdroid.R.string;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SongsActivity extends BrowseActivity {

	private List<Music> dispMusic = null;

	public SongsActivity() {
		super(R.string.addSong, R.string.songAdded, MPD.MPD_SEARCH_TITLE);
		items = new ArrayList<String>();		
	}
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.artists);

		MPDApplication app = (MPDApplication)getApplication();
		String album = (String) this.getIntent().getStringExtra("album");
		this.setTitle(album);
		
		pd = ProgressDialog.show(this, getResources().getString(R.string.loading), getResources().getString(R.string.loadingSongs));


		// Loading Albums asynchronous...
		app.oMPDAsyncHelper.addAsyncExecListener(this);
		Runnable run = new Runnable(){
			@Override
			public void run() {
				try {
					MPDApplication app = (MPDApplication)getApplication();
					dispMusic = new ArrayList<Music>(app.oMPDAsyncHelper.oMPD.find(MPD.MPD_FIND_ALBUM, album));
				} catch (MPDServerException e) {
					
				}
			}
			
			String album;
			public Runnable setAlbum(String album)
			{
				this.album = album;
				return this;
			}
		}.setAlbum(album);
		
		iJobID = app.oMPDAsyncHelper.execAsync(run);
		
		
		registerForContextMenu(getListView());	
	}
    
    @Override
	protected void Add(String item) {
    	Add(items.indexOf(item));
	}
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Add(position);
	}    
    
    protected void Add(int index) {
    	Music music = dispMusic.get(index);
		try {
			MPDApplication app = (MPDApplication)getApplication();

			app.oMPDAsyncHelper.oMPD.getPlaylist().add(music);
			MainMenuActivity.notifyUser(String.format(getResources().getString(R.string.songAdded,music.getTitle()),music.getName()), this);
		} catch (MPDServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	@Override
	public void asyncExecSucceeded(int jobID) {
		if(iJobID == jobID) {
			for (Music music : dispMusic) {
				items.add(music.getTitle());
			}
			
			super.asyncExecSucceeded(jobID);
		}
	}
    
}

@@ -874,16 +874,22 @@
 on();%0A%09%09
+final 
 String a
@@ -946,16 +946,16 @@
 lbum%22);%0A
-
 %09%09this.s
@@ -1194,23 +1194,47 @@
 ;%0A%09%09
-Runnable run = 
+iJobID = app.oMPDAsyncHelper.execAsync(
 new 
@@ -1508,193 +1508,11 @@
 %7D%0A%09%09
-%09%0A%09%09%09String album;%0A%09%09%09public Runnable setAlbum(String album)%0A%09%09%09%7B%0A%09%09%09%09this.album = album;%0A%09%09%09%09return this;%0A%09%09%09%7D%0A%09%09%7D.setAlbum(album);%0A%09%09%0A%09%09iJobID = app.oMPDAsyncHelper.execAsync(run);%0A%09%09
+%7D);
 %0A%09%09%0A

Set the album variable final so that it can be reused in the Runnable