/*
 * Copyright 2019 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.local.room;

import androidx.annotation.NonNull;
import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
import com.google.android.gnd.persistence.shared.FeatureMutation;

/**
 * Defines how Room persists features in the local db. By default, Room uses the name of object
 * fields and their respective types to determine database column names and types.
 */
@Entity(
    tableName = "feature",
    indices = {@Index("id")})
public class FeatureEntity {

  @NonNull @PrimaryKey public String id;

  // TODO: Rename to DeletionState.
  @NonNull public EntityState state;

  @NonNull public String projectId;

  @NonNull @Embedded public Coordinates location;

  @NonNull
  static FeatureEntity fromMutation(FeatureMutation m) {
    FeatureEntity fe = new FeatureEntity();
    fe.id = m.getFeatureId();
    fe.projectId = m.getProjectId();
    fe.state = EntityState.DEFAULT;
    m.getNewLocation().ifPresent(l -> fe.location = Coordinates.fromPoint(l));
    return fe;
  }
}

@@ -867,16 +867,112 @@
 utation;
+%0Aimport com.google.auto.value.AutoValue;%0Aimport com.google.auto.value.AutoValue.CopyAnnotations;
 %0A%0A/**%0A *
@@ -1152,16 +1152,27 @@
 es.%0A */%0A
+@AutoValue%0A
 @Entity(
@@ -1236,16 +1236,25 @@
 %0Apublic 
+abstract 
 class Fe
@@ -1267,16 +1267,34 @@
 ntity %7B%0A
+  @CopyAnnotations
 %0A  @NonN
@@ -1296,16 +1296,18 @@
 @NonNull
+%0A 
  @Primar
@@ -1310,24 +1310,26 @@
 imaryKey
+%0A 
  public 
 String i
@@ -1324,17 +1324,31 @@
 lic 
-String id
+abstract String getId()
 ;%0A%0A 
@@ -1382,16 +1382,35 @@
 nState.%0A
+  @CopyAnnotations%0A
   @NonNu
@@ -1411,24 +1411,26 @@
 @NonNull
+%0A 
  public 
 EntitySt
@@ -1421,16 +1421,25 @@
  public 
+abstract 
 EntitySt
@@ -1446,16 +1446,40 @@
 ate 
-s
+getS
 tate
+()
 ;%0A%0A
+  @CopyAnnotations%0A
   @N
@@ -1484,24 +1484,26 @@
 @NonNull
+%0A 
  public 
 String p
@@ -1498,24 +1498,36 @@
 lic 
+abstract 
 String 
-p
+getP
 rojectId
 ;%0A%0A 
@@ -1522,19 +1522,40 @@
 rojectId
+()
 ;%0A%0A
+  @CopyAnnotations%0A
   @NonNu
@@ -1556,16 +1556,18 @@
 @NonNull
+%0A 
  @Embedd
@@ -1568,24 +1568,26 @@
 Embedded
+%0A 
  public 
 Coordina
@@ -1578,16 +1578,25 @@
  public 
+abstract 
 Coordina
@@ -1599,24 +1599,29 @@
 dinates 
-l
+getL
 ocation
+()
 ;%0A%0A  @No
@@ -1679,16 +1679,23 @@
 tation m
+utation
 ) %7B%0A    
@@ -1711,17 +1711,33 @@
 tity
- fe = new
+.Builder entity =%0A       
  Fea
@@ -1750,25 +1750,46 @@
 tity
+.builder
 ()
-;
 %0A    
-fe.id = m
+        .setId(mutation
 .get
@@ -1803,30 +1803,44 @@
 Id()
-;
+)
 %0A    
-fe.projectId = m
+        .setProjectId(mutation
 .get
@@ -1854,25 +1854,32 @@
 Id()
-;
+)
 %0A    
-fe.s
+        .setS
 tate
- = 
+(
 Enti
@@ -1897,15 +1897,23 @@
 AULT
+)
 ;%0A    m
+utation
 .get
@@ -1930,77 +1930,839 @@
 n().
-ifPresent(l -%3E fe.location = Coordinates.fromPoint(l));%0A    return fe
+map(Coordinates::fromPoint).ifPresent(entity::setLocation);%0A    return entity.build();%0A  %7D%0A%0A  // Auto-generated boilerplate.%0A%0A  public static FeatureEntity create(%0A      String id, EntityState state, String projectId, Coordinates location) %7B%0A    return builder()%0A        .setId(id)%0A        .setState(state)%0A        .setProjectId(projectId)%0A        .setLocation(location)%0A        .build();%0A  %7D%0A%0A  public static Builder builder() %7B%0A    return new AutoValue_FeatureEntity.Builder();%0A  %7D%0A%0A  @AutoValue.Builder%0A  public abstract static class Builder %7B%0A%0A    public abstract Builder setId(String newId);%0A%0A    public abstract Builder setState(EntityState newState);%0A%0A    public abstract Builder setProjectId(String newProjectId);%0A%0A    public abstract Builder setLocation(Coordinates newLocation);%0A%0A    public abstract FeatureEntity build()
 ;%0A  

Reimplement FeatureEntity as AutoValue