package babysteps.model;

import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.stream.Collectors;

public class TaskModel {
    
    /**
     * Top of the task B-tree. All subtasks should be contained within this instance.
     */
    private Task currentTask;
    
    /**
     * Constructor.
     */
    public TaskModel() {
        currentTask = new Task("Tasks");
    }
    
    /**
     * Provide all of the subtask titles.
     * 
     * @return a List of strings
     */
    public List<String> subtaskNames() {
        return currentTask.getSubtasks().stream()
                .map(Task::getTitle)
                .collect(Collectors.toList());
    }
    
    /**
     * Return the head of the task tree.
     * 
     * @return a Task instance
     */
    public Task getCurrentTask() {
        return currentTask;
    }
    
    /**
     * Add a child to the current task.
     * 
     * @param title
     */
    public void createTask(String title) {
        currentTask.getSubtasks().add(new Task(title, currentTask));
    }
    
    /**
     * Move down the tree to a specific task.
     * 
     * @param taskNumber
     */
    public void enterTask(int taskNumber) {
        currentTask = currentTask.getSubtasks().get(taskNumber);
    }
    
    /**
     * Go to the task above this one in the task tree.
     */
    public void moveUp() {
        currentTask.getParent().ifPresent(t -> currentTask = t);
    }
    
    /**
     * Find the top of the task tree. i.e. discover the most general task.
     * 
     * @return the parent task
     */
    private Task headTask() {
        Task place = currentTask;
        while(place.getParent().isPresent()) {
            place = place.getParent().get();
        }
        return place;
    }
    
    /**
     * Save all tasks (including parents) to the given file.
     * 
     * @param file
     * @return true if successful, false otherwise
     */
    public boolean serialize(String path) {
        try {
            FileOutputStream fileOut = new FileOutputStream(path);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(headTask());
            out.close();
            fileOut.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * Overwrite the entire model with a new task tree. The previous task tree is not saved.
     * 
     * @param file
     * @return true if successful, false otherwise
     */
    public boolean deseriablize(String path) {
        try {
            FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            currentTask = (Task) in.readObject();
            in.close();
            fileIn.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
}

@@ -42,16 +42,37 @@
 l.List;%0A
+import java.io.File;%0A
 import j
@@ -448,54 +448,469 @@
   * 
-Constructor.%0A     */%0A    public TaskModel() %7B%0A
+Name of the file used for serialization.%0A     * TODO Let the user manipulate this value through JFileChooser.%0A     */%0A    private static final String SAVE_PATH = %22.tasksave%22;%0A    %0A    /**%0A     * Constructor. If a backup exists, load it. Otherwise instantiate new task tree.%0A     */%0A    public TaskModel() %7B%0A        File saveFile = new File(SAVE_PATH);%0A        if(saveFile.exists() && !saveFile.isDirectory()) %7B %0A            deseriablize();%0A        %7D else %7B%0A    
     
@@ -946,16 +946,26 @@
 asks%22);%0A
+        %7D%0A
     %7D%0A  
@@ -2404,17 +2404,9 @@
  to 
-the given
+a
  fil
@@ -2416,35 +2416,16 @@
      * %0A
-     * @param file%0A
      * @
@@ -2500,35 +2500,24 @@
 n serialize(
-String path
 ) %7B%0A        
@@ -2574,36 +2574,41 @@
 ileOutputStream(
-path
+SAVE_PATH
 );%0A            O
@@ -2660,24 +2660,60 @@
 m(fileOut);%0A
+            Task head = headTask();%0A
             
@@ -2732,22 +2732,16 @@
 ect(head
-Task()
 );%0A     
@@ -3041,27 +3041,8 @@
  * %0A
-     * @param file%0A
     
@@ -3132,19 +3132,8 @@
 ize(
-String path
 ) %7B%0A
@@ -3207,12 +3207,17 @@
 eam(
-path
+SAVE_PATH
 );%0A 

Make the serialization location a constant (temporarily)