/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.gnd.persistence.geojson;

import com.google.android.gms.maps.model.LatLng;
import com.google.common.collect.ImmutableList;
import javax.annotation.Nullable;
import org.json.JSONArray;

class GeoJsonExtent {

  private final GeoJsonGeometry geometry;

  @Nullable
  GeoJsonExtent(GeoJsonGeometry geometry) {
    this.geometry = geometry;
  }

  ImmutableList<LatLng> getVertices() {
    if (geometry.getVertices().isEmpty()) {
      return ImmutableList.of();
    }

    JSONArray g = geometry.getVertices().map(j -> j.optJSONArray(0)).orElse(null);

    if (g == null) {
      return ImmutableList.of();
    }

    JSONArray sw = g.optJSONArray(0);
    JSONArray ne = g.optJSONArray(2);

    if (sw == null || ne == null) {
      return ImmutableList.of();
    }

    double south = sw.optDouble(1, 0.0);
    double west = sw.optDouble(0, 0.0);
    double north = ne.optDouble(1, 0.0);
    double east = ne.optDouble(0, 0.0);

    return ImmutableList.of(
        new LatLng(south, west),
        new LatLng(north, east),
        new LatLng(south, east),
        new LatLng(north, west));
  }
}

@@ -642,16 +642,153 @@
 ojson;%0A%0A
+import static com.google.android.gnd.util.ImmutableListCollector.toImmutableList;%0Aimport static java8.util.stream.StreamSupport.stream;%0A%0A
 import c
@@ -872,24 +872,103 @@
 utableList;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java8.util.Optional;%0A
 import javax
@@ -1222,102 +1222,39 @@
     
-if (geometry.getVertices().isEmpty()) %7B%0A      return ImmutableList.of();%0A    %7D%0A%0A    JSONArray 
+Optional%3CJSONArray%3E exteriorRin
 g = 
@@ -1303,16 +1303,67 @@
 rray(0))
+;%0A%0A    return ringCoordinatesToLatLngs(exteriorRing
 .orElse(
@@ -1371,306 +1371,332 @@
 ull)
+)
 ;%0A
-%0A    if (g == null) %7B%0A      return ImmutableList.of();%0A    %7D%0A%0A    JSONArray sw = g.optJSONArray(0);%0A    JSONArray ne = g.optJSONArray(2);%0A%0A    if (sw == null %7C%7C ne == null) %7B%0A      return ImmutableList.of();%0A    %7D%0A%0A    double south = sw.optDouble(1, 0.0);%0A    double west = sw.optDouble(0, 0.0
+  %7D%0A%0A  private ImmutableList%3CLatLng%3E ringCoordinatesToLatLngs(JSONArray exteriorRing) %7B%0A    if (exteriorRing == null) %7B%0A      return ImmutableList.of();%0A    %7D%0A%0A    List%3CLatLng%3E coordinates = new ArrayList%3C%3E();%0A%0A    for (int i = 0; i %3C exteriorRing.length(); i++) %7B%0A      JSONArray point = exteriorRing.optJSONArray(i
 );%0A
+  
     
@@ -1706,18 +1706,19 @@
 ble 
-north = ne
+lat = point
 .opt
@@ -1741,24 +1741,28 @@
     
+  
 double 
-east = ne
+lng = point
 .opt
@@ -1786,162 +1786,111 @@
     
-return ImmutableList.of(%0A        new LatLng(south, west),
+  coordinates.add(new LatLng(lat, lng));
 %0A    
+%7D%0A%0A
     
-new LatLng(north, east),%0A        new LatLng(south, east),%0A        new LatLng(north, we
+return stream(coordinates).collect(toImmutableLi
 st
+(
 ));%0A

Improve semantic accuracy of GeoJsonExtents