package com.michaldabski.filemanager;

import java.io.File;
import java.security.InvalidParameterException;
import java.util.Comparator;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.michaldabski.utils.FileUtils;

public class AppPreferences
{
	private static final String 
		NAME = "FileExplorerPreferences",
		
		PREF_START_FOLDER = "start_folder",
		PREF_SORT_BY = "sort_by";
	
	public static final int
		SORT_BY_NAME = 0,
		SORT_BY_TYPE = 1,
		SORT_BY_SIZE = 2;
	
	private final static int DEFAULT_SORT_BY = SORT_BY_NAME;
	
	File startFolder;
	int sortBy;
	
	private AppPreferences() {		
	}
	
	private void loadFromSharedPreferences(SharedPreferences sharedPreferences)
	{
		String startPath = sharedPreferences.getString(PREF_START_FOLDER, null);
		if (startPath == null)
		{
			if (Environment.getExternalStorageDirectory().list() != null)
				startFolder = Environment.getExternalStorageDirectory();
			else 
				startFolder = new File("/");
		}
		else this.startFolder = new File(startPath);
		this.sortBy = sharedPreferences.getInt(PREF_SORT_BY, DEFAULT_SORT_BY);
	}
	
	private void saveToSharedPreferences(SharedPreferences sharedPreferences)
	{
		sharedPreferences.edit()
			.putString(PREF_START_FOLDER, startFolder.getAbsolutePath())
			.putInt(PREF_SORT_BY, sortBy)
			.commit();		
	}
	
	public void saveChangesAsync(final Context context)
	{
		new Thread(new Runnable()
		{
			
			@Override
			public void run()
			{
				saveChanges(context);
				
			}
		}).run();
	}
	
	public void saveChanges(Context context)
	{
		saveToSharedPreferences(context.getSharedPreferences(NAME, Context.MODE_PRIVATE));
	}

	public AppPreferences setStartFolder(File startFolder)
	{
		this.startFolder = startFolder;
		return this;
	}
	
	public AppPreferences setSortBy(int sortBy)
	{
		if (sortBy < 0 || sortBy > 2)
			throw new InvalidParameterException(String.valueOf(sortBy)+" is not a valid id of sorting order");
		
		this.sortBy = sortBy;
		return this;
	}
	
	public int getSortBy()
	{
		return sortBy;
	}
	
	public File getStartFolder()
	{
		return startFolder;
	}
	
	public Comparator<File> getFileSortingComparator()
	{
		switch (sortBy)
		{
			case SORT_BY_SIZE:
				return new FileUtils.FileSizeComparator();
				
			case SORT_BY_TYPE:
				return new FileUtils.FileExtensionComparator();
				
			default:
				return new FileUtils.FileNameComparator();
		}
	}
	
	public static AppPreferences loadPreferences(Context context)
	{
		AppPreferences instance = new AppPreferences();
		instance.loadFromSharedPreferences(context.getSharedPreferences(NAME, Context.MODE_PRIVATE));
		return instance;
	}
}

@@ -416,16 +416,52 @@
 older%22,%0A
+%09%09PREF_CARD_LAYOUT = %22card_layout%22,%0A
 %09%09PREF_S
@@ -480,16 +480,16 @@
 rt_by%22;%0A
-
 %09%0A%09publi
@@ -565,24 +565,127 @@
 SIZE = 2;%0A%09%0A
+%09public static final int%0A%09%09CARD_LAYOUT_MEDIA = 0,%0A%09%09CARD_LAYOUT_ALWAYS = 1,%0A%09%09CARD_LAYOUT_NEVER = 2;%0A%09%0A
 %09private fin
@@ -764,16 +764,33 @@
 sortBy;%0A
+%09int cardLayout;%0A
 %09%0A%09priva
@@ -1294,16 +1294,99 @@
 RT_BY);%0A
+%09%09this.cardLayout = sharedPreferences.getInt(PREF_CARD_LAYOUT, CARD_LAYOUT_MEDIA);%0A
 %09%7D%0A%09%0A%09pr
@@ -1584,16 +1584,57 @@
 sortBy)%0A
+%09%09%09.putInt(PREF_CARD_LAYOUT, cardLayout)%0A
 %09%09%09.comm
@@ -2057,16 +2057,16 @@
 Folder;%0A
-
 %09%09return
@@ -2069,32 +2069,172 @@
 turn this;%0A%09%7D%0A%09%0A
+%09public void setCardLayout(int cardLayout)%0A%09%7B%0A%09%09this.cardLayout = cardLayout;%0A%09%7D%0A%09%0A%09public int getCardLayout()%0A%09%7B%0A%09%09return cardLayout;%0A%09%7D%0A%09%0A
 %09public AppPrefe

Add card layout option to settings