package com.example.habitrack;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * Will display all the Habit Types on Screen.
 * User able to click on Habit Types to redirect to HabitType Details page
 */
public class AllHabitTypesActivity extends AppCompatActivity {
    // declare components
    // int typeID = 0;
    private ListView allTypes;
    //private ArrayAdapter<String> adapter;
    private ArrayAdapter<HabitType> adapter;
    private static ArrayList<HabitType> typeList = new ArrayList<HabitType>();
    // private static ArrayList<String> namesOfTypes = new ArrayList<String>();

    HabitTypeController htc = new HabitTypeController(this);

    /**
     * Initializes the ListView.
     * @param savedInstanceState
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_habit_types);
        // initialize ListView
        allTypes = (ListView) findViewById(R.id.listView2);

        allTypes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(AllHabitTypesActivity.this, HabitTypeDetailsActivity.class);
                //intent.putExtra("HabitTitle", allTypes.getItemAtPosition(i).toString());
                intent.putExtra("habitID", typeList.get(i).getID());
                startActivity(intent);
//                final ArrayList<HabitType> today = HabitTypeStateManager.getHTStateManager().getHabitTypesForToday();
//                HabitType iterater = null;
//                for (int j = 0; j < today.size(); j++)
//                    iterater = today.get(j);
//                Log.d("iterator", iterater.getTitle());
//                if (allTypes.getItemAtPosition(i).toString().equals(iterater.getTitle())) {
//                    typeID = iterater.getID() ;
//                }
//                intent.putExtra("typeID", typeID);

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    /**
     * Get IDs and reload data from File every time app is in onStart
     * Will populize the array for names that will be displayed
     * and handle the necessary IDs for when a Habit Type is selected
     */
    @Override
    protected void onStart() {
        super.onStart();
        // Restore all HT
//        htc.loadFromFile();

        // Get Recent events and HabitTypes for today
        //htc.getHabitTypesForToday();
//        htc.generateHabitsForToday();
        typeList = HabitTypeStateManager.getHTStateManager().getAllHabitTypes();
//        Log.d("start", "Entered start");
//        Log.d("array", typeList.toString());
//        if (!(typeList.isEmpty())) {
//            for (int i = 0; i < typeList.size(); i++) {
//                HabitType tempHt = typeList.get(i);
//                String stringTitle = tempHt.getTitle();
//                namesOfTypes.add(stringTitle);
//                Log.d("stringTitle", stringTitle);
//            }
//        }

        //adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, namesOfTypes);
        adapter = new ArrayAdapter<HabitType>(this, R.layout.list_item, typeList);
        allTypes.setAdapter(adapter);
    }
/*
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("start", "Entered resume");
        namesOfTypes.clear();
        htc.getHabitTypesForToday();
        hc.updateRecentHabitEvents();
        typeList = HabitTypeStateManager.getHTStateManager().getAllHabitTypes();
        if (!(typeList.isEmpty())) {
            for (int i = 0; i < typeList.size(); i++) {
                HabitType tempHt = typeList.get(i);
                String stringTitle = tempHt.getTitle();
                namesOfTypes.add(stringTitle);
            }
        }
        adapter.notifyDataSetChanged();
    }
    */
}


@@ -259,16 +259,45 @@
 istView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -670,16 +670,24 @@
 abitType
+Metadata
 %3E adapte
@@ -685,32 +685,34 @@
 a%3E adapter;%0A    
+//
 private static A
@@ -773,19 +773,16 @@
 %3E();%0A   
- //
  private
@@ -803,115 +803,71 @@
 ist%3C
-String%3E namesOfTypes = new ArrayList%3CString%3E();%0A%0A    HabitTypeController htc = new HabitTypeController(this
+HabitTypeMetadata%3E htMDList = new ArrayList%3CHabitTypeMetadata%3E(
 );%0A%0A
@@ -1623,39 +1623,26 @@
     
-intent.putExtra(%22habitID%22, type
+String esID = htMD
 List
@@ -1656,13 +1656,14 @@
 .get
+Es
 ID()
-)
 ;%0A  
@@ -1680,360 +1680,206 @@
     
-startActivity(intent);%0A//                final ArrayList%3CHabitType%3E today = HabitTypeStateManager.getHTStateManager().getHabitTypesForToday();%0A//                H
+if(esID == null %7C%7C esID == %22%22)%7B%0A                    Toast.makeText(getApplicationContext(), %22This h
 abit
-T
+ t
 ype 
-iterater = null;%0A//                for (int j = 0; j %3C today.size(); j++)%0A//                    iterater = today.get(j);%0A//                Log.d(%22iterator%22, iterater.getTitle()
+has not been saved.%5CnIt cannot be edited currently edited.%22, Toast.LENGTH_SHORT).show(
 );%0A
-//
     
@@ -1894,86 +1894,17 @@
     
-if (allTypes.getItemAtPosition(i).toString().equals(iterater.getTitle()))
+%7D else
  %7B%0A
-//
     
@@ -1923,109 +1923,99 @@
     
-typeID = iterater.getID() ;%0A//                %7D%0A//                intent.putExtra(%22typeID%22, typeID);%0A
+intent.putExtra(%22esID%22, esID);%0A                    startActivity(intent);%0A                %7D
 %0A   
@@ -2483,683 +2483,88 @@
  HT%0A
-//        htc.loadFromFile();%0A%0A        // Get Recent events and HabitTypes for today%0A        //htc.getHabitTypesForToday();%0A//        htc.generateHabitsForToday();%0A        typeList = HabitTypeStateManager.getHTStateManager().getAllHabitTypes();%0A//        Log.d(%22start%22, %22Entered start%22);%0A//        Log.d(%22array%22, typeList.toString
+%0A        htMDList = HabitTypeStateManager.getHTStateManager().getAllMetadata
 ()
-)
 ;%0A
-//        if (!(typeList.isEmpty())) %7B%0A//            for (int i = 0; i %3C typeList.size(); i++) %7B%0A//                HabitType tempHt = typeList.get(i);%0A//                String stringTitle = tempHt.getTitle();%0A//                namesOfTypes.add(stringTitle);%0A//                Log.d(%22stringTitle%22, stringTitle);%0A//            %7D%0A//        %7D%0A%0A
     
@@ -2706,16 +2706,24 @@
 abitType
+Metadata
 %3E(this, 
@@ -2742,20 +2742,20 @@
 t_item, 
-type
+htMD
 List);%0A 
@@ -2801,635 +2801,8 @@
   %7D%0A
-/*%0A    @Override%0A    protected void onResume() %7B%0A        super.onResume();%0A        Log.d(%22start%22, %22Entered resume%22);%0A        namesOfTypes.clear();%0A        htc.getHabitTypesForToday();%0A        hc.updateRecentHabitEvents();%0A        typeList = HabitTypeStateManager.getHTStateManager().getAllHabitTypes();%0A        if (!(typeList.isEmpty())) %7B%0A            for (int i = 0; i %3C typeList.size(); i++) %7B%0A                HabitType tempHt = typeList.get(i);%0A                String stringTitle = tempHt.getTitle();%0A                namesOfTypes.add(stringTitle);%0A            %7D%0A        %7D%0A        adapter.notifyDataSetChanged();%0A    %7D%0A    */
 %0A%7D%0A%0A

change allht list to read from htmetadata lists