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 {
            JSONObject json = new JSONObject(responseBody);
            boolean success = json.getBoolean("success");

            if(success) {
                JSONArray jsonArray = json.getJSONArray("payload");
                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;
            } else {
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static TrackResponse parseTrackResponse(String responseBody) {
        try {
            JSONObject json = new JSONObject(responseBody);
            boolean success = json.getBoolean("success");
            if(success) {
                String id = json.getString("location");
                String name = json.getString("name");
                boolean finished = json.getBoolean("finished");

                return new TrackResponse(new Location(id, name), finished);
            } else {
                return null;
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

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

@@ -1686,16 +1686,96 @@
 nished =
+ false;%0A                if(json.has(%22finished%22))%7B%0A                    finished =
  json.ge
@@ -1795,16 +1795,34 @@
 ished%22);
+%0A                %7D
 %0A%0A      

Fix track response parsing