package cz.iim.navsysclient.api;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.iim.navsysclient.internal.Constants;
import cz.iim.navsysclient.entities.Location;


public class ResponseParser {

    private ResponseParser() {
    }

    public static List<Location> parseDestinationsResponse(String responseBody) {
        List<Location> locations = new ArrayList<>();

        try {
            JSONArray jsonArray = new JSONArray(responseBody);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject destinationJSON = jsonArray.getJSONObject(i);
                String id = destinationJSON.getString(Constants.NAVSYS_LOCATION_ID_KEY);
                String name = destinationJSON.getString(Constants.NAVSYS_LOCATION_ID_NAME);

                locations.add(new Location(id, name));
            }
            return locations;

        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Location parseTrackResponse(String responseBody) {
        try {
            JSONObject json = new JSONObject(responseBody);
            String id = json.getString("location");
            String name = json.getString("name");

            return new Location(id, name);

        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static RegisterResponse parseRegisterResponse(String responseBody) {
        try {
            JSONObject json = new JSONObject(responseBody);
            String locationId = json.getString("location");
            String name = json.getString("name");
            String color = json.getString("color");
            return new RegisterResponse(new Location(locationId, name), color);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }
}

@@ -473,32 +473,181 @@
 %0A%0A        try %7B%0A
+            JSONObject json = new JSONObject(responseBody);%0A            boolean success = json.getBoolean(%22success%22);%0A%0A            if(success) %7B%0A    
             JSON
@@ -664,20 +664,24 @@
 Array = 
-new 
+json.get
 JSONArra
@@ -682,31 +682,32 @@
 ONArray(
-responseBody);%0A
+%22payload%22);%0A    
         
@@ -765,32 +765,36 @@
                 
+    
 JSONObject desti
@@ -834,16 +834,20 @@
 ect(i);%0A
+    
         
@@ -935,32 +935,36 @@
                 
+    
 String name = de
@@ -1040,16 +1040,20 @@
         
+    
 location
@@ -1091,26 +1091,34 @@
             
+ 
-%7D%0A
+   %7D%0A    
             
@@ -1135,16 +1135,79 @@
 ations;%0A
+            %7D else %7B%0A                return null;%0A            %7D
 %0A       
@@ -1445,32 +1445,120 @@
 (responseBody);%0A
+            boolean success = json.getBoolean(%22success%22);%0A            if(success) %7B%0A    
             Stri
@@ -1597,32 +1597,36 @@
 %22);%0A            
+    
 String name = js
@@ -1652,32 +1652,36 @@
 );%0A%0A            
+   
+ 
 return new Locat
@@ -1695,16 +1695,79 @@
  name);%0A
+            %7D else %7B%0A                return null;%0A            %7D
 %0A       
@@ -2016,32 +2016,120 @@
 (responseBody);%0A
+            boolean success = json.getBoolean(%22success%22);%0A            if(success) %7B%0A    
             Stri
@@ -2176,32 +2176,36 @@
 %22);%0A            
+   
+ 
 String name = js
@@ -2222,24 +2222,28 @@
 ng(%22name%22);%0A
+    
             
@@ -2286,32 +2286,36 @@
 %22);%0A            
+    
 return new Regis
@@ -2366,16 +2366,80 @@
 color);%0A
+            %7D else %7B%0A                return null;%0A            %7D%0A
         

Check for success in responses