package de.tudarmstadt.informatik.hostage.ui2.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
import de.tudarmstadt.informatik.hostage.logging.LogExport;
import de.tudarmstadt.informatik.hostage.logging.MessageRecord;
import de.tudarmstadt.informatik.hostage.logging.NetworkRecord;
import de.tudarmstadt.informatik.hostage.logging.Record;
import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
import de.tudarmstadt.informatik.hostage.sync.bluetooth.BluetoothSyncActivity;
import de.tudarmstadt.informatik.hostage.sync.nfc.NFCSync;
import de.tudarmstadt.informatik.hostage.sync.tracing.TracingSyncActivity;
import de.tudarmstadt.informatik.hostage.ui.LogFilter;
import de.tudarmstadt.informatik.hostage.ui.LogFilter.SortType;
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
import de.tudarmstadt.informatik.hostage.ui2.adapter.RecordListAdapter;
import de.tudarmstadt.informatik.hostage.ui2.dialog.ChecklistDialog;
import de.tudarmstadt.informatik.hostage.ui2.dialog.DateTimeDialogFragment;
import de.tudarmstadt.informatik.hostage.ui2.model.ExpandableListItem;
import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopup;
import de.tudarmstadt.informatik.hostage.ui2.popup.AbstractPopupItem;
import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupItem;
import de.tudarmstadt.informatik.hostage.ui2.popup.SimplePopupTable;
import de.tudarmstadt.informatik.hostage.ui2.popup.SplitPopupItem;

public class RecordOverviewFragment extends UpNavigatibleFragment implements ChecklistDialog.ChecklistDialogListener, DateTimeDialogFragment.DateTimeDialogFragmentListener {

	static final String FILTER_MENU_TITLE_BSSID = "BSSID";
	static final String FILTER_MENU_TITLE_ESSID = "ESSID";
	static final String FILTER_MENU_TITLE_PROTOCOLS = MainActivity.getContext().getString(R.string.rec_protocol);
	static final String FILTER_MENU_TITLE_TIMESTAMP_BELOW = MainActivity.getContext().getString(
			R.string.rec_latest);
	static final String FILTER_MENU_TITLE_TIMESTAMP_ABOVE = MainActivity.getContext().getString(
			R.string.rec_earliest);
	static final String FILTER_MENU_TITLE_SORTING = MainActivity.getContext().getString(R.string.rec_sortby);
	static final String FILTER_MENU_TITLE_REMOVE = MainActivity.getContext().getString(R.string.rec_reset_filter);
    static final String FILTER_MENU_TITLE_GROUP = MainActivity.getContext().getString(
			R.string.rec_group_by);
    static final String FILTER_MENU_POPUP_TITLE = MainActivity.getContext().getString(
			R.string.rec_filter_by);

    private boolean wasBelowTimePicker;

    private LogFilter filter;
    private boolean showFilterButton;
    private View rootView;

    private int mListPosition = -1;
    private int mItemPosition = -1;

    public String groupingKey;

    private ExpandableListView expListView;
    private ProgressBar spinner;

    private Toast noDataNotificationToast;

    HostageDBOpenHelper dbh;

    private String sectionToOpen = "";
    private ArrayList<Integer> openSections;

	private SharedPreferences pref;

	public void setFilter(LogFilter filter){
        this.filter = filter;
    }

    Thread loader;

    public RecordOverviewFragment(){}

