package com.github.grimpy.botifier;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

public class BlackListFragment extends PreferenceFragment {
	private SharedPreferences mSharedPref;
	private PreferenceCategory mBlackList;
	private Set<String> mBlackListEntries;
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
		setHasOptionsMenu(true);
		addPreferencesFromResource(R.xml.blacklist_preference);
		mBlackList = (PreferenceCategory) findPreference("blacklist");
		mBlackListEntries = new HashSet<String>(mSharedPref.getStringSet("blacklistentries", null));
		if (mBlackListEntries == null) {
			mBlackListEntries = new HashSet<String>();
		}
		for (String blackitem : mBlackListEntries) {
			Preference test = new Preference(getActivity());
			test.setTitle(blackitem);
			mBlackList.addPreference(test);
		}
	}
	
	private void addEntry(String black) {
		Preference pref = new Preference(getActivity());
        pref.setTitle(black);
        mBlackList.addPreference(pref);
        mBlackListEntries.add(black);
        Editor editor = mSharedPref.edit();
        editor.putStringSet("blacklistentries", mBlackListEntries);
        editor.apply();
	}
	
	private void deleteEntry(Preference pref) {
		mBlackList.removePreference(pref);
		mBlackListEntries.remove(pref.getTitle());
		Editor editor = mSharedPref.edit();
        editor.putStringSet("blacklistentries", mBlackListEntries);
        editor.apply();
	}
	
	private void editEntry(Preference pref, String value) {
		String oldvalue = pref.getTitle().toString();
		pref.setTitle(value);
		ArrayList<String> newlist = new ArrayList<String>(mBlackListEntries);
		int idx = newlist.indexOf(oldvalue);
		if (idx != -1) {
			newlist.set(idx, value);
			mBlackListEntries = new HashSet<String>(newlist);
			Editor editor = mSharedPref.edit();
	        editor.putStringSet("blacklistentries", mBlackListEntries);
	        editor.apply();
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final EditText input = new EditText(getActivity());
	    builder.setView(input);
	    builder.setTitle("Add Black List");
	    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	        //@Override
	        public void onClick(DialogInterface dialog, int which) {
	            Editable value = input.getText();
	            addEntry(value.toString());

	        }
	    });
	    builder.show();
	    
		return super.onOptionsItemSelected(item);
	}


	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, final Preference preference) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		final EditText input = new EditText(getActivity());
		input.setText(preference.getTitle());
		input.selectAll();
		input.setActivated(true);
	    builder.setView(input);
	    builder.setTitle("Edit Black List");
	    builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
	        //@Override
	        public void onClick(DialogInterface dialog, int which) {
	        	deleteEntry(preference);

	        }
	    });

	    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
	        //@Override
	        public void onClick(DialogInterface dialog, int which) {
	            Editable value = input.getText();
	            editEntry(preference, value.toString());
	            
	        }
	    });
	    builder.show();

		return true;
	}
	
	@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		MenuItem item = menu.add("Add");
		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
			MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}
		
    
}

@@ -1188,48 +1188,30 @@
 ;%0A%09%09
-mBlackListEntries = new HashSet%3CString%3E(
+Set%3CString%3E entries = 
 mSha
@@ -1256,25 +1256,24 @@
 %22, null)
-)
 ;%0A%09%09if (
 mBlackLi
@@ -1264,16 +1264,38 @@
 ;%0A%09%09if (
+entries == null) %7B%0A%09%09%09
 mBlackLi
@@ -1297,39 +1297,64 @@
 ackListEntries =
-=
  n
-ull)
+ew HashSet%3CString%3E();%0A%09%09%7D else
  %7B%0A%09%09%09mBlackList
@@ -1379,24 +1379,31 @@
 Set%3CString%3E(
+entries
 );%0A%09%09%7D%0A%09%09for

fix NPE when no blacklist settings are stored