/*
 * 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.offlinebasemap.viewer;

import static com.google.android.gnd.rx.RxAutoDispose.autoDisposable;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gnd.MainActivity;
import com.google.android.gnd.R;
import com.google.android.gnd.databinding.OfflineBaseMapViewerFragBinding;
import com.google.android.gnd.model.basemap.OfflineBaseMap;
import com.google.android.gnd.model.feature.Point;
import com.google.android.gnd.ui.common.AbstractFragment;
import com.google.android.gnd.ui.common.Navigator;
import com.google.android.gnd.ui.map.MapAdapter;
import com.google.android.gnd.ui.map.MapProvider;
import dagger.hilt.android.AndroidEntryPoint;
import io.reactivex.Single;
import javax.inject.Inject;
import timber.log.Timber;

/**
 * The OfflineAreaViewerFragment provides a UI for managing a single offline area on the user's
 * device.
 */
@AndroidEntryPoint
public class OfflineBaseMapViewerFragment extends AbstractFragment {

  private static final String MAP_FRAGMENT = MapProvider.class.getName() + "#fragment";

  @Inject Navigator navigator;
  @Inject MapProvider mapProvider;

  private OfflineBaseMapViewerViewModel viewModel;
  @Nullable private MapAdapter map;

  @Inject
  public OfflineBaseMapViewerFragment() {}

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    OfflineBaseMapViewerFragmentArgs args =
        OfflineBaseMapViewerFragmentArgs.fromBundle(getArguments());
    viewModel = getViewModel(OfflineBaseMapViewerViewModel.class);
    viewModel.loadOfflineArea(args);
    Single<MapAdapter> mapAdapter = mapProvider.getMapAdapter();
    mapAdapter.as(autoDisposable(this)).subscribe(this::onMapReady);
    viewModel.getOfflineArea().observe(this, this::panMap);
  }

  @Override
  public View onCreateView(
      @NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    OfflineBaseMapViewerFragBinding binding =
        OfflineBaseMapViewerFragBinding.inflate(inflater, container, false);
    binding.setViewModel(viewModel);
    binding.setLifecycleOwner(this);
    binding.removeButton.setOnClickListener(__ -> onRemoveClick());
    ((MainActivity) getActivity()).setActionBar(binding.offlineAreaViewerToolbar, true);
    return binding.getRoot();
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (savedInstanceState == null) {
      replaceFragment(R.id.map, mapProvider.getFragment());
    } else {
      mapProvider.restore(restoreChildFragment(savedInstanceState, MAP_FRAGMENT));
    }
  }

  private void onMapReady(MapAdapter map) {
    this.map = map;
  }

  private void panMap(OfflineBaseMap offlineBaseMap) {
    if (map == null) {
      return;
    }

    double lat = offlineBaseMap.getBounds().northeast.latitude;
    double lon = offlineBaseMap.getBounds().southwest.longitude;
    Point point = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
    map.moveCamera(point);
  }

  /** Removes the area associated with this fragment from the user's device. */
  public void onRemoveClick() {
    if (map == null) {
      return;
    }

    Timber.d("Removing offline area %s", viewModel.getOfflineArea());
    viewModel
        .onRemoveClick()
        .doOnComplete(() -> navigator.navigateUp())
        .as(autoDisposable(this))
        .subscribe();
  }
}

@@ -4160,60 +4160,8 @@
 k()%0A
-        .doOnComplete(() -%3E navigator.navigateUp())%0A
     
@@ -4209,16 +4209,44 @@
 bscribe(
+() -%3E navigator.navigateUp()
 );%0A  %7D%0A%7D

Move navigation logic in basemap viewer fragment into subscribe