package com.example.habitrack;

import android.util.Log;

import java.util.ArrayList;
import java.util.Calendar;

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

public class HabitEventStateManager {
    /**
     * Version 2.0
     * This class is the state manager for all Habit event related entities
     * Added load and save functions for events, and for habitevent ID
     *
     */

    private static Integer habitEventID;


    public static HabitEventStateManager heManager = new HabitEventStateManager();

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

    private static ArrayList<HabitEvent> ALL_HABITEVENTS = new ArrayList<HabitEvent>();
    private static ArrayList<HabitEvent> RECENT_HABITEVENTS = new ArrayList<HabitEvent>();


    public HabitEventStateManager(){}

    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 ArrayList<HabitEvent> getAllHabitEvents(){
        return ALL_HABITEVENTS;
    }

    public ArrayList<HabitEvent> getRecentHabitevents(){
        updateRecentHabitEvents();
        return ALL_HABITEVENTS;
    }

    public void setAllHabitEvents(ArrayList<HabitEvent> allEvents){
        this.ALL_HABITEVENTS = allEvents;
    }

    public void setRecentHabitEvents(ArrayList<HabitEvent> recentEvents){
        this.RECENT_HABITEVENTS = recentEvents;
    }

    public void removeAllHabitEvents(){
        this.ALL_HABITEVENTS.clear();
    }

    public void removeRecentHabitEvents(){
        this.RECENT_HABITEVENTS.clear();;
    }

    public void storeHabitEvent(HabitEvent he){
        ALL_HABITEVENTS.add(he);
        Log.d("seen",ALL_HABITEVENTS.toString());
//        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 void setID(Integer savedID){
        HabitEventStateManager.habitEventID = savedID;
        Log.d("mm","last test "+ savedID.toString());
    }

    public Integer getIDToSave(){
        return HabitEventStateManager.habitEventID;
    }


    public Integer getHabitEventID(){
        HabitEventStateManager.habitEventID++;
        Log.d("tag", habitEventID.toString());
        return HabitEventStateManager.habitEventID;
    }


}

@@ -467,28 +467,27 @@
 ventID;%0A
-%0A%0A
     p
-ublic
+rivate
  static 
@@ -825,16 +825,110 @@
 ent%3E();%0A
+    private static ArrayList%3CHabitEvent%3E HABITEVENTS_FOR_TODAY = new ArrayList%3CHabitEvent%3E();%0A
 %0A%0A    pu
@@ -1572,25 +1572,24 @@
           %7D%0A
-%0A
         %7D%0A  
@@ -1585,32 +1585,338 @@
        %7D%0A    %7D%0A%0A
+    // setter and getter for HABITEVENTS_FOR_TODAY%0A    public ArrayList%3CHabitEvent%3E getALlHabitEventsForToday() %7B%0A        return HABITEVENTS_FOR_TODAY;%0A    %7D%0A%0A    public void setAllHabitEventsForToday(ArrayList%3CHabitEvent%3E habiteventsForToday) %7B%0A        HABITEVENTS_FOR_TODAY = habiteventsForToday;%0A    %7D%0A%0A
     public Array
@@ -2594,16 +2594,55 @@
 nt he)%7B%0A
+        HABITEVENTS_FOR_TODAY.add(he);%0A
         
@@ -4393,24 +4393,23 @@
 ventID;%0A
+
     %7D%0A%0A
-%0A
     publ

add new hefortoday list to hestatemanager, and its related necessary functions