package com.example.habitrack;

import java.util.Calendar;

import java.util.ArrayList;

/**
 * HabitEventStateManager
 *
 * Version 1.0
 *
 * Created by sshussai on 11/5/17.
 */

public class HabitEventStateManager {
    /**
     * Version 1.0
     * This class is the state manager for all Habit event related entities
     *
     */

    private static Integer habitEventID;

    public static HabitEventStateManager heManager = new HabitEventStateManager();

    //private static final ArrayList<Integer> ALL_HABITEVENTS_ID = new ArrayList<Integer>();
    private static final ArrayList<HabitEvent> ALL_HABITEVENTS = new ArrayList<HabitEvent>();
    private static final ArrayList<HabitEvent> RECENT_HABITEVENTS = new ArrayList<HabitEvent>();


    private HabitEventStateManager(){
        habitEventID = 0;
    }

    public static HabitEventStateManager getHEStateManager(){
        return heManager;
    }

    public void updateRecentHabitEvents(){
        // Get date from 4 weeks ago, by adding -28 to today's date
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -28);
        // Check if any currently recent events are old
        for(Integer count = 0; count < RECENT_HABITEVENTS.size(); count++){
            Calendar currCal = RECENT_HABITEVENTS.get(count).getDate();
            if (currCal.before(cal)){
                RECENT_HABITEVENTS.remove(count.intValue());
            }

        }
    }

    public void storeHabitEvent(HabitEvent he){
        RECENT_HABITEVENTS.add(he);
        ALL_HABITEVENTS.add(he);
        //ALL_HABITEVENTS_ID.add(he.getHabitEventID());
    }

    public HabitEvent getHabitEvent(Integer requestedID){
        for(Integer count = 0; count < RECENT_HABITEVENTS.size(); count++){
            if(RECENT_HABITEVENTS.get(count).getHabitEventID() == requestedID){
                return RECENT_HABITEVENTS.get(count);
            }
        }
        for(Integer count = 0; count < ALL_HABITEVENTS.size(); count++){
            if(ALL_HABITEVENTS.get(count).getHabitEventID() == requestedID){
                return ALL_HABITEVENTS.get(count);
            }
        }
        HabitEvent he = new HabitEvent(-1, -1);
        return he;
    }


    public void removeHabitEvent(Integer requestedID){
        for(Integer count = 0; count < RECENT_HABITEVENTS.size(); count++){
            if(RECENT_HABITEVENTS.get(count).getHabitEventID() == requestedID){
                RECENT_HABITEVENTS.remove(count.intValue());
            }
        }
        for(Integer count = 0; count < ALL_HABITEVENTS.size(); count++){
            if(ALL_HABITEVENTS.get(count).getHabitEventID() == requestedID){
                ALL_HABITEVENTS.remove(count.intValue());
            }
        }
        /*
        for(Integer count = 0; count < ALL_HABITEVENTS_ID.size(); count++){
            if(ALL_HABITEVENTS_ID.get(count) == requestedID){
                ALL_HABITEVENTS_ID.remove(count.intValue());
            }
        }
        */
    }

    public Integer getHabitEventID(){
        HabitEventStateManager.habitEventID++;
        return HabitEventStateManager.habitEventID;
    }


}

@@ -25,16 +25,48 @@
 track;%0A%0A
+import java.lang.reflect.Array;%0A
 import j
@@ -154,25 +154,25 @@
 %0A * Version 
-1
+2
 .0%0A *%0A * Cre
@@ -266,17 +266,17 @@
 Version 
-1
+2
 .0%0A     
@@ -346,16 +346,87 @@
 ntities%0A
+     * Added load and save functions for events, and for habitevent ID%0A
      *%0A 
@@ -663,38 +663,32 @@
  private static 
-final 
 ArrayList%3CHabitE
@@ -751,38 +751,32 @@
  private static 
-final 
 ArrayList%3CHabitE
@@ -1552,42 +1552,409 @@
 lic 
-void storeHabitEvent(HabitEvent he
+ArrayList%3CHabitEvent%3E getAllHabitEvents()%7B%0A        return ALL_HABITEVENTS;%0A    %7D%0A%0A    public ArrayList%3CHabitEvent%3E getRecentHabitevents()%7B%0A        updateRecentHabitEvents();%0A        return ALL_HABITEVENTS;%0A    %7D%0A%0A    public void setAllHabitEvents(ArrayList%3CHabitEvent%3E allEvents)%7B%0A        this.ALL_HABITEVENTS = allEvents;%0A    %7D%0A%0A    public void setRecentHabitEvents(ArrayList%3CHabitEvent%3E recentEvents
 )%7B%0A 
@@ -1952,32 +1952,37 @@
 vents)%7B%0A        
+this.
 RECENT_HABITEVEN
@@ -1979,33 +1979,272 @@
 _HABITEVENTS
-.add(he);
+ = recentEvents;%0A    %7D%0A%0A    public void removeAllHabitEvents()%7B%0A        this.ALL_HABITEVENTS.clear();%0A    %7D%0A%0A    public void removeRecentHabitEvents()%7B%0A        this.RECENT_HABITEVENTS.clear();;%0A    %7D%0A%0A    public void storeHabitEvent(HabitEvent he)%7B
 %0A        ALL
@@ -2265,24 +2265,26 @@
 dd(he);%0A
+//
         
 //ALL_HA
@@ -2275,18 +2275,16 @@
         
-//
 ALL_HABI
@@ -3692,32 +3692,228 @@
       */%0A    %7D%0A%0A
+    public void setID(Integer savedID)%7B%0A        HabitEventStateManager.habitEventID = savedID;%0A    %7D%0A%0A    public Integer getIDToSave()%7B%0A        return HabitEventStateManager.habitEventID;%0A    %7D%0A%0A%0A
     public Integ

add setters/getters for id and all events and recent events