/*
 * 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.ui.editobservation;

import android.content.Context;
import com.google.android.gnd.model.form.MultipleChoice;
import com.google.android.gnd.model.observation.Response;
import com.google.auto.value.AutoValue;
import java8.util.Optional;
import java8.util.function.Consumer;

@AutoValue
public abstract class SelectDialogFactory {

  public static Builder builder() {
    return new AutoValue_SelectDialogFactory.Builder();
  }

  public abstract Context getContext();

  public abstract String getTitle();

  public abstract MultipleChoice getMultipleChoice();

  public abstract Optional<Response> getCurrentValue();

  public abstract Consumer<Optional<Response>> getValueConsumer();

  @AutoValue.Builder
  public abstract static class Builder {

    public abstract Builder setContext(Context context);

    public abstract Builder setTitle(String title);

    public abstract Builder setMultipleChoice(MultipleChoice multipleChoice);

    public abstract Builder setCurrentValue(Optional<Response> response);

    public abstract Builder setValueConsumer(Consumer<Optional<Response>> consumer);

    public abstract SelectDialogFactory build();
  }
}

@@ -652,87 +652,421 @@
 ort 
-android.content.Context;%0Aimport com.google.android.gnd.model.form.MultipleChoic
+static com.google.android.gnd.util.ImmutableListCollector.toImmutableList;%0Aimport static java8.util.stream.StreamSupport.stream;%0A%0Aimport android.content.Context;%0Aimport androidx.appcompat.app.AlertDialog;%0Aimport com.google.android.gnd.R;%0Aimport com.google.android.gnd.model.form.MultipleChoice;%0Aimport com.google.android.gnd.model.form.Option;%0Aimport com.google.android.gnd.model.observation.MultipleChoiceRespons
 e;%0Ai
@@ -1144,28 +1144,36 @@
 gle.
-auto.value.AutoValue
+common.collect.ImmutableList
 ;%0Aim
@@ -1240,19 +1240,8 @@
 r;%0A%0A
-@AutoValue%0A
 publ
@@ -1288,100 +1288,1468 @@
 %0A  p
-ublic static Builder builder() %7B%0A    return new AutoValue_SelectDialogFactory.Builder
+rotected ImmutableList%3COption%3E getOptions() %7B%0A    return getMultipleChoice().getOptions();%0A  %7D%0A%0A  protected Option getOption(int index) %7B%0A    return getOptions().get(index);%0A  %7D%0A%0A  protected int size() %7B%0A    return getOptions().size();%0A  %7D%0A%0A  protected String%5B%5D getLabels() %7B%0A    return stream(getMultipleChoice().getOptions()).map(Option::getLabel).toArray(String%5B%5D::new);%0A  %7D%0A%0A  /** Returns the selected item(s) as a %7B@link Response%7D. */%0A  protected Optional%3CResponse%3E onCreateResponse() %7B%0A    return MultipleChoiceResponse.fromList(%0A        stream(getSelectedOptions()).map(Option::getId).collect(toImmutableList()));%0A  %7D%0A%0A  // TODO: Replace with modal bottom sheet.%0A  protected AlertDialog.Builder createDialogBuilder() %7B%0A    return new AlertDialog.Builder(getContext())%0A        .setCancelable(false)%0A        .setTitle(getTitle())%0A        .setPositiveButton(%0A            R.string.apply_multiple_choice_changes,%0A            (dialog, which) -%3E getValueConsumer().accept(onCreateResponse()))%0A        .setNegativeButton(R.string.cancel, (dialog, which) -%3E %7B%7D);%0A  %7D%0A%0A  private AlertDialog createDialog() %7B%0A    initSelectedState();%0A    return createDialogBuilder().create();%0A  %7D%0A%0A  /** Creates and displays the dialog. */%0A  protected void show() %7B%0A    createDialog().show();%0A  %7D%0A%0A  /** Initialize current state. */%0A  protected abstract void initSelectedState
 ();%0A
+%0A
   
-%7D
+/** List of selected options. */%0A  protected abstract ImmutableList%3COption%3E getSelectedOptions();
 %0A%0A  
@@ -2899,32 +2899,46 @@
 stract Optional%3C
+MultipleChoice
 Response%3E getCur
@@ -3023,29 +3023,8 @@
 );%0A%0A
-  @AutoValue.Builder%0A
   pu
@@ -3057,16 +3057,19 @@
  Builder
+%3CB%3E
  %7B%0A%0A    
@@ -3081,30 +3081,24 @@
 c abstract B
-uilder
  setContext(
@@ -3133,30 +3133,24 @@
 c abstract B
-uilder
  setTitle(St
@@ -3180,30 +3180,24 @@
 c abstract B
-uilder
  setMultiple
@@ -3253,30 +3253,24 @@
 c abstract B
-uilder
  setCurrentV
@@ -3275,32 +3275,46 @@
 tValue(Optional%3C
+MultipleChoice
 Response%3E respon
@@ -3340,22 +3340,16 @@
 stract B
-uilder
  setValu
@@ -3401,58 +3401,8 @@
 er);
-%0A%0A    public abstract SelectDialogFactory build();
 %0A  %7D

Add base methods for generating alert dialog