package com.parnswir.unmp.playlist;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class Playlist extends PlaylistElement {
	
	private static final long serialVersionUID = 3196172024840309061L;
	
	private String name;
	private int current;
	
	public ArrayList<MediaFile> children = new ArrayList<MediaFile>();
	
	
	public void addItemsFromFilter(SQLiteDatabase db, String query) {
		Cursor cursor = db.rawQuery(query, null);
		boolean successful = cursor.moveToFirst();
		if (successful)
			while (! cursor.isAfterLast()) {
				children.add(new MediaFile(cursor.getString(0)));
				cursor.moveToNext();
			}
		cursor.close();
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPosition() {
		return current;
	}
	
	public void setPosition(int position) {
		current = position;
	}
	
	public void nextSource() {
		setPosition(getPosition() + 1);
	}
	
	public void previousSource() {
		setPosition(getPosition() - 1);
	}
	
	public boolean isAtEnd() {
		return getPosition() >= children.size();
	}
	
	public boolean isAtStart() {
		return getPosition() < 0;
	}
	
	public MediaFile getCurrentChild() {
		if (getPosition() < 0 || getPosition() >= children.size())
			return null;
		return children.get(getPosition());
	}

	@Override
	public String getCurrentFile() {
		if (getCurrentChild() == null) 
			return null;
		return getCurrentChild().getCurrentFile();
	}
	
	public void next() {
		if (isRepeating()) return;
		nextSource();
		if (isAtEnd() && isRepeatingAll()) 
			setPosition(0);
	}
	
	public void previous() {
		if (isRepeating()) return;
		previousSource();
		if (isAtStart() && isRepeatingAll())
			setPosition(children.size() - 1);
	}
}

@@ -53,24 +53,49 @@
 l.ArrayList;
+%0Aimport java.util.Random;
 %0A%0Aimport and
@@ -331,16 +331,111 @@
 rent;%0A%09%0A
+%09private int cronCurrent;%0A%09private ArrayList%3CInteger%3E chronology = new ArrayList%3CInteger%3E();%0A%09%0A
 %09public 
@@ -1074,110 +1074,705 @@
 %7B%0A%09%09
-setPosition(getPosition() + 1);%0A%09%7D%0A%09%0A%09public void previousSource() %7B%0A%09%09setPosition(getPosition() - 1);
+cronCurrent += 1;%0A%09%09if (chronology.size() %3E cronCurrent) %7B%0A%09%09%09setPosition(chronology.get(cronCurrent));%0A%09%09%7D else %7B%0A%09%09%09if (isShuffled()) %7B%0A%09%09%09%09Random random = new Random();%0A%09%09%09%09setPosition(random.nextInt(children.size()));%0A%09%09%09%7D else %7B%0A%09%09%09%09setPosition(getPosition() + 1);%0A%09%09%09%7D%0A%09%09%09chronology.add(getPosition());%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void previousSource() %7B%0A%09%09cronCurrent -= 1;%0A%09%09if (cronCurrent %3E= 0 && !chronology.isEmpty()) %7B%0A%09%09%09setPosition(chronology.get(cronCurrent));%0A%09%09%7D else %7B%0A%09%09%09if (isShuffled()) %7B%0A%09%09%09%09Random random = new Random();%0A%09%09%09%09setPosition(random.nextInt(children.size()));%0A%09%09%09%7D else %7B%0A%09%09%09%09setPosition(getPosition() - 1);%0A%09%09%09%7D%0A%09%09%09chronology.add(0, getPosition());%0A%09%09%09cronCurrent = 0;%0A%09%09%7D
 %0A%09%7D%0A

Implement shuffle