package com.wealthfront.magellan.sample.advanced.tide;

import android.content.Context;

import com.wealthfront.magellan.rx.RxScreen;
import com.wealthfront.magellan.sample.advanced.NoaaApi;
import com.wealthfront.magellan.sample.advanced.model.Observation;
import com.wealthfront.magellan.sample.advanced.model.TideInfo;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.wealthfront.magellan.sample.advanced.SampleApplication.app;

public class TideDetailsScreen extends RxScreen<TideDetailsView> {

  public static final Comparator<Observation>
      OBSERVATION_COMPARATOR = new Comparator<Observation>() {
    @Override
    public int compare(Observation o1, Observation o2) {
      return o1.getVerifiedWaterLevel().compareTo(o2.getVerifiedWaterLevel());
    }
  };
  @Inject NoaaApi noaaApi;
  private final String tideLocationName;
  int noaaApiId;

  TideDetailsScreen(int noaaApiId, String tideLocationName) {
    this.noaaApiId = noaaApiId;
    this.tideLocationName = tideLocationName;
  }

  @Override
  protected TideDetailsView createView(Context context) {
    app(context).injector().inject(this);
    return new TideDetailsView(context);
  }

  @Override
  public String getTitle(Context context) {
    return tideLocationName;
  }

  @Override
  protected void onSubscribe(Context context) {
    autoUnsubscribe(noaaApi.getTideInfo(noaaApiId)
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<TideInfo>() {
          @Override
          public void call(TideInfo tideInfo) {
            if (!tideInfo.getData().isEmpty()) {
              List<Observation> observations = tideInfo.getData();
              BigDecimal highestMeasuredTideHeight =
                  Collections.max(observations, OBSERVATION_COMPARATOR).getVerifiedWaterLevel();
              BigDecimal lowestMeasuredTideHeight =
                  Collections.min(observations, OBSERVATION_COMPARATOR).getVerifiedWaterLevel();
              BigDecimal latestMeasuredTideHeight =
                  observations.get(observations.size() - 1).getVerifiedWaterLevel();
              getView().setTideHeights(latestMeasuredTideHeight, lowestMeasuredTideHeight,
                  highestMeasuredTideHeight);
            }
          }
        }));
  }
}

@@ -760,22 +760,16 @@
 rvation%3E
-%0A     
  OBSERVA
@@ -785,16 +785,22 @@
 ARATOR =
+%0A     
  new Com
@@ -820,24 +820,28 @@
 vation%3E() %7B%0A
+    
     @Overrid
@@ -838,24 +838,28 @@
   @Override%0A
+    
     public i
@@ -899,24 +899,28 @@
 ation o2) %7B%0A
+    
       return
@@ -990,18 +990,26 @@
 ));%0A    
-%7D%0A
+    %7D%0A    
   %7D;%0A  @

fix checkstyle