    public void setGroupKey(String key){
        this.groupingKey = key;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {

		setHasOptionsMenu(true);
		getActivity().setTitle(getResources().getString(R.string.drawer_records));

		dbh = new HostageDBOpenHelper(this.getActivity().getBaseContext());
	    pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        //this.addRecordToDB(5, 2);

	    // Get the message from the intent


        if (this.filter == null){
            Intent intent = this.getActivity().getIntent();
            LogFilter filter = intent.getParcelableExtra(LogFilter.LOG_FILTER_INTENT_KEY);

            if(filter == null){
                this.clearFilter();
            } else {
                this.filter = filter;
            }
        }

        if (this.groupingKey == null) this.groupingKey = this.groupingTitles().get(0);

	    this.setShowFilterButton(!this.filter.isNotEditable());

		View rootView = inflater.inflate(this.getLayoutId(), container, false);
        this.rootView = rootView;
		ExpandableListView mylist = (ExpandableListView) rootView.findViewById(R.id.loglistview);

        this.spinner =(ProgressBar) rootView.findViewById(R.id.progressBar1);
        this.spinner.setVisibility(View.GONE);

		this.expListView = mylist;

        this.initialiseListView();

        ImageButton filterButton = (ImageButton) rootView.findViewById(R.id.FilterButton);
        filterButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	RecordOverviewFragment.this.openFilterPopupMenuOnView(v);
            }
        });
        filterButton.setVisibility(this.showFilterButton? View.VISIBLE : View.INVISIBLE);

        ImageButton sortButton = (ImageButton) rootView.findViewById(R.id.SortButton);
        sortButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Open SortMenu
                RecordOverviewFragment.this.openSortingDialog();
            }
        });

        ImageButton groupButton = (ImageButton) rootView.findViewById(R.id.GroupButton);
        groupButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Open SortMenu
                RecordOverviewFragment.this.openGroupingDialog();
            }
        });

		return rootView;
	 }


    /**Initialises the expandable list view in a backgorund thread*/
    private void initialiseListView(){
        if (loader != null) loader.interrupt();

        this.spinner.setVisibility(View.VISIBLE);

        loader = new Thread(new Runnable(){

            private void updateUI(final RecordListAdapter currentAdapter)
            {
                if(loader.isInterrupted()){
                    return;
                }
                Activity activity = RecordOverviewFragment.this.getActivity();

                if (activity != null){
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            RecordOverviewFragment.this.expListView.setAdapter(currentAdapter);
                            // Update view and remove loading spinner etc...
                            RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();

                            if (adapter != null){
                                adapter.notifyDataSetChanged();

                                if (adapter.getGroupCount() == 1){
                                    RecordOverviewFragment.this.expListView.expandGroup(0);
                                } else {
                                    RecordOverviewFragment.this.setSectionToOpen(RecordOverviewFragment.this.sectionToOpen);
                                }
                            }

                            if (RecordOverviewFragment.this.openSections != null && RecordOverviewFragment.this.openSections.size() != 0){
                                for (int i = 0; i < RecordOverviewFragment.this.openSections.size(); i++){
                                    int index = RecordOverviewFragment.this.openSections.get(i);
                                    RecordOverviewFragment.this.expListView.expandGroup(index);
                                }
                            } else {
                                RecordOverviewFragment.this.openSections = new ArrayList<Integer>();
                            }

                            if (mListPosition != -1 && mItemPosition != -1)
                                RecordOverviewFragment.this.expListView.setSelectedChild(mListPosition, mItemPosition, true);

                            mListPosition = -1;
                            mItemPosition = -1;
                            registerListClickCallback(RecordOverviewFragment.this.expListView);
                            RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
                            RecordOverviewFragment.this.actualiseFilterButton();
                            RecordOverviewFragment.this.showEmptyDataNotification();
                        }
                    });
                }
            }

            private RecordListAdapter doInBackground()
            {
                return populateListViewFromDB(RecordOverviewFragment.this.expListView);
            }

            @Override
            public void run()
            {
                //RecordOverviewFragment.this.addRecordToDB(5, 10);
                updateUI(doInBackground());
            }

        });

        loader.start();

        this.actualiseFilterButton();
    }


    /**
    *  Returns the Fragment layout ID
    *  @return int The fragment layout ID
    * */
    public int getLayoutId(){
        return R.layout.fragment_record_list;
    }

    /**
    * Gets called if the user clicks on item in the filter menu.
    *
    * @param  item {@link AbstractPopupItem AbstractPopupItem }
    * */
	public void onFilterMenuItemSelected(AbstractPopupItem item) {
		String title = item.getTitle();

        if (item instanceof SplitPopupItem){
            SplitPopupItem splitItem = (SplitPopupItem)item;
            if (splitItem.wasRightTouch){
                this.openTimestampToFilterDialog();
            } else {
                this.openTimestampFromFilterDialog();
            }
            return;
        }

        if (title != null){
            if(title.equals(FILTER_MENU_TITLE_BSSID)){
                this.openBSSIDFilterDialog();
            }
            if(title.equals(FILTER_MENU_TITLE_ESSID)){
                this.openESSIDFilterDialog();
            }
            if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
                this.openProtocolsFilterDialog();
            }
            if(title.equals(FILTER_MENU_TITLE_SORTING)){
                this.openSortingDialog();
            }
            if(title.equals(FILTER_MENU_TITLE_REMOVE)){
                this.clearFilter();
                this.actualiseListViewInBackground();
            }
            if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
                this.openTimestampToFilterDialog();
            }
            if(title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
                this.openTimestampFromFilterDialog();
            }
        }
		//return super.onOptionsItemSelected(item);
	}



    @Override
    public void onStart() {
        super.onStart();
        if (this.expListView.getExpandableListAdapter() != null){
            if (this.expListView.getExpandableListAdapter().getGroupCount() == 1){
                this.expListView.expandGroup(0);
            } else {
                this.setSectionToOpen(this.sectionToOpen);
            }
        }

    }

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// Inflate the menu items for use in the action bar
		inflater.inflate(R.menu.records_overview_actions, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.records_action_synchronize:

				AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
				builder.setTitle("Synchronize records");
				builder.setItems(new String[]{
						"Via Bluetooth",
						"Via NFC",
						"Via Online Database"
				}, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int position) {
						switch(position){
							case 0:
								getActivity().startActivity(new Intent(getActivity(), BluetoothSyncActivity.class));
								break;
							case 1:
								getActivity().startActivity(new Intent(getActivity(), NFCSync.class));
								break;

							case 2:
								getActivity().startActivity(new Intent(getActivity(), TracingSyncActivity.class));
								break;
						}
					}
				});
				builder.create();
				builder.show();


				return true;
			case R.id.records_action_export:
				AlertDialog.Builder builderExport = new AlertDialog.Builder(getActivity());
				builderExport.setTitle("Choose export format");
				builderExport.setItems(R.array.format, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int position) {
						//RecordOverviewFragment.this.exportDatabase(position);
						Intent intent = new Intent(getActivity(), LogExport.class);
						intent.setAction(LogExport.ACTION_EXPORT_DATABASE);
						intent.putExtra(LogExport.FORMAT_EXPORT_DATABASE, position);

						RecordOverviewFragment.this.getActivity().startService(intent);
					}
				});
				builderExport.create();
				builderExport.show();

				return true;
		}

		return false;
	}

	/*****************************
	 *
	 * 			Public API
	 *
	 * ***************************/

	/**
	 * Group records by SSID and expand given SSID
	 *
	 * @param SSID the SSID
	 */
	public void showDetailsForSSID(Context context,  String SSID) {
		Log.e("RecordOverviewFragment", "Implement showDetailsForSSID!!");
        this.clearFilter();
        int ESSID_INDEX = 2;
        ArrayList<String> ssids = new ArrayList<String>();
        this.sectionToOpen = SSID;
        this.groupingKey = this.groupingTitles(context).get(ESSID_INDEX);
  	}


	/*****************************
	 *
	 *          ListView Stuff
	 *
	 * ***************************/

    /**
    *  Reloads the data in the ExpandableListView for the given filter object.
    *  @param  mylist {@link ExpandableListView ExpandableListView}
    * */
	private RecordListAdapter populateListViewFromDB(ExpandableListView mylist) {

        HashMap<String, ArrayList<ExpandableListItem>> sectionData = new HashMap<String, ArrayList<ExpandableListItem>>();

        ArrayList<Record> data = dbh.getRecordsForFilter(RecordOverviewFragment.this.filter);

        // Adding Items to ListView
        String keys[] = new String[] { RecordOverviewFragment.this.getString(R.string.RecordBSSID), RecordOverviewFragment.this.getString(R.string.RecordSSID), RecordOverviewFragment.this.getString(R.string.RecordProtocol), RecordOverviewFragment.this.getString(R.string.RecordTimestamp)};
        int ids[] = new int[] {R.id.RecordTextFieldBSSID, R.id.RecordTextFieldSSID, R.id.RecordTextFieldProtocol, R.id.RecordTextFieldTimestamp };

        HashMap<String, Integer> mapping = new HashMap<String, Integer>();
        int i = 0;
        for(String key : keys){
            mapping.put(key, ids[i]);
            i++;
        }

        ArrayList<String>groupTitle = new ArrayList<String>();

        for (Record val : data) {
            // DO GROUPING IN HERE
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(RecordOverviewFragment.this.getString(R.string.RecordBSSID), val.getBssid());
            map.put(RecordOverviewFragment.this.getString(R.string.RecordSSID), val.getSsid());
            map.put(RecordOverviewFragment.this.getString(R.string.RecordProtocol), val.getProtocol());
            map.put(RecordOverviewFragment.this.getString(R.string.RecordTimestamp),
                    RecordOverviewFragment.this.getDateAsString(val.getTimestamp()));

            ExpandableListItem item = new ExpandableListItem();
            item.setData(map);

            item.setId_Mapping(mapping);

            item.setTag(val.getAttack_id());

            String groupID = RecordOverviewFragment.this.getGroupValue(val);

            ArrayList<ExpandableListItem> items = sectionData.get(groupID);
            if (items == null) {
                items = new ArrayList<ExpandableListItem>();
                sectionData.put(groupID, items);
                groupTitle.add(groupID);
            }


            items.add(item);
        }

        Collections.sort(groupTitle, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });


        RecordListAdapter adapter = null;
        if (mylist.getAdapter() != null && mylist.getAdapter() instanceof RecordListAdapter){
            adapter = (RecordListAdapter) mylist.getAdapter();
            adapter.setData(sectionData);
            adapter.setSectionHeader(groupTitle);
        } else {
            adapter = new RecordListAdapter( RecordOverviewFragment.this.getApplicationContext(), groupTitle, sectionData);
        }

        return adapter;
	}

    /**
     * Actualises the list in a background thread
     */
    private void actualiseListViewInBackground(){
        if (loader != null && loader.isAlive()) loader.interrupt();

        loader = null;

        this.spinner.setVisibility(View.VISIBLE);
        this.actualiseFilterButton();

        loader = new Thread(new Runnable() {
            @Override
            public void run() {
                this.runOnUiThread(this.doInBackground());
            }

            private RecordListAdapter doInBackground(){
                return RecordOverviewFragment.this.populateListViewFromDB(RecordOverviewFragment.this.expListView);
            }

            private void runOnUiThread(final RecordListAdapter adapter){
                Activity actv = RecordOverviewFragment.this.getActivity();
                if (actv != null){
                    actv.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            this.actualiseUI();
                        }
                        private void actualiseUI(){
                            if (adapter != null){
                                RecordOverviewFragment.this.expListView.setAdapter(adapter);
                                adapter.notifyDataSetChanged();
                                RecordOverviewFragment.this.spinner.setVisibility(View.GONE);
                            }
                            RecordOverviewFragment.this.showEmptyDataNotification();
                        }
                    });
                }
            }
        });
        loader.start();
    }

    /**
     * Shows a small toast if the data to show is empty (no records).
     */
    private void showEmptyDataNotification(){
        if (RecordOverviewFragment.this.noDataNotificationToast == null){
            RecordOverviewFragment.this.noDataNotificationToast =  Toast.makeText(getApplicationContext(), R.string.no_data_notification, Toast.LENGTH_SHORT);
        }
        RecordListAdapter adapter = (RecordListAdapter) RecordOverviewFragment.this.expListView.getExpandableListAdapter();

        if (this.getFilterButton().getVisibility() == View.VISIBLE && this.filter.isSet()){
            this.noDataNotificationToast.setText(R.string.no_data_notification);
        } else {
            this.noDataNotificationToast.setText(R.string.no_data_notification_no_filter);
        }
        if (adapter == null || adapter.getData().isEmpty())
            RecordOverviewFragment.this.noDataNotificationToast.show();

    }

    /**This will open a section in the ExpandableListView with the same title as the parameter s.
    *
    * @param s String (the section title to open)
    *
    * */
    private void setSectionToOpen(String s){
        this.sectionToOpen = s;
        if (this.sectionToOpen != null && this.sectionToOpen.length() != 0){
            if (this.getGroupTitles().contains(this.sectionToOpen)){
                int section = this.getGroupTitles().indexOf(this.sectionToOpen);
                this.expListView.expandGroup(section);
                this.sectionToOpen = "";
            }
        }
    }

    /**
    * Returns the base context.
    * @return Context baseContext
    * */
	private Context getBaseContext(){
		return this.getActivity().getBaseContext();
	}

    /**Returns the application context.
    * @return Context application context
    * */
	private Context getApplicationContext(){
		return this.getActivity().getApplicationContext();
	}

    /**Sets the list view listener on the given ExpandableListView.
    *
    * @param mylist  {@link ExpandableListView ExpandableListView }
    * */
	private void registerListClickCallback(ExpandableListView mylist) {
        mylist.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
                RecordListAdapter adapter = (RecordListAdapter)expandableListView.getExpandableListAdapter();

                ExpandableListItem item = (ExpandableListItem)adapter.getChild(i,i2);

                mListPosition = i;
                mItemPosition = i2;
	            HostageDBOpenHelper dbh = new HostageDBOpenHelper(getBaseContext());
                Record rec = dbh.getRecordOfAttackId((int) item.getTag());
                RecordOverviewFragment.this.pushRecordDetailViewForRecord(rec);
                return true;
            }
        });
        mylist.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int i) {
                RecordOverviewFragment.this.openSections.add(new Integer(i));
            }
        });
        mylist.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int i) {
                RecordOverviewFragment.this.openSections.remove(new Integer(i));
            }
        });
	}



	/*****************************
	 *
	 *          Date Transform
	 *
	 * ***************************/


    /**Returns the date format "H:mm d.M.yy" for the given timestamp (long)
    * @param timeStamp long */
	@SuppressLint("SimpleDateFormat")
	private String getDateAsString(long timeStamp) {

		try {
			DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
			Date netDate = (new Date(timeStamp));
			return sdf.format(netDate);
		} catch (Exception ex) {
			return "xx";
		}
	}


	/*****************************
	 *
	 *          Getter / Setter
	 *
	 * ***************************/

	public boolean isShowFilterButton() {
		return showFilterButton;
	}

	public void setShowFilterButton(boolean showFilterButton) {
		this.showFilterButton = showFilterButton;
	}




	/*****************************
	 *
	 *          Open Dialog Methods
	 *
	 * ***************************/

    /**Opens the grouping dialog*/
    private void openGroupingDialog(){
        ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_GROUP, this.groupingTitles(), this.selectedGroup(), false , this);
        newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_GROUP);
    }

    /**opens the bssid filter dialog*/
	private void openBSSIDFilterDialog(){
		ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_BSSID,this.bssids(), this.selectedBSSIDs(), true , this);
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_BSSID);
	}

    /**opens the essid filter dialog*/
	private void openESSIDFilterDialog(){
		ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_ESSID,this.essids(), this.selectedESSIDs(), true , this);
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_ESSID);
	}

    /**opens the protocol filter dialog*/
	private void openProtocolsFilterDialog(){
		ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_PROTOCOLS,this.protocolTitles(), this.selectedProtocols(), true , this);
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_PROTOCOLS);
	}

    /**opens the timestamp filter dialog (minimal timestamp required)*/
	private void openTimestampFromFilterDialog(){
		this.wasBelowTimePicker = false;
		DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
        if (this.filter.aboveTimestamp != Long.MIN_VALUE)newFragment.setDate(this.filter.aboveTimestamp);
	}

    /**opens time timestamp filter dialog (maximal timestamp required)*/
	private void openTimestampToFilterDialog(){
		this.wasBelowTimePicker = true;
		DateTimeDialogFragment newFragment = new DateTimeDialogFragment(this.getActivity());
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
        if (this.filter.belowTimestamp != Long.MAX_VALUE) newFragment.setDate(this.filter.belowTimestamp);
    }

    /**opens the sorting dialog*/
	private void openSortingDialog(){
		ChecklistDialog newFragment = new ChecklistDialog(FILTER_MENU_TITLE_SORTING,this.sortTypeTiles(), this.selectedSorttype(), false , this);
	    newFragment.show(this.getActivity().getFragmentManager(), FILTER_MENU_TITLE_SORTING);
	}

    /*****************************
     *
     *          Grouping Stuff
     *
     * ***************************/

    /**returns the group title for the given record. Uses the groupingKey to decied which value of the record should be used.
    * @param  rec {@link Record Record }
    * @return String grouptitle*/
    public String getGroupValue(Record rec){
        int index = this.groupingTitles().indexOf(this.groupingKey);
        switch (index){
            case 0:
                return rec.getProtocol();
            case 1:
                return rec.getBssid();
            case 2:
                return rec.getSsid();
            default:
                return rec.getProtocol();
        }
    }

    /**Returns the Group titles for the specified grouping key. e.g. groupingKey is "ESSID" it returns all available essids.
    * @return ArrayList<String> grouptitles*/
    public ArrayList<String> getGroupTitles(){
        int index = this.groupingTitles().indexOf(this.groupingKey);
        switch (index){
            case 0:
                return this.protocolTitles();
            case 1:
                return this.bssids();
            case 2:
                return this.essids();
            default:
                return this.protocolTitles();
        }
    }


	/*****************************
	 *
	 *          Filter Stuff
	 *
	 * ***************************/

    /**Returns the FilterButton.
     * @return ImageButton filterButton*/
    private ImageButton getFilterButton(){
        return (ImageButton) this.rootView.findViewById(R.id.FilterButton);
    }

    /**Opens the filter menu on a anchor view. The filter menu will always be on top of the anchor.
    * @param  v View the anchorView*/
	private void openFilterPopupMenuOnView(View v){

        SimplePopupTable filterMenu = new SimplePopupTable(this.getActivity(), new AbstractPopup.OnPopupItemClickListener() {
            public void onItemClick(Object ob) {
                if (ob instanceof  AbstractPopupItem){
                    AbstractPopupItem item = (AbstractPopupItem) ob;
                    RecordOverviewFragment.this.onFilterMenuItemSelected(item);
                }
            }
        });
        filterMenu.setTitle(FILTER_MENU_POPUP_TITLE);
		for(String title : RecordOverviewFragment.this.filterMenuTitles()){
            AbstractPopupItem item = null;
            if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)) continue;
            if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
                item = new SplitPopupItem(this.getActivity());
                item.setValue(SplitPopupItem.RIGHT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_BELOW);
                item.setValue(SplitPopupItem.LEFT_TITLE, FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
                if (this.filter.hasBelowTimestamp()){
                    item.setValue(SplitPopupItem.RIGHT_SUBTITLE, this.getDateAsString(this.filter.belowTimestamp));
                }
                if (this.filter.hasAboveTimestamp()){
                    item.setValue(SplitPopupItem.LEFT_SUBTITLE, this.getDateAsString(this.filter.aboveTimestamp));
                }
            } else {
                item = new SimplePopupItem(this.getActivity());
                item.setTitle(title);
                ((SimplePopupItem)item).setSelected(this.isFilterSetForTitle(title));
            }

            filterMenu.addItem(item);
		}
		filterMenu.showOnView(v);
	}

    /**Returns true  if the filter object is set for the given title otherwise false. e.g. the filter object has protocols,
    * so the method will return for the title FILTER_MENU_TITLE_PROTOCOLS TRUE.
    * @param  title String
    * @return boolean value
    * */
    private boolean isFilterSetForTitle(String title){
        if (title.equals(FILTER_MENU_TITLE_BSSID)){
            return this.filter.hasBSSIDs();
        }
        if (title.equals(FILTER_MENU_TITLE_ESSID)){
            return this.filter.hasESSIDs();
        }
        if (title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
            return this.filter.hasProtocols();
        }
        if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_BELOW)){
            return this.filter.hasBelowTimestamp();
        }
        if (title.equals(FILTER_MENU_TITLE_TIMESTAMP_ABOVE)){
            return this.filter.hasAboveTimestamp();
        }
        return false;
    }

    /**clears the filter. Does not invoke populatelistview!*/
	private void clearFilter(){
    	if(filter == null) this.filter = new LogFilter();
    	this.filter.clear();
	}

    /**Returns all grouping titles
    * @param  context Context
    * @return ArrayList<String> titles*/
    public ArrayList<String> groupingTitles(Context context){
        ArrayList<String> titles = new ArrayList<String>();
        for (String groupTitle : context.getResources().getStringArray(
                R.array.Grouping)) {
            titles.add(groupTitle);
        }
        return titles;
    }
    /**Returns all grouping titles.
    * @return ArrayList<String> tiles*/
    public ArrayList<String> groupingTitles(){
        ArrayList<String> titles = new ArrayList<String>();
        for (String groupTitle : this.getResources().getStringArray(
                R.array.Grouping)) {
            titles.add(groupTitle);
        }
        return titles;
    }
    /**
     * Returns a bool array. This array is true at the index of the groupingKey in groupingTitles(), otherwise false.
    * @return boolean[] selection
    * */
    public boolean[] selectedGroup(){
        ArrayList<String> groups = this.groupingTitles();
        boolean[] selected = new boolean[groups.size()];
        int i = 0;
        for(String group : groups){
            selected[i] =(group.equals(this.groupingKey));
            i++;
        }
        return selected;
    }

    /**Returns all protocol titles / names.
    * @return ArrayList<String> protocolTitles
    * */
	public ArrayList<String> protocolTitles(){
		ArrayList<String> titles = new ArrayList<String>();
		for (String protocol : this.getResources().getStringArray(
				R.array.protocols)) {
			titles.add(protocol);
		}
		return titles;
	}
    /**Return a boolean array of the selected / filtered protocols. If the filter object has
    * an protocol from the protocolTitles() array, the index of it will be true, otherwise false.
    * @return boolean[] protocol selection
    * */
	public boolean[] selectedProtocols(){
		ArrayList<String> protocols = this.protocolTitles();
		boolean[] selected = new boolean[protocols.size()];

		int i = 0;
		for(String protocol : protocols){
			selected[i] =(this.filter.protocols.contains(protocol));
			i++;
		}
		return selected;
	}

    /**
    * Returns the Sorttype Titles
    * @return ArayList<String> Sort type titles
    * */
	public ArrayList<String> sortTypeTiles(){
		ArrayList<String> titles = new ArrayList<String>();
		titles.add(MainActivity.getContext().getString(R.string.rec_time));
		titles.add(MainActivity.getContext().getString(R.string.rec_protocol));
		titles.add(MainActivity.getContext().getString(R.string.BSSID));
		titles.add(MainActivity.getContext().getString(R.string.ESSID));
		return titles;
	}
    /**
    * Returns an boolean array. The array is true at the index of the selected sort type..
    * The index of the selected sort type is the same index in the sortTypeTiles array.
    * @return boolean array, length == sortTypeTiles().length
    * */
	public boolean[] selectedSorttype(){
		ArrayList<String> types = this.sortTypeTiles();
		boolean[] selected = new boolean[types.size()];
		int i = 0;
		for(String sorttype : types){
			selected[i] =(this.filter.sorttype.toString().equals(sorttype));
			i++;
		}
		return selected;
	}

    /**
    * Returns all unique bssids.
    * @return ArrayList<String>
    * */
	public ArrayList<String> bssids(){
		ArrayList<String> records = dbh.getUniqueBSSIDRecords();
		return records;
	}
    /**
    * Returns an boolean array. The array is true at the indices of the selected bssids.
    * The index of the selected bssid is the same index in the bssids() array.
    * @return boolean array, length == bssids().length
    * */
	public boolean[] selectedBSSIDs(){
		ArrayList<String> bssids = this.bssids();
		boolean[] selected = new boolean[bssids.size()];

		int i = 0;
		for(String bssid : bssids){
			selected[i] =(this.filter.BSSIDs.contains(bssid));
			i++;
		}
		return selected;
	}

    /**
    * Returns all unique essids.
    * @return ArrayList<String>
    * */
	public ArrayList<String> essids(){
		ArrayList<String> records = dbh.getUniqueESSIDRecords();
		return records;
	}
    /**
    * Returns an boolean array. The array is true at the indices of the selected essids.
    * The index of the selected essid is the same index in the essids() array.
    * @return boolean array, length == essids().length
    * */
	public boolean[] selectedESSIDs(){
		ArrayList<String> essids = this.essids();
		boolean[] selected = new boolean[essids.size()];

		int i = 0;
		for(String essid : essids){
			selected[i] =(this.filter.ESSIDs.contains(essid));
			i++;
		}
		return selected;
	}

    /**
     * Returns all filter menu titles.
     * @return ArrayList<String>
     * */
	private ArrayList<String> filterMenuTitles(){
		ArrayList<String> titles = new ArrayList<String>();
		titles.add(FILTER_MENU_TITLE_BSSID);
		titles.add(FILTER_MENU_TITLE_ESSID);
		titles.add(FILTER_MENU_TITLE_PROTOCOLS);
		titles.add(FILTER_MENU_TITLE_TIMESTAMP_ABOVE);
		titles.add(FILTER_MENU_TITLE_TIMESTAMP_BELOW);
        if (this.filter.isSet())titles.add(FILTER_MENU_TITLE_REMOVE);
		return titles;
	}


	/*****************************
	 *
	 *          Listener Actions
	 *
	 * ***************************/

    /**
     * Will be called if the users selects a timestamp.
     * @param  dialog {@link DateTimeDialogFragment DateTimeDialogFragment }
     * */
	public void onDateTimePickerPositiveClick(DateTimeDialogFragment dialog) {
		if(this.wasBelowTimePicker){
			this.filter.setBelowTimestamp(dialog.getDate());
		} else {
			this.filter.setAboveTimestamp(dialog.getDate());
		}
        this.actualiseListViewInBackground();
        this.actualiseFilterButton();
    }
    /**
     * Will be called if the users cancels a timestamp selection.
     * @param dialog  {@link DateTimeDialogFragment DateTimeDialogFragment }
     * */
	public void onDateTimePickerNegativeClick(DateTimeDialogFragment dialog) {
		if(this.wasBelowTimePicker){
			this.filter.setBelowTimestamp(Long.MAX_VALUE);
		} else {
			this.filter.setAboveTimestamp(Long.MIN_VALUE);
		}
        this.actualiseFilterButton();
    }

    /**
     * Will be called if the users clicks the positiv button on a ChechlistDialog.
     * @param  dialog  {@link ChecklistDialog ChecklistDialog }
     */
	public void onDialogPositiveClick(ChecklistDialog dialog) {
		String title = dialog.getTitle();
		if(title.equals(FILTER_MENU_TITLE_BSSID)){
            ArrayList<String> titles =dialog.getSelectedItemTitles();
            if (titles.size() == this.bssids().size()){
                this.filter.setBSSIDs(new ArrayList<String>());
            } else {
                this.filter.setBSSIDs(titles);
            }
		}
		if(title.equals(FILTER_MENU_TITLE_ESSID)){
            ArrayList<String> titles =dialog.getSelectedItemTitles();
            if (titles.size() == this.essids().size()){
                this.filter.setESSIDs(new ArrayList<String>());
            } else {
                this.filter.setESSIDs(titles);
            }
		}
		if(title.equals(FILTER_MENU_TITLE_PROTOCOLS)){
            ArrayList<String> protocols = dialog.getSelectedItemTitles();
            if (protocols.size() == this.protocolTitles().size()){
                this.filter.setProtocols(new ArrayList<String>());
            } else {
			    this.filter.setProtocols(dialog.getSelectedItemTitles());
            }
		}
		if(title.equals(FILTER_MENU_TITLE_SORTING)){
			ArrayList<String> titles = dialog.getSelectedItemTitles();
            if (titles.size() == 0) return;
			String t = titles.get(0);
			int sortType = this.sortTypeTiles().indexOf(t);
			this.filter.setSorttype(SortType.values()[sortType]);
		}
        if (title.equals(FILTER_MENU_TITLE_GROUP)){
            ArrayList<String> titles = dialog.getSelectedItemTitles();
            if (titles.size() == 0) return;
            this.groupingKey =  titles.get(0);
        }
        this.actualiseListViewInBackground();

        this.actualiseFilterButton();
	}

    /**Paints the filter button if the current filter object is set.*/
    private void actualiseFilterButton(){
        if (this.filter.isSet() ){
            ImageButton filterButton = this.getFilterButton();
            if (filterButton != null){
                filterButton.setImageResource(R.drawable.ic_filter_pressed);
                filterButton.invalidate();
            }
        } else {
            ImageButton filterButton = this.getFilterButton();
            if (filterButton != null){
                filterButton.setImageResource(R.drawable.ic_filter);
                filterButton.invalidate();
            }
        }
    }

    /**
     * Will be called if the users clicks the negativ button on a ChechlistDialog.
     * @param  dialog  {@link ChecklistDialog ChecklistDialog }
     */
	public void onDialogNegativeClick(ChecklistDialog dialog) {}


	/*****************************
	 *
	 *          TEST
	 *
	 * ***************************/

    /**
    * This will clear the database at first and than add new attacks.
    * @param createNetworks number of networks to create
    * @param attacksPerNetwork maximal number of attack per network
    * */
	private void addRecordToDB( int createNetworks, int attacksPerNetwork) {
        if ((dbh.getRecordCount() > 0)) dbh.clearData();

		Calendar cal = Calendar.getInstance();

		int maxProtocolsIndex = this.getResources().getStringArray(
				R.array.protocols).length;

		Random random = new Random();

		LatLng tudarmstadtLoc = new LatLng(49.86923, 8.6632768);


		final double ssidRadius = 0.1;
		final double bssidRadius = 0.004;

        int attackId = 0;

        for (int numOfNetworks = 0; numOfNetworks < createNetworks; numOfNetworks++){
            String ssidName = "WiFi" + ((numOfNetworks) + 1);
            String bssidName = "127.0.0." + ((numOfNetworks) + 1);

            int protocolIndex = numOfNetworks % maxProtocolsIndex;
            String protocolName = this.getResources().getStringArray(
                    R.array.protocols)[protocolIndex];

            int numOfAttackPerNetwork = (Math.abs(random.nextInt()) % attacksPerNetwork) + 1;

            NetworkRecord network = new NetworkRecord();
            network.setBssid(bssidName);
            network.setSsid(ssidName);

            LatLng ssidLocation = new LatLng(tudarmstadtLoc.latitude - ssidRadius + 2.0 * ssidRadius * Math.random(), tudarmstadtLoc.longitude - ssidRadius + 2.0 * ssidRadius * Math.random());
            double latitude = ssidLocation.latitude - bssidRadius + 2.0 * bssidRadius * Math.random();
            double longitude = ssidLocation.longitude - bssidRadius + 2.0 * bssidRadius * Math.random();

            long timestamp = cal.getTimeInMillis();
            network.setTimestampLocation(timestamp);
            network.setLongitude(longitude);
            network.setLatitude(latitude);
            network.setAccuracy(0.f);

            dbh.updateNetworkInformation(network);

            // ATTACKS PER NETWORK
            for (int attackNumber = 0; attackNumber < numOfAttackPerNetwork; attackNumber++) {

                int numRecordsPerAttack = (Math.abs(random.nextInt()) % 5) + 1;
                /*
                * ADD A ATTACK*/

                AttackRecord attack = new AttackRecord();
                attack.setAttack_id(attackId);

                attack.setBssid(bssidName);

                attack.setProtocol(protocolName);
                attack.setLocalIP(bssidName);


                dbh.addAttackRecord(attack);

                // RECORDS PER ATTACK
                for (int messageID = attackId; messageID < attackId + numRecordsPerAttack; messageID++) {
                    MessageRecord message = new MessageRecord();
                    message.setId(messageID);
                    message.setAttack_id(attackId);

                    // GO BACK IN TIME
                    message.setTimestamp(cal.getTimeInMillis()
                            - ((messageID * 60 * 60 * 24) * 1000) + (1000 * ((messageID - attackId) + 1)));

                    if ((messageID - attackId) % 2 == 0){
                        message.setType(MessageRecord.TYPE.RECEIVE);
                    } else {
                        message.setType(MessageRecord.TYPE.SEND);
                    }
                    message.setPacket("");

                    dbh.addMessageRecord(message);
                }

                attackId+=numRecordsPerAttack;
            }

        }

//        int countAllLogs = dbh.getAllRecords().size();
//        int countRecords = dbh.getRecordCount();
//        int countAttacks = dbh.getAttackCount();
//
//        if ((countRecords == 0)) {
//            Record rec = dbh.getRecordOfAttackId(0);
//            Record rec2 = dbh.getRecord(0);
//
//            System.out.println("" + "Could not create logs!");
//        }

    }


    /**Navigation. Shows the record detail view for the given record
    * @param  record  {@link Record Record } to show
    * */
    private void pushRecordDetailViewForRecord(Record record){

        FragmentManager fm = this.getActivity().getFragmentManager();

        if (fm != null){
            RecordDetailFragment newFragment = new RecordDetailFragment();
            newFragment.setRecord(record);

            newFragment.setUpNavigatible(true);

            MainActivity.getInstance().injectFragment(newFragment);

        }

    }
}