/*
 * 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.remote.firestore.schema;

import static com.google.android.gnd.util.Localization.getLocalizedMessage;

import com.google.android.gnd.model.layer.Layer;
import java8.util.Maps;

/** Converts between Firestore documents and {@link Layer} instances. */
class LayerConverter {

  static Layer toLayer(String id, LayerNestedObject obj) {
    Layer.Builder layer = Layer.newBuilder();
    layer
        .setId(id)
        .setListHeading(getLocalizedMessage(obj.getListHeading()))
        .setItemLabel(getLocalizedMessage(obj.getItemLabel()));
    if (obj.getDefaultStyle() != null) {
      layer.setDefaultStyle(StyleConverter.toStyle(obj.getDefaultStyle()));
    }
    if (obj.getForms() != null) {
      Maps.forEach(
          obj.getForms(),
          (formId, formDoc) -> layer.addForm(FormConverter.toForm(formId, formDoc)));
    }
    return layer.build();
  }
}

@@ -735,16 +735,41 @@
 ssage;%0A%0A
+import android.util.Log;%0A
 import c
@@ -812,32 +812,8 @@
 yer;
-%0Aimport java8.util.Maps;
 %0A%0A/*
@@ -907,16 +907,91 @@
 rter %7B%0A%0A
+  private static final String TAG = LayerConverter.class.getSimpleName();%0A%0A
   static
@@ -1404,98 +1404,220 @@
 null
-) %7B%0A      Maps.forEach(%0A          obj.getForms(),
+ && !obj.getForms().isEmpty()) %7B%0A      if (obj.getForms().size() %3E 1) %7B%0A        Log.w(TAG, %22Multiple forms not supported%22);
 %0A      
+%7D%0A
     
-(formId, formDoc) -%3E
+  String formId = obj.getForms().keySet().iterator().next();%0A     
  layer.
-add
+set
 Form
@@ -1650,15 +1650,33 @@
 Id, 
-formDoc
+obj.getForms().get(formId
 )));

Update to latest Layer model, warn on multiple forms