package com.michaldabski.filemanager.favourites;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;

import com.michaldabski.filemanager.sqlite.SQLiteHelper;

public class FavouritesManager
{
	public static class FolderAlreadyFavouriteException extends Exception
	{

		public FolderAlreadyFavouriteException(FavouriteFolder folder) {
			super(folder.toString() + " is already bookmarked");
			// TODO Auto-generated constructor stub
		}
		
	}
	
	public static interface FavouritesListener
	{
		void onFavouritesChanged(FavouritesManager favouritesManager);
	}
	
	private final List<FavouriteFolder> folders;
	private final SQLiteHelper sqLiteHelper;
	private final Set<FavouritesListener> favouritesListeners;

	public FavouritesManager(Context context) 
	{
		this.sqLiteHelper = new SQLiteHelper(context);
		this.favouritesListeners = new HashSet<FavouritesManager.FavouritesListener>();
		this.folders = sqLiteHelper.selectAll(FavouriteFolder.class);
		Collections.sort(folders);
	}
	
	public void addFavouritesListener(FavouritesListener favouritesListener)
	{
		favouritesListeners.add(favouritesListener);
	}
	
	public void removeFavouritesListener(FavouritesListener favouritesListener)
	{
		favouritesListeners.remove(favouritesListener);
	}
	
	void notifyListeners()
	{
		for (FavouritesListener listener : favouritesListeners)
			listener.onFavouritesChanged(this);
	}
	
	public List<FavouriteFolder> getFolders()
	{
		return folders;
	}
	
	public void addFavourite(FavouriteFolder favouriteFolder) throws FolderAlreadyFavouriteException
	{
		long id = sqLiteHelper.insert(favouriteFolder);
		if (id == -1) throw new FolderAlreadyFavouriteException(favouriteFolder);
		folders.add(favouriteFolder);
		notifyListeners();
	}
	
	public void removeFavourite(File file)
	{
		for (FavouriteFolder folder : folders) if (folder.equals(file))
			{
				removeFavourite(folder);
				break;
			}
	}
	
	public void removeFavourite(FavouriteFolder favouriteFolder)
	{
		folders.remove(favouriteFolder);
		sqLiteHelper.delete(favouriteFolder);
		notifyListeners();
	}
	
	public boolean isFolderFavourite(File file)
	{
		for (FavouriteFolder folder : folders)
			if (folder.equals(file))
				return true;

		return false;
	}

}

@@ -1051,16 +1051,54 @@
 class);%0A
+%09%09sort();%0A%09%7D%0A%09%0A%09public void sort()%0A%09%7B%0A
 %09%09Collec

add method to sort favourites