package controller;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import model.PlayerModel;
import model.PlayerProgress;
import boundary.PlayerApplication;

/**
 * Loads the player's progress from the progress file.
 *
 * Unlike the majority of controllers in sixes-wild, users will not directly
 * invoke this controller. Instead, the PlayerApplication will automatically
 * create and run this controller on startup, to load the player progress.
 *
 * Despite this difference from most controllers, this controller still receives
 * the top-level boundary, in the event that a saving icon is included in the
 * future.
 *
 * @author Eli Skeggs
 */
public class PlayerLoadProgressCtrl {
  public static final File progressdir = new File("resource/progress");
  public static final File progressfile = new File("resource/progress/progress");

  PlayerApplication app;
  PlayerModel model;

  /**
   * Create a PlayerLoadProgressCtrl.
   *
   * @param app
   * @param model
   */
  public PlayerLoadProgressCtrl(PlayerApplication app, PlayerModel model) {
    this.app = app;
    this.model = model;
  }

  /**
   * This method loads the player's progress from a file. If the file does not
   * exist, it creates a new file with no player progress. If the file is
   * corrupted, it creates a backup, and then overwrites the original file with
   * an empty user progress file.
   *
   * TODO: clean up control flow.
   */
  public void loadProgress() {
    if (!progressdir.mkdirs() && !progressdir.isDirectory()) {
      // TODO: dialog?
      System.err.println("Unable to create the appropriate directory structure,"
        + "your progress will not be saved");
      model.progress = new PlayerProgress();
      return;
    }

    // no progress file, write a new one
    if (!progressfile.isFile()) {
      model.progress = new PlayerProgress();
      DataOutputStream out = null;
      try {
        out = new DataOutputStream(new FileOutputStream(progressfile));
        model.progress.write(out);
      } catch (IOException err) {
        System.err.println("Unable to save progress");
        System.err.println(err.getMessage());
        err.printStackTrace();
      } finally {
        try {
          if (out != null) {
            out.close();
          }
        } catch (IOException err) {
          System.err.println(err.getMessage());
          err.printStackTrace();
        }
      }
      return;
    }

    // read the existing file
    try {
      model.progress = new PlayerProgress(new DataInputStream(
        new FileInputStream(progressfile)));
    } catch (IOException err) {
      // TODO: dialog?
      System.err.println("Unable to load progress, your progress will not be" +
        "saved");
      model.progress = new PlayerProgress();
      return;
    }
  }
}

@@ -25,180 +25,56 @@
 ort 
-java.io.DataInputStream;%0Aimport java.io.DataOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0A
+boundary.PlayerApplication;%0Aimport java.io.File;
 %0Aimp
@@ -136,34 +136,54 @@
 ort 
-boundary.PlayerApplication
+utils.ReadStream;%0Aimport utils.StreamFileUtils
 ;%0A%0A/
@@ -1421,30 +1421,37 @@
 DO: 
-clean up control flow.
+should we have alert dialogs?
 %0A   
@@ -1496,213 +1496,264 @@
 if (
-!progressdir.mkdirs() && !progressdir.isDirectory()) %7B%0A      // TODO: dialog?%0A      System.err.println(%22Unable to create the appropriate directory structure,%22%0A        + %22your progress will not be saved%22);%0A
+StreamFileUtils.ensureParent(progressfile)) %7B%0A      if (progressfile.isFile()) %7B%0A        ReadStream readStream = PlayerProgress.getReadable();%0A        Object result = StreamFileUtils.readStream(progressfile, readStream);%0A%0A        if (result == null) %7B%0A    
     
@@ -1797,245 +1797,219 @@
 ();%0A
+%0A
       
-return;%0A    %7D%0A%0A    // no progress file, write a new one%0A    if (!progressfile.isFile()) %7B%0A      model.progress = new PlayerProgress();%0A      DataOutputStream out = null;%0A      try %7B%0A        out = new DataOutputStream(new FileOutput
+    File backup = new File(%22resource/progress/progress-%22 +%0A            System.currentTimeMillis() + %22.bak%22);%0A          if (StreamFileUtils.backup(progressfile, backup)) %7B%0A            StreamFileUtils.write
 Stre
@@ -2023,27 +2023,17 @@
 ressfile
-));%0A       
+,
  model.p
@@ -2043,55 +2043,15 @@
 ress
-.write(out);%0A      %7D catch (IOException err) %7B%0A
+);%0A    
     
@@ -2084,20 +2084,20 @@
 able to 
-save
+load
  progres
@@ -2101,195 +2101,207 @@
 ress
-%22);%0A        System.err.println(err.getMessage());%0A        err.printStackTrace();%0A      %7D finally %7B%0A        try %7B%0A          if (out != null) %7B%0A            out.close();%0A    
+, your existing progress%22%0A              + %22 file has been backed up and overwritten%22);%0A          %7D else %7B%0A            System.err.println(%22Unable to load progress, your progress will %22 +%0A
      
- %7D%0A
         
 %7D ca
@@ -2300,116 +2300,79 @@
     
-%7D catch (IOException err) %7B%0A          System.err.println(err.getMessage());%0A          err.printStackTrace();
+ %22not be saved%22);%0A            model.disableProgress = true;%0A          %7D
 %0A   
@@ -2381,78 +2381,95 @@
    %7D
+ else %7B
 %0A      
-%7D%0A
     
-  return;%0A
+model.progress = (PlayerProgress) result;%0A  
     
+  
 %7D
-%0A
 %0A    
-// read the existing file%0A    try %7B%0A
+  %7D else %7B%0A  
     
@@ -2510,179 +2510,191 @@
 ess(
-new DataInputStream(%0A        new FileInputStream(progressfile)));%0A    %7D catch (IOException err) %7B%0A      // TODO: dialog?%0A      System.err.println(%22Unable to load progress,
+);%0A        StreamFileUtils.writeStream(progressfile, model.progress);%0A      %7D%0A    %7D else %7B%0A      System.err.println(%22Unable to create the appropriate directory structure,%22%0A        + %22
  you
@@ -2719,21 +2719,9 @@
 t be
-%22 +%0A        %22
+ 
 save
@@ -2774,22 +2774,8 @@
 ();%0A
-      return;%0A
     

Add missing load progess changes for c1f35f8