/*
 * Copyright 2018 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.projectselector;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.LiveDataReactiveStreams;
import com.google.android.gnd.model.Project;
import com.google.android.gnd.repository.ProjectRepository;
import com.google.android.gnd.rx.Loadable;
import com.google.android.gnd.system.auth.AuthenticationManager;
import com.google.android.gnd.ui.common.AbstractViewModel;
import com.google.common.collect.ImmutableList;
import io.reactivex.Single;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;

/** Represents view state and behaviors of the project selector dialog. */
public class ProjectSelectorViewModel extends AbstractViewModel {
  private final ProjectRepository projectRepository;
  private final LiveData<Loadable<List<Project>>> projectSummaries;

  @Inject
  ProjectSelectorViewModel(ProjectRepository projectRepository, AuthenticationManager authManager) {
    this.projectRepository = projectRepository;

    this.projectSummaries =
        LiveDataReactiveStreams.fromPublisher(
            projectRepository.getProjectSummaries(authManager.getCurrentUser()));
  }

  public LiveData<Loadable<List<Project>>> getProjectSummaries() {
    return projectSummaries;
  }

  public Single<ImmutableList<Project>> getOfflineProjects() {
    return projectRepository.getOfflineProjects();
  }

  private Project getProjectSummary(int idx) {
    return Loadable.getValue(this.projectSummaries).orElse(Collections.emptyList()).get(idx);
  }

  /**
   * Triggers the specified project to be loaded and activated.
   *
   * @param idx the index in the project summary list.
   */
  public void activateProject(int idx) {
    projectRepository.activateProject(getProjectSummary(idx).getId());
  }

  public void activateOfflineProject(String projectId) {
    projectRepository.activateProject(projectId);
  }
}

@@ -1093,27 +1093,20 @@
 va.util.
-Collections
+List
 ;%0Aimport
@@ -1106,34 +1106,39 @@
 %0Aimport java
+8
 .util.
-List
+Optional
 ;%0Aimport jav
@@ -1154,16 +1154,42 @@
 .Inject;
+%0Aimport timber.log.Timber;
 %0A%0A/** Re
@@ -1991,160 +1991,14 @@
  %7D%0A%0A
-  private Project getProjectSummary(int idx) %7B%0A    return Loadable.getValue(this.projectSummaries).orElse(Collections.emptyList()).get(idx);%0A  %7D%0A%0A
   /**%0A
+
    *
@@ -2172,34 +2172,145 @@
     
-projectRepository.
+Optional%3CList%3CProject%3E%3E projects = Loadable.getValue(this.projectSummaries);%0A    if (projects.isEmpty()) %7B%0A      Timber.e(%22Can't 
 activate
 Proj
@@ -2309,38 +2309,343 @@
 vate
-P
+ p
 roject
-(getProjectSummary(idx)
+ before list is loaded%22);%0A      return;%0A    %7D%0A    if (idx %3E= projects.get().size()) %7B%0A      Timber.e(%0A          %22Can't activate project at index %25d, only %25d projects in list%22,%0A          idx, projects.get().size());%0A      return;%0A    %7D%0A    Project project = projects.get().get(idx);%0A    projectRepository.activateProject(project
 .get

Check project list loaded before activating