package com.example.habitrack;

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

import java.util.ArrayList;


/**
 *  Handles all the Activities on the Main Page once Logged in
 */

public class MainActivity extends AppCompatActivity {

    // declare components
    Button createTypeButton;
    Button historybutton;
    Button allButton;
    Button logoutButton;
    private ListView displayNames;
    private ArrayList<HabitType> today = new ArrayList<HabitType>();
    //private ArrayAdapter<String> adapter;
    private ArrayAdapter<HabitType> adapter;
    ArrayList<String> todaysHabits = new ArrayList<>();
    HabitTypeController htc = new HabitTypeController(this);
    HabitEventController hec = new HabitEventController(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Preliminary Setup
        // 1. Get the controllers
        // 2. Get IDs if they're saved. Otherwise, create new IDs
        htc.loadHTID();
        hec.loadHEID();
        // 3. Restore all HT and HE if saved
        htc.loadFromFile();
        hec.loadFromFile();
        // 4. Get Recent events and HabitTypes for today
        htc.generateHabitsForToday();
        hec.updateRecentHabitEvents();

        createTypeButton = (Button) findViewById(R.id.button);
        allButton = (Button) findViewById(R.id.button2);
        historybutton = (Button) findViewById(R.id.button3);
        logoutButton = (Button) findViewById(R.id.button10);
        displayNames = (ListView) findViewById(R.id.listView);

        // Checks if app is in a logged in state. If not, goes to login page (SignupActivity)
        SharedPreferences loggedInPrefs = getApplicationContext().getSharedPreferences("loggedInStatus", MODE_PRIVATE);
        final SharedPreferences.Editor loggedInEditor = loggedInPrefs.edit();
        boolean isLoggedIn = loggedInPrefs.getBoolean("loggedIn", false);
        final Intent toLogIn = new Intent(getApplicationContext(), SignupActivity.class);
        if(!isLoggedIn) {
            startActivity(toLogIn);
        }

        //If logoutButton is clicked, change loggedIn to false and go to SignupAcitivty
        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loggedInEditor.putBoolean("loggedIn", false);
                loggedInEditor.apply();
                startActivity(toLogIn);
            }
        });


        // Handles if user pressed CREATE button , redirects to create a new habit type class
        createTypeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent newType = new Intent(getApplicationContext(), NewHabitTypeActivity.class);
                startActivity(newType);
            }
        });

        // Handles if user pressed HISTORY button , redirects to history class
        historybutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent newType = new Intent(getApplicationContext(), HabitHistory.class);
                startActivity(newType);
            }
        });

        // Handles if user pressed ALL button, redirects to all habit types
        allButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent viewAll = new Intent(getApplicationContext(), AllHabitTypesActivity.class);
                startActivity(viewAll);
            }
        });

        // Handles the pressing of Habits on the Main Activity to launch a new habit event
        displayNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, NewHabitEventActivity.class);
                //intent.putExtra("HabitTitle", displayNames.getItemAtPosition(i).toString());
                //Log.d("position", displayNames.getItemAtPosition(i).toString());
                intent.putExtra("habitID", today.get(i).getID());
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();

        htc.loadHTID();
        hec.loadHEID();
        // 3. Restore all HT and HE if saved
        htc.loadFromFile();
        hec.loadFromFile();
        // 4. Get Recent events and HabitTypes for today
        //htc.generateHabitsForToday();
        today = htc.getHabitTypesForToday();
        hec.updateRecentHabitEvents();


        adapter = new ArrayAdapter<HabitType>(this, R.layout.list_item, today);
        displayNames.setAdapter(adapter);

        }

    }


@@ -177,33 +177,8 @@
 ty;%0A
-import android.util.Log;%0A
 impo
@@ -726,52 +726,8 @@
 ();%0A
-    //private ArrayAdapter%3CString%3E adapter;%0A
     
@@ -775,59 +775,58 @@
     
-ArrayList%3CString%3E todaysHabits = new ArrayList%3C%3E();
+// Preliminary Setup%0A    // 1. Get the controllers
 %0A   
@@ -1113,420 +1113,8 @@
 in);
-%0A        // Preliminary Setup%0A        // 1. Get the controllers%0A        // 2. Get IDs if they're saved. Otherwise, create new IDs%0A        htc.loadHTID();%0A        hec.loadHEID();%0A        // 3. Restore all HT and HE if saved%0A        htc.loadFromFile();%0A        hec.loadFromFile();%0A        // 4. Get Recent events and HabitTypes for today%0A        htc.generateHabitsForToday();%0A        hec.updateRecentHabitEvents();
 %0A%0A  
@@ -1161,25 +1161,36 @@
 ewById(R.id.
-b
+createHabitB
 utton);%0A    
@@ -1236,15 +1236,17 @@
 .id.
-b
+allB
 utton
-2
 );%0A 
@@ -1299,15 +1299,21 @@
 .id.
-b
+historyB
 utton
-3
 );%0A 
@@ -1435,16 +1435,37 @@
 tView);%0A
+// ------------------
 %0A       
@@ -2378,16 +2378,37 @@
     %7D);%0A
+// ------------------
 %0A%0A      
@@ -3931,186 +3931,8 @@
 s);%0A
-                //intent.putExtra(%22HabitTitle%22, displayNames.getItemAtPosition(i).toString());%0A                //Log.d(%22position%22, displayNames.getItemAtPosition(i).toString());%0A
     
@@ -4136,16 +4136,34 @@
 tart();%0A
+        // 2. load
 %0A       
@@ -4369,18 +4369,16 @@
         
-//
 htc.gene
@@ -4484,17 +4484,16 @@
 nts();%0A%0A
-%0A
         

simplify code by moving the loading code to onStart()