package cx.cad.nfsn.net;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.logging.Logger;

public class APIResponse {

    private static final String PARSE_EXCEPTION_ERROR_JSONSTRING = "{\"error\":\"Failed to parse JSON response\"}";
    private String responseString;
    private Boolean status;
    private Object parsedObject;

    private final Logger LOGGER = Logger.getLogger(APIResponse.class.toString());

    public static Boolean SUCCESS = Boolean.TRUE;
    public static Boolean FAILURE = Boolean.FALSE;

    public APIResponse(String responseString) {
        this.responseString = responseString;
        this.status = SUCCESS;
    }

    public APIResponse(String responseString, Boolean status) {
        this(responseString);
        this.status = FAILURE;
    }

    public boolean getStatus(){
        return status;
    }

    public boolean isError(){
        return ((JSONObject)getObject()).containsKey("error");
    }

    public String getDebugMessage(){
          return (String) ((JSONObject)getObject()).get("debug");
    }
    public String getError(){
        return (String) ((JSONObject)getObject()).get("error");
    }

    public Object getObject() {
        if(parsedObject == null){
            try {
                parsedObject = parse(responseString);
            } catch (ParseException e) {
                LOGGER.severe(String.format("%s: Failed to parse JSON response: %s", e.getMessage(), responseString));
                try {
                    parsedObject = parse(PARSE_EXCEPTION_ERROR_JSONSTRING);
                } catch (ParseException e1) {
                    LOGGER.severe(String.format("%s: Failed to parse canned JSON error response: %s",
                            e.getMessage(), PARSE_EXCEPTION_ERROR_JSONSTRING));
                }
            }
        }
        return parsedObject;
    }

    public Double getDouble(){
        return (Double) getObject();
    }

    public String getString(){
        return (String) getObject();
    }

    private Object parse(String jsonString) throws ParseException {
        JSONParser parser = new JSONParser();
        return parser.parse(jsonString);
    }

    //TODO: determine if this is needed
    private JSONArray arrayFromJson() throws ParseException {
        JSONParser parser = new JSONParser();
        return (JSONArray) parser.parse(responseString);
    }

}

@@ -1012,32 +1012,23 @@
 urn 
-((JSONObject)get
+getJson
 Object()
 ).co
@@ -1023,17 +1023,16 @@
 Object()
-)
 .contain
@@ -1094,26 +1094,24 @@
 ()%7B%0A        
-  
 return (Stri
@@ -1118,32 +1118,23 @@
 ng) 
-((JSONObject)get
+getJson
 Object()
 ).ge
@@ -1125,25 +1125,24 @@
 JsonObject()
-)
 .get(%22debug%22
@@ -1208,32 +1208,23 @@
 ng) 
-((JSONObject)get
+getJson
 Object()
 ).ge
@@ -1219,17 +1219,16 @@
 Object()
-)
 .get(%22er
@@ -1935,32 +1935,119 @@
 dObject;%0A    %7D%0A%0A
+    public JSONObject getJsonObject()%7B%0A        return (JSONObject) getObject();%0A    %7D%0A%0A
     public Doubl
@@ -2346,218 +2346,6 @@
  %7D%0A%0A
-    //TODO: determine if this is needed%0A    private JSONArray arrayFromJson() throws ParseException %7B%0A        JSONParser parser = new JSONParser();%0A        return (JSONArray) parser.parse(responseString);%0A    %7D%0A%0A
 %7D%0A

add getJsonObject and use it where appropriate