package org.hsbp.burnstation3;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.*;
import android.view.View;
import android.view.Window;
import java.util.Map;

public class Main extends Activity implements AdapterView.OnItemClickListener,
	   AdapterView.OnItemSelectedListener {
	protected Player player;
	protected String currentAlbumZip;
	protected PlayerUI playerUi;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		initPlayer();
		initAlbumsOrder();
		initListViewClickListeners();
	}

	public void initPlayer() {
		final ListView lv = (ListView)findViewById(R.id.playlist);
		playerUi = new PlayerUiImpl(this);
		player = new Player(this, playerUi);
		lv.setAdapter(player);
	}

	public void initAlbumsOrder() {
		final Spinner albumsOrder = (Spinner)findViewById(R.id.albums_order);
		final ArrayAdapter<Album.Order> albumsOrderAdapter = new ArrayAdapter<Album.Order>(
				this, android.R.layout.simple_spinner_dropdown_item, Album.Order.values());
		albumsOrder.setAdapter(albumsOrderAdapter);
		albumsOrder.setOnItemSelectedListener(this);
		albumsOrder.setSelection(0);
	}

	public void initListViewClickListeners() {
		final int[] listViewIds = {R.id.albums, R.id.tracks, R.id.playlist};
		for (final int id : listViewIds) {
			final ListView view = (ListView)findViewById(id);
			view.setOnItemClickListener(this);
		}
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
		Album.Order order = (Album.Order)parent.getSelectedItem();
		playerUi.showIndeterminateProgressDialog(getString(R.string.loading_param, order));
		new AlbumFillTask((ListView)findViewById(R.id.albums), this, playerUi).execute(order);
    }

    public void onNothingSelected(AdapterView<?> parent) {}

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object item = parent.getItemAtPosition(position);
        switch (parent.getId()) {
            case R.id.albums:
                loadAlbumTracks((Map<String, String>)item);
                break;
            case R.id.tracks:
                enqueueTrack((Track)item);
                break;
            case R.id.playlist:
                player.play((Player.Item)item, true);
                break;
        }
    }

    protected void loadAlbumTracks(Map<String, String> album) {
        currentAlbumZip = album.get(Album.ZIP);
        playerUi.showIndeterminateProgressDialog(getString(
                    R.string.loading_param, album.get(Album.NAME)));
        new TrackListFillTask((ListView)findViewById(R.id.tracks),
					this, playerUi).execute(album.get(Album.ID));
    }

    protected void enqueueTrack(Track track) {
        track.prepare(playerUi);
        player.add(track);
        playClicked(null);
    }

    public void enqueueAllTracks(View view) {
        AdapterView<?> av = (AdapterView<?>)findViewById(R.id.tracks);
        for (int i = 0; i < av.getCount(); i++) {
            onItemClick(av, null, i, 0);
        }
    }

    public void playClicked(View view) {
        if (player.getCount() == 0) return;
        player.play(player.getItem(0), false);
    }

    public void pauseClicked(View view) {
        player.pause();
    }

    public void previousClicked(View view) {
        player.playPreviousTrack();
    }

    public void nextClicked(View view) {
        player.playNextTrack();
    }

    public void showAlbumZipAccess(View view) {
        new Album.ZipAccessTask(this, currentAlbumZip).execute();
    }
}

@@ -485,16 +485,104 @@
 layerUi;
+%0A%09protected TrackListFillTask trackListFiller;%0A%09protected AlbumFillTask albumListFiller;
 %0A%0A%09/** C
@@ -1072,16 +1072,210 @@
 layer);%0A
+        trackListFiller = new TrackListFillTask((ListView)findViewById(R.id.tracks), this, playerUi);%0A%09%09albumListFiller = new AlbumFillTask((ListView)findViewById(R.id.albums), this, playerUi);%0A
 %09%7D%0A%0A%09pub
@@ -2160,78 +2160,23 @@
 ;%0A%09%09
-new AlbumFillTask((ListView)findViewById(R.id.albums), this, playerUi)
+albumListFiller
 .exe
@@ -3019,21 +3019,17 @@
         
-new T
+t
 rackList
@@ -3036,70 +3036,10 @@
 Fill
-Task((ListView)findViewById(R.id.tracks),%0A%09%09%09%09%09this, playerUi)
+er
 .exe

store list filler tasks