package com.example.habitrack;

import android.util.Log;

import java.util.Calendar;

import java.util.ArrayList;

/**
 * HabitTypeStateManager
 *
 * Version 2.0
 *
 * Created by sshussai on 11/4/17.
 *
 */

public class HabitTypeStateManager {

    /**
     * This class is the state manager for all Habit Type related events. It contains a
     * list that has all the habit types created so far, along with all the habit types
     * for today.
     * It can add habit types to the list, retrieve and remove them, and calculate habit
     * types for today.
     * Added functions to set and get loaded or saved IDs
     *
     */

    private static Integer habitTypeID;
    private static Calendar habitTypeDate;
    private static Calendar cal = Calendar.getInstance();
    public static HabitTypeStateManager htManager = new HabitTypeStateManager();

    private static ArrayList<HabitType> ALL_HABITTYPES = new ArrayList<HabitType>();
    private static ArrayList<HabitType> HABITTYPES_FOR_TODAY = new ArrayList<HabitType>();


    private HabitTypeStateManager(){}

    public static HabitTypeStateManager getHTStateManager(){
        return htManager;
    }

    public static void calculateHabitsForToday(){
        if(ALL_HABITTYPES.isEmpty()){
            HABITTYPES_FOR_TODAY = new ArrayList<HabitType>();
        } else {
            HABITTYPES_FOR_TODAY.clear();
            for (Integer count = 0; count < ALL_HABITTYPES.size(); count++) {
                Calendar currCal = ALL_HABITTYPES.get(count).getStartDate();
                if (currCal.compareTo(cal) <= 0) {
                    ArrayList<Integer> schedule = ALL_HABITTYPES.get(count).getSchedule();
                    for (Integer cnt = 0; cnt < schedule.size(); cnt++) {
                        if (schedule.get(cnt) == cal.get(Calendar.DAY_OF_WEEK)) {
                            HABITTYPES_FOR_TODAY.add(ALL_HABITTYPES.get(count));
                            break;
                        }
                    }
                }
            }
        }
    }

    public ArrayList<HabitType> getAllHabitTypes(){
        return ALL_HABITTYPES;
    }

    public void setAllHabittypes(ArrayList<HabitType> allHabittypes){
        this.ALL_HABITTYPES = allHabittypes;
    }

    public ArrayList<HabitType> getHabitTypesForToday(){
        calculateHabitsForToday();
        return HABITTYPES_FOR_TODAY;
    }

    public void removeAllHabitTypes(){
        ALL_HABITTYPES.clear();
    }

    public void removeHabitTypesForToday(){
        HABITTYPES_FOR_TODAY.clear();
    }

    public void storeHabitType(HabitType ht){
        ALL_HABITTYPES.add(ht);



        HABITTYPES_FOR_TODAY.add(ht);



    }

    public HabitType getHabitType(Integer requestedID){
        for(Integer count = 0; count < HABITTYPES_FOR_TODAY.size(); count++){
            if(HABITTYPES_FOR_TODAY.get(count).getID() == requestedID){
                return HABITTYPES_FOR_TODAY.get(count);
            }
        }
        for(Integer count = 0; count < ALL_HABITTYPES.size(); count++){
            if(ALL_HABITTYPES.get(count).getID() == requestedID){
                return ALL_HABITTYPES.get(count);
            }
        }
        HabitType ht = new HabitType(-1);
        return ht;
    }

    public void removeHabitType(Integer requestedID){
        for(Integer count = 0; count < ALL_HABITTYPES.size(); count++){
            if(ALL_HABITTYPES.get(count).getID() == requestedID){
                HabitType rmht = ALL_HABITTYPES.get(count);
                ALL_HABITTYPES.remove(rmht);
            }
        }
        for(Integer count = 0; count < HABITTYPES_FOR_TODAY.size(); count++){
            if(HABITTYPES_FOR_TODAY.get(count).getID() == requestedID){
                HabitType rmht = HABITTYPES_FOR_TODAY.get(count);
                HABITTYPES_FOR_TODAY.remove(rmht);
            }
        }
    }

    public Calendar getHabitTypeDate() {
        return habitTypeDate;
    }

    public void setHabitTypeDate(Calendar habitTypeDate) {
        HabitTypeStateManager.habitTypeDate = habitTypeDate;
    }

    public void setID(Integer savedID){
        HabitTypeStateManager.habitTypeID = savedID;
    }

    public Integer getIDToSave(){
        return HabitTypeStateManager.habitTypeID;
    }

    public Integer getHabitTypeID(){
        HabitTypeStateManager.habitTypeID++;
        return HabitTypeStateManager.habitTypeID;
    }
}

@@ -715,66 +715,8 @@
 te;%0A
-    private static Calendar cal = Calendar.getInstance();%0A
     
@@ -1146,32 +1146,103 @@
 bitsForToday()%7B%0A
+        // If no habit types defined, then return empty list for today%0A
         if(ALL_H
@@ -1359,37 +1359,170 @@
     
-HABITTYPES_FOR_TODAY.clear();
+// Get today%0A            Calendar cal = Calendar.getInstance();%0A            // Otherwise%0A            HABITTYPES_FOR_TODAY.clear();       // clear the current list
 %0A   
@@ -1588,32 +1588,75 @@
 e(); count++) %7B%0A
+                // for all the habit types%0A
                 
@@ -1719,59 +1719,255 @@
 e();
-%0A                if (currCal.compareTo(cal) %3C= 0) %7B
+    // get date for habit type%0A                // If the date for the habit type is before the current day%0A                if (currCal.compareTo(cal) %3C= 0) %7B%0A                    // Then get the schedule for the habit, and compare each day to today
 %0A   
@@ -2206,24 +2206,113 @@
 OF_WEEK)) %7B%0A
+                            // If today matches a day, then add to habit types for today%0A
             
@@ -3109,52 +3109,8 @@
 t);%0A
-%0A%0A%0A        HABITTYPES_FOR_TODAY.add(ht);%0A%0A%0A%0A
     
@@ -4726,24 +4726,58 @@
 itTypeID()%7B%0A
+        if(habitTypeID %3E= 0)%7B%0A    
         Habi
@@ -4809,16 +4809,20 @@
 peID++;%0A
+    
         
@@ -4859,20 +4859,69 @@
 abitTypeID;%0A
+        %7D else %7B%0A            return 0;%0A        %7D%0A
     %7D%0A%7D%0A

add comments and localize variable