package com.isawabird;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Utils {

	private static ArrayList<Species>  allSpecies = new ArrayList<Species>(); 
	private static boolean checklistLoaded = false; 

	public static SharedPreferences prefs = null;

	public static void initializeChecklist(Context context, String checklist) throws IOException{
		try{
			DataInputStream fis = new DataInputStream((InputStream) context.getAssets().open("checklists/" + checklist));
			String fileText = "";
			ArrayList<String> speciesList = new ArrayList<String>(1000); 
			while ( (fileText = fis.readLine()) != null){
				speciesList.add(fileText);
			}
			
			for (String speciesName : speciesList){
				//Log.d("ISawABird", speciesList[i]);
				Species temp = new Species(speciesName);
				allSpecies.add(temp);
				//Log.d("ISawABird", temp.getUnPunctuatedName());
			}
			Log.d(Consts.TAG,  allSpecies.size() + " species added to checklist");
			checklistLoaded = true; 
		}catch(IOException ioex){
			throw ioex;
		}
	}

	public static ArrayList<Species> getAllSpecies() {
		return allSpecies;
	}

	/** Function to search through the 'current' checklist for a given search term. 
	 * @param searchTerm : The search query. 
	 * @param subset : The subset of the checklist to search within. If null, search 
	 * is performed on the full checklist. This is useful to search as the user types.
	 * For each key press, the search function can be called with the subset being the 
	 * return value from the previous call. 
	 * 
	 * @returns A ArrayList of Species objects matching the search terms.
	 */
	public static ArrayList<Species> search(String searchTerm, ArrayList<Species> subset) {
		ArrayList<Species> returnVal = new ArrayList<Species> ();
		if (!checklistLoaded){
			return returnVal;
		}
		ArrayList<Species> searchList = (subset == null || subset.size() == 0) ? allSpecies : subset ;

		Iterator<Species> iter = searchList.iterator();
		while(iter.hasNext()){
			Species temp = iter.next(); 
			if (unpunctuate(temp.getFullName()).indexOf(unpunctuate(searchTerm)) != -1) {
				//Log.d(Consts.TAG, "Adding " + temp + " to search results.");
				returnVal.add(temp);
			}
		}
		return returnVal;
	}

	public static String unpunctuate(String string){
		return string.replaceAll("[-' ]", "").toLowerCase();
	}

	public static void setCurrentList(String listName, long listID){
		prefs.edit().putString(Consts.CURRENT_LIST_KEY, listName)
		.putLong(Consts.CURRENT_LIST_ID_KEY, listID).apply();
	}

	public static String setCurrentUsername(String username) {
		prefs.edit().putString(Consts.CURRENT_USER_ANONYMOUS, username).apply();
		return username;
	}

	public static boolean setFirstTime(boolean value) {
		prefs.edit().putBoolean(Consts.IS_FIRST_TIME, value).apply();
		return value;
	}

	public static String getCurrentListName(){
		return prefs.getString(Consts.CURRENT_LIST_KEY, "");
	}

	public static long getCurrentListID(){
		return prefs.getLong(Consts.CURRENT_LIST_ID_KEY, -1);
	}

	public static String getCurrentUsername() {
		return prefs.getString(Consts.CURRENT_USER_ANONYMOUS, null);
	}

	public static boolean isFirstTime() {
		return prefs.getBoolean(Consts.IS_FIRST_TIME, true);
	}
	public static String getChecklistName(){
		return prefs.getString(Consts.CHECKLIST, "India");
	}
	
	public static void setChecklistName(String checklistName){ 
		prefs.edit().putString(Consts.CHECKLIST, checklistName).apply();
	}
	
	private void initializePrefs(Context context){
		if (prefs==null){
			prefs = context. getSharedPreferences(Consts.PREF, Context.MODE_PRIVATE);
		}
	}
}
@@ -559,13 +559,261 @@
 %7B%0A%09%09
-try%7B%0A
+if (!checklist.equals(Utils.getChecklistName()) %7C%7C allSpecies == null)%7B%0A%09%09%09/* Load the checklist only if (a) the default checklist has changed (b) It has not been initialized */%0A%09%09%09try%7B%0A%09%09%09%09Log.i(Consts.TAG, %22Initializing checklist for %22 + checklist);%0A%09
 %09%09%09D
@@ -924,16 +924,17 @@
 t));%0A%09%09%09
+%09
 String f
@@ -947,16 +947,60 @@
 t = %22%22;%0A
+%09%09%09%09allSpecies = new ArrayList%3CSpecies%3E();%0A%09
 %09%09%09Array
@@ -1056,16 +1056,17 @@
 1000); %0A
+%09
 %09%09%09while
@@ -1110,16 +1110,17 @@
 l)%7B%0A%09%09%09%09
+%09
 speciesL
@@ -1145,229 +1145,56 @@
 %0A%09%09%09
-%7D%0A%09%09%09%0A%09%09%09for (String speciesName : speciesList)%7B%0A%09%09%09%09//Log.d(%22ISawABird%22, speciesList%5Bi%5D);%0A%09%09%09%09Species temp = new Species(speciesName);%0A%09%09%09%09allSpecies.add(temp);%0A%09%09%09%09//Log.d(%22ISawABird%22, temp.getUnPunctuatedName());%0A%09%09%09%7D%0A
+%09%09allSpecies.add(new Species(fileText));%0A%09%09%09%09%7D%0A%09
 %09%09%09L
@@ -1266,16 +1266,17 @@
 t%22);%0A%09%09%09
+%09
 checklis
@@ -1292,16 +1292,56 @@
  true; %0A
+%09%09%09%09Utils.setChecklistName(checklist);%0A%09
 %09%09%7Dcatch
@@ -1363,16 +1363,17 @@
 ex)%7B%0A%09%09%09
+%09
 throw io
@@ -1376,16 +1376,21 @@
 w ioex;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Improve performance of initialize Checklist