package floobits;

import java.io.*;
import java.lang.reflect.Type;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.FileUtils;


class Workspace implements Serializable {
    String url;
    String path;

    Workspace () {

    }

    Workspace(String url, String path) {
        this.url = url;
        this.path = path;
    }

    public void clean() {
      if (!this.url.endsWith("/")) {
          this.url += "/";
      }
    }
}

public class PersistentJson {
    public HashMap<String, Map<String, Workspace>> workspaces = new HashMap<String, Map<String,Workspace>>();
    public Boolean auto_generated_account = false;
    public Boolean disable_account_creation = true;
    public LinkedList<Workspace> recent_workspaces = new LinkedList<Workspace>();

    public void addWorkspace(FlooUrl flooUrl, String path) {
        Map<String, Workspace> workspaces = this.workspaces.get(flooUrl.owner);
        if (workspaces == null) {
            workspaces = new HashMap<String, Workspace>();
            this.workspaces.put(flooUrl.owner, workspaces);
        }
        Workspace workspace = workspaces.get(flooUrl.workspace);
        if (workspace == null) {
            workspace = new Workspace(flooUrl.toString(), path);
            workspaces.put(flooUrl.workspace, workspace);
        } else {
            workspace.path = path;
            workspace.url = flooUrl.toString();
        }
        this.recent_workspaces.add(workspace);
        HashSet<String> seen = new HashSet<String>();
        LinkedList<Workspace> unique = new LinkedList<Workspace>();
        for (Workspace w : this.recent_workspaces) {
            w.clean();
            if (seen.contains(w.url)) {
                continue;
            }
            seen.add(w.url);
            unique.add(w);
        }
        this.recent_workspaces = unique;
    }

    public void save ()  {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        try {
            FileUtils.write(getFile(), gson.toJson(this), "UTF-8");
        } catch (Exception e) {
            Flog.warn(e);
        }
    }

    public static File getFile() {
        return new File(FilenameUtils.concat(Shared.baseDir, "persistent.json"));
    }

    public static PersistentJson getInstance() {
        String s;
        try {
            s = FileUtils.readFileToString(getFile(), "UTF-8");
        } catch (Exception e) {
            Flog.warn(e);
            s = "{}";
        }
        return new Gson().fromJson(s, (Type) PersistentJson.class);
    }
}

@@ -2384,16 +2384,51 @@
 ring s;%0A
+        String defaultJSON = %22%7B%7D%22;%0A
         
@@ -2575,38 +2575,270 @@
 s = 
-%22%7B%7D%22;%0A        %7D%0A        return
+defaultJSON;%0A        %7D%0A        PersistentJson pj;%0A        try %7B%0A            pj = new Gson().fromJson(s, (Type) PersistentJson.class);%0A        %7D catch (com.google.gson.JsonSyntaxException e) %7B%0A            Flog.warn(%22Bad JSON in persistent json%22);%0A            pj =
  new
@@ -2846,33 +2846,43 @@
 Gson().fromJson(
-s
+defaultJSON
 , (Type) Persist
@@ -2893,20 +2893,49 @@
 son.class);%0A
+        %7D%0A        return pj;%0A
     %7D%0A%7D%0A

Stop throwing exceptions on bad persistent json.