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(){
        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;
    }
}

@@ -1204,32 +1204,154 @@
 bitsForToday()%7B%0A
+        if(ALL_HABITTYPES.isEmpty())%7B%0A            HABITTYPES_FOR_TODAY = new ArrayList%3CHabitType%3E();%0A        %7D else %7B%0A    
         HABITTYP
@@ -1372,35 +1372,40 @@
 lear();%0A        
+    
 for
+ 
 (Integer count =
@@ -1439,32 +1439,33 @@
 size(); count++)
+ 
 %7B%0A            Ca
@@ -1462,16 +1462,20 @@
         
+  
+  
 Calendar
@@ -1539,18 +1539,23 @@
         
+    
 if
+ 
 (currCal
@@ -1575,16 +1575,17 @@
 l) %3C= 0)
+ 
 %7B%0A      
@@ -1594,16 +1594,20 @@
         
+   
+ 
 ArrayLis
@@ -1677,35 +1677,40 @@
                 
+    
 for
+ 
 (Integer cnt = 0
@@ -1736,26 +1736,31 @@
 ze(); cnt++)
+ 
 %7B%0A
+    
             
@@ -1769,16 +1769,17 @@
       if
+ 
 (schedul
@@ -1822,18 +1822,23 @@
 F_WEEK))
+ 
 %7B%0A
+    
         
@@ -1934,15 +1934,45 @@
     
+    
 break;%0A
+                        %7D%0A
     

add option for empty all habit types list when calculating habits for today