package uk.ac.ic.wlgitbridge.snapshot.getdoc;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import uk.ac.ic.wlgitbridge.git.exception.GitUserException;
import uk.ac.ic.wlgitbridge.git.exception.SnapshotAPIException;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.base.Result;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.snapshot.getsavedvers.WLUser;
import uk.ac.ic.wlgitbridge.util.Log;

/**
 * Created by Winston on 06/11/14.
 */
public class GetDocResult extends Result {

    private int error;
    private int versionID;
    private String createdAt;
    private WLUser user;

    private SnapshotAPIException exception;
    private ForbiddenException forbidden;

    public GetDocResult(
            Request request,
            JsonElement json
    ) throws FailedConnectionException {
        super(request, json);
    }

    public GetDocResult(
            JsonElement error,
            int versionID,
            String createdAt,
            String email,
            String name
    ) {
        if (error == null) {
            this.error = -1;
        } else {
            this.error = error.getAsInt();
        }
        this.versionID = versionID;
        this.createdAt = createdAt;
        this.user = new WLUser(name, email);
    }

    @Override
    public JsonElement toJson() {
        JsonObject jsonThis = new JsonObject();
        if (error == -1) {
            jsonThis.addProperty("latestVerId", versionID);
            jsonThis.addProperty("latestVerAt", createdAt);
            JsonObject latestVerBy = new JsonObject();
            latestVerBy.addProperty("email", getEmail());
            latestVerBy.addProperty("name", getName());
            jsonThis.add("latestVerBy", latestVerBy);
        } else {
            jsonThis.addProperty("status", error);
            String message;
            if (error == 403) {
                message = "Forbidden";
            } else {
                message = "Not Found";
            }
            jsonThis.addProperty("message", message);
        }
        return jsonThis;
    }

    @Override
    public void fromJSON(JsonElement json) {
        Log.info("GetDocResult: " + json);
        JsonObject jsonObject = json.getAsJsonObject();
        if (jsonObject.has("status")) {
            switch (jsonObject.get("status").getAsInt()) {
            case 401:
            case 403:
                forbidden = new ForbiddenException();
                break;
            case 404:
                exception = new InvalidProjectException();
                break;
            default:
                throw new IllegalArgumentException(
                        "unknown get doc error code"
                );
            }
        } else {
            versionID = jsonObject.get("latestVerId").getAsInt();
            createdAt = jsonObject.get("latestVerAt").getAsString();
            String name = null;
            String email = null;
            JsonElement latestVerBy = jsonObject.get("latestVerBy");

            if (latestVerBy.isJsonObject()) {
                JsonObject userObject = latestVerBy.getAsJsonObject();
                name = userObject.get("name").getAsString();
                email = userObject.get("email").getAsString();
            }

            user = new WLUser(name, email);
        }
    }

    public int getVersionID() throws GitUserException {
        if (exception != null) {
            throw exception;
        }
        return versionID;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public String getName() {
        return user.getName();
    }

    public String getEmail() {
        return user.getEmail();
    }

}

@@ -781,24 +781,59 @@
  versionID;%0A
+    private String migratedFromID;%0A
     private 
@@ -846,24 +846,24 @@
  createdAt;%0A
-
     private 
@@ -1286,16 +1286,51 @@
 ing name
+,%0A            String migratedFromID
 %0A    ) %7B
@@ -1571,24 +1571,310 @@
 me, email);%0A
+        this.migratedFromID = migratedFromID;%0A    %7D%0A%0A    public GetDocResult(%0A            JsonElement error,%0A            int versionID,%0A            String createdAt,%0A            String email,%0A            String name%0A    ) %7B%0A        this(error, versionID, createdAt, email, name, null);%0A
     %7D%0A%0A    @
@@ -2334,16 +2334,144 @@
 VerBy);%0A
+            if (migratedFromID != null) %7B%0A                jsonThis.addProperty(%22migratedFromId%22, migratedFromID);%0A            %7D%0A
         
@@ -3584,32 +3584,239 @@
 .getAsString();%0A
+            if (jsonObject.has(%22migratedFromId%22)) %7B%0A                migratedFromID = jsonObject.get(%22migratedFromId%22).getAsString();%0A            %7D else %7B%0A                migratedFromID = null;%0A            %7D%0A
             Stri
@@ -4611,17 +4611,83 @@
 mail();%0A
-
     %7D%0A%0A
+    public String getMigratedFromID() %7B return migratedFromID; %7D%0A%0A
 %7D%0A

Add `migratedFromID` property to `GetDocResult`