@Override
  public byte[] retrieveImage(String photoId) throws IOException {

    JSONObject json = retrieveRawPhotos(photoId);
    JSONArray array = json.getJSONArray("data");

    boolean isBlessed = false;
    int index = 0;
    while (!isBlessed && index < array.length()) {

      JSONObject image = array.getJSONObject(index);

      if (image.getBoolean("is_blessed")) {
        isBlessed = true;
      } else {

        index++;
      }
    }

    if (!isBlessed) {

      index = 0;
    }

    JSONObject image = array.getJSONObject(index);
    String url = image.getJSONObject("images").getJSONObject("large").getString("url");

    return restProvider.retrieveRawImage(url);
  }
  private List<Place> retrievePlaces(String locationID, String type) throws IOException {

    StringBuilder urlBuilder = new StringBuilder(urlList);
    urlBuilder.append(locationID);
    urlBuilder.append("/");
    urlBuilder.append(type);
    urlBuilder.append("?lang=");
    urlBuilder.append(language);
    urlBuilder.append("&key=");
    urlBuilder.append(apiKey);

    String urlString = urlBuilder.toString();

    String genreJson = restProvider.retrieveRawInformation(urlString);
    JSONObject json = new JSONObject(genreJson);

    JSONArray data = json.getJSONArray("data");

    List<Place> result = new ArrayList<>(data.length());
    for (int i = 0; i < data.length(); i++) {

      Place place = buildPlace(data.getJSONObject(i));
      completePlace(place);
      result.add(place);
    }

    return result;
  }
  private String retrieveLocationID(Coordinates coord) throws IOException {

    StringBuilder urlBuilder = new StringBuilder(urlMapping);
    urlBuilder.append(coord.getLat());
    urlBuilder.append(",");
    urlBuilder.append(coord.getLon());
    urlBuilder.append("?lang=");
    urlBuilder.append(language);
    urlBuilder.append("&key=");
    urlBuilder.append(apiKey);

    String urlString = urlBuilder.toString();

    String genreJson = restProvider.retrieveRawInformation(urlString);
    JSONObject json = new JSONObject(genreJson);

    JSONArray ancestors = json.getJSONArray("data").getJSONObject(0).getJSONArray("ancestors");
    String locationID = null;

    boolean city = false;
    String cityLocation = "";

    boolean island = false;
    String islandLocation = "";

    for (int i = 0; i < ancestors.length() && !(city && island); i++) {

      JSONObject ancestor = ancestors.getJSONObject(i);
      if (ancestor.getString("level").equals("City")
          || ancestor.getString("level").equals("Municipality")) {

        city = true;
        cityLocation = ancestor.getString("location_id");
      } else if (ancestor.getString("level").equals("Island")) {

        island = true;
        islandLocation = ancestor.getString("location_id");
      }
    }

    if (island) {

      locationID = islandLocation;
    } else if (city) {

      locationID = cityLocation;
    }

    return locationID;
  }
  private JSONObject retrieveRawPhotos(String locationID) throws IOException {

    StringBuilder urlBuilder = new StringBuilder(urlList);
    urlBuilder.append(locationID);
    urlBuilder.append("/photos");
    urlBuilder.append("?lang=");
    urlBuilder.append(language);
    urlBuilder.append("&key=");

    urlBuilder.append(apiKey);

    String urlString = urlBuilder.toString();

    String genreJson = restProvider.retrieveRawInformation(urlString);
    JSONObject json = new JSONObject(genreJson);

    return json;
  }