package com.androidapp.richard.startfresh.Activities;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.androidapp.richard.startfresh.R;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class FoodTypeSelectionActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_food_type_selection);
        final ListView foodSelectionList = (ListView) findViewById(R.id.food_selection_list_view);
        EditText searchTextField = (EditText) findViewById(R.id.search_foods_edit_text);
        final ArrayList<String> listOfFoods = new ArrayList<String>();
        FirebaseApp.initializeApp(this);
        DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
        if (getIntent().getExtras() != null) {
            String foodGroupSelected = getIntent().getExtras().getString("food group selected");
            dbRef.child("food groups").child(foodGroupSelected).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot == null) {
                        Log.d("ondatachange", "is null");
                    } else {
                        for (DataSnapshot child : dataSnapshot.getChildren()) {
                            listOfFoods.add(child.getValue().toString());
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            final ArrayAdapter adapter = new ArrayAdapter(this,
                    android.R.layout.simple_list_item_1, listOfFoods);
            foodSelectionList.setAdapter(adapter);
            foodSelectionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                }
            });
            searchTextField.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void afterTextChanged(Editable editable) {
                    String currentText = editable.toString();
                    Log.d("liststuff", "ontextchanged with size" + String.valueOf(listOfFoods.size()));
                    ArrayList<String> originalList = new ArrayList<String>();
                    for(int i =0; i < listOfFoods.size(); i ++){
                        originalList.add(listOfFoods.get(i));
                    }
                    listOfFoods.clear();
                    for (int j = 0; j< originalList.size(); j ++){
                        Log.d("liststuff", "for loop");
                        if(currentText.equals(originalList.get(j).substring(0, editable.length()))){
                            listOfFoods.add(originalList.get(j));
                            Log.d("liststuff", "for loop if");
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            });
        }

    }


}

@@ -791,16 +791,52 @@
 ivity %7B%0A
+    ArrayList%3CString%3E originalList;%0A
     @Ove
@@ -837,24 +837,24 @@
   @Override%0A
-
     protecte
@@ -1006,16 +1006,64 @@
 ction);%0A
+        originalList = new ArrayList%3CString%3E();%0A
         
@@ -2082,24 +2082,99 @@
 oString());%0A
+                            originalList.add(child.getValue().toString());%0A
             
@@ -3308,339 +3308,8 @@
 ();%0A
-                    Log.d(%22liststuff%22, %22ontextchanged with size%22 + String.valueOf(listOfFoods.size()));%0A                    ArrayList%3CString%3E originalList = new ArrayList%3CString%3E();%0A                    for(int i =0; i %3C listOfFoods.size(); i ++)%7B%0A                        originalList.add(listOfFoods.get(i));%0A                    %7D%0A
     
@@ -3416,64 +3416,8 @@
 +)%7B%0A
-                        Log.d(%22liststuff%22, %22for loop%22);%0A
     
@@ -3513,16 +3513,16 @@
 h())))%7B%0A
+
         
@@ -3583,71 +3583,8 @@
 ));%0A
-                            Log.d(%22liststuff%22, %22for loop if%22);%0A
     

edit food search algorithm