package com.example.habitrack;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by sshussai on 11/29/17.
 */

/**
 * Steps to add a new object:
 * 1. Declare a base object
 * 2. Declare a public final MODE value
 * 2. Declare a private final FILENAME value
 * 3. Add the if case to the save function. Modify as needed
 * 4. Add the if case to the load function. Modify as needed
 */

public class FileManager {

    private Context ctx;

    // 1. Base objects
    private ArrayList<HabitTypeMetadata> habitTypeMetadata;
    private ArrayList<HabitEvent> recentHabitEvents;

    // 2. MODES
    public final Integer HT_METADATA_MODE = 100;
    public final Integer RECENT_HE_MODE = 200;

    // 3. FILENAMES
    private String filename;
    private final String HT_METADATA_FILE = "htmetadata.sav";
    private final String RECENT_HE_FILE = "recenthabitevents.sav";

    // Constructor
    public FileManager(Context context) {
        this.ctx = context;
    }

    public void save(Integer mode){
        // 4. If cases for the save function
        if(mode == HT_METADATA_MODE){
            habitTypeMetadata = HabitTypeStateManager.getHTStateManager().getAllMetadata();
            filename = HT_METADATA_FILE;
        } else if(mode == RECENT_HE_MODE){
            recentHabitEvents = HabitEventStateManager.getHEStateManager().getRecentHabitevents();
            filename = RECENT_HE_FILE;
        }
        try {
            FileOutputStream fos = ctx.openFileOutput(filename,0);
            OutputStreamWriter writer = new OutputStreamWriter(fos);
            Gson gson = new Gson();
            if(mode == HT_METADATA_MODE) {
                gson.toJson(habitTypeMetadata, writer);
            } else if (mode == RECENT_HE_MODE){
                gson.toJson(recentHabitEvents, writer);
            }
            writer.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }
    }

    public void load(Integer mode) {
        // 5. If cases for load function
        if(mode == HT_METADATA_MODE){
            filename = HT_METADATA_FILE;
        } else if(mode == RECENT_HE_MODE){
            filename = RECENT_HE_FILE;
        }
        try {
            FileInputStream fis = ctx.openFileInput(filename);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            Gson gson = new Gson();
            //Code taken from http://stackoverflow.com/questions/12384064/gson-convert-from-json-to-a-typed-arraylistt Sept.22,2016
            if(mode == HT_METADATA_MODE) {
                Type calType = new TypeToken<ArrayList<HabitTypeMetadata>>() {}.getType();
                habitTypeMetadata = gson.fromJson(in, calType);
            } else if (mode == RECENT_HE_MODE){
                Type calType = new TypeToken<ArrayList<HabitEvent>>() {}.getType();
                recentHabitEvents = gson.fromJson(in, calType);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            if(mode == HT_METADATA_MODE){
                habitTypeMetadata = new ArrayList<HabitTypeMetadata>();
            } else if(mode == RECENT_HE_MODE){
                recentHabitEvents = new ArrayList<HabitEvent>();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException();
        }
        if(mode == HT_METADATA_MODE){
            HabitTypeStateManager.getHTStateManager().setHtMetadata(habitTypeMetadata);
        } else if(mode == RECENT_HE_MODE){
            HabitEventStateManager.getHEStateManager().setRecentHabitEvents(recentHabitEvents);
        }
    }

}


@@ -956,16 +956,68 @@
 tEvents;
+%0A    private ArrayList%3CHabitEvent%3E todayHabitEvents;
 %0A%0A    //
@@ -1121,16 +1121,62 @@
 E = 200;
+%0A    public final Integer TODAY_HE_MODE = 300;
 %0A%0A    //
@@ -1346,16 +1346,81 @@
 ts.sav%22;
+%0A    private final String TODAY_HE_FILE = %22todayhabitevents.sav%22;
 %0A%0A    //
@@ -1934,32 +1934,171 @@
 RECENT_HE_FILE;%0A
+        %7D else if(mode == TODAY_HE_MODE)%7B%0A            todayHabitEvents = HabitEventStateManager.getHEStateManager().getTodayHabitevents();%0A
         %7D%0A      
@@ -2472,32 +2472,133 @@
 vents, writer);%0A
+            %7D else if(mode == TODAY_HE_MODE)%7B%0A                gson.toJson(todayHabitEvents, writer);%0A
             %7D%0A  
@@ -3127,32 +3127,112 @@
 RECENT_HE_FILE;%0A
+        %7D else if(mode == TODAY_HE_MODE)%7B%0A            filename = TODAY_HE_FILE;%0A
         %7D%0A      
@@ -3936,32 +3936,226 @@
 n(in, calType);%0A
+            %7D else if (mode == TODAY_HE_MODE)%7B%0A                Type calType = new TypeToken%3CArrayList%3CHabitEvent%3E%3E() %7B%7D.getType();%0A                todayHabitEvents = gson.fromJson(in, calType);%0A
             %7D%0A  
@@ -4465,24 +4465,134 @@
 itEvent%3E();%0A
+            %7D else if(mode == TODAY_HE_MODE)%7B%0A                todayHabitEvents = new ArrayList%3CHabitEvent%3E();%0A
             
@@ -4887,32 +4887,32 @@
 ECENT_HE_MODE)%7B%0A
-
             Habi
@@ -4987,24 +4987,160 @@
 bitEvents);%0A
+        %7D else if(mode == TODAY_HE_MODE)%7B%0A            HabitEventStateManager.getHEStateManager().setTodayHabitEvents(todayHabitEvents);%0A
         %7D%0A  

add local load/save mode for today's habit events