/*
 * Copyright 2015 Ben Sechrist
 *
 * 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
 *
 *     http://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.bt4vt.geofence;

import com.google.inject.Inject;

import roboguice.receiver.RoboBroadcastReceiver;

public class BootReceiver extends RoboBroadcastReceiver {

  private static final String TAG = "BootReceiver";

  @Inject
  private BusStopGeofenceService busStopGeofenceService;

//  @Override
//  public void handleReceive(Context context, Intent intent) {
//    Log.d(TAG, "Received boot");
//    if (GoogleApiAvailability.getInstance()
//        .isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS) {
//      Log.e(TAG, context.getString(R.string.no_play_services));
//    } else {
//      List<StopFavorite> stopFavorites = new Select().from(StopFavorite.class)
//          .where("isFavorited = ?", true).execute();
//      Log.d(TAG, "Adding geofences for favorited stops");
//      Log.d(TAG, stopFavorites.toString());
//      for (StopFavorite stopFavorite : stopFavorites) {
//        // TODO: fetch stop info
//      }
//    }
//  }
//
//  @Override
//  public void onSuccess(StopModel stopModel) {
//    if (stopModel != null) {
//      busStopGeofenceService.registerGeofence(stopModel);
//    }
//  }
//
//  @Override
//  public void onException(Exception e) {
//    Log.e(TAG, e.getLocalizedMessage());
//  }
}

@@ -619,16 +619,445 @@
 fence;%0A%0A
+import android.content.Context;%0Aimport android.content.Intent;%0Aimport android.util.Log;%0A%0Aimport com.bt4vt.R;%0Aimport com.bt4vt.external.bt4u.Response;%0Aimport com.bt4vt.external.bt4u.Stop;%0Aimport com.bt4vt.external.bt4u.StopService;%0Aimport com.bt4vt.model.FavoriteStop;%0Aimport com.bt4vt.service.FavoriteStopService;%0Aimport com.google.android.gms.common.ConnectionResult;%0Aimport com.google.android.gms.common.GoogleApiAvailability;%0A
 import c
@@ -1082,16 +1082,40 @@
 nject;%0A%0A
+import java.util.List;%0A%0A
 import r
@@ -1212,16 +1212,83 @@
 eceiver 
+implements Response.Listener%3CStop%3E,%0A    Response.ExceptionListener 
 %7B%0A%0A  pri
@@ -1335,16 +1335,124 @@
 iver%22;%0A%0A
+  @Inject%0A  private FavoriteStopService favoriteStopService;%0A%0A  @Inject%0A  private StopService stopService;%0A%0A
   @Injec
@@ -1511,18 +1511,16 @@
 rvice;%0A%0A
-//
   @Overr
@@ -1515,34 +1515,32 @@
 e;%0A%0A  @Override%0A
-//
   public void ha
@@ -1581,26 +1581,24 @@
 t intent) %7B%0A
-//
     Log.d(TA
@@ -1614,26 +1614,24 @@
 ved boot%22);%0A
-//
     if (Goog
@@ -1662,18 +1662,16 @@
 tance()%0A
-//
         
@@ -1737,26 +1737,24 @@
 .SUCCESS) %7B%0A
-//
       Log.e(
@@ -1801,26 +1801,24 @@
 services));%0A
-//
     %7D else %7B
@@ -1818,18 +1818,16 @@
  else %7B%0A
-//
       Li
@@ -1829,28 +1829,24 @@
    List%3C
-Stop
 Favorite
 %3E stopFa
@@ -1841,121 +1841,70 @@
 rite
-%3E stopFavorites = new Select().from(StopFavorite.class)%0A//          .where(%22isFavorited = ?%22, true).execute
+Stop%3E favoriteStops = favoriteStopService.getFavoriteStops
 ();%0A
-//
     
@@ -1957,18 +1957,16 @@
 tops%22);%0A
-//
       Lo
@@ -1974,28 +1974,28 @@
 .d(TAG, 
-stopF
+f
 avorite
+Stop
 s.toStri
@@ -2001,18 +2001,16 @@
 ing());%0A
-//
       fo
@@ -2016,20 +2016,16 @@
 or (
-Stop
 Favorite
  sto
@@ -2024,51 +2024,53 @@
 rite
- stopF
+Stop f
 avorite
- : stopF
+Stop : f
 avorite
+Stop
 s) %7B%0A
-//
         
 // T
@@ -2069,43 +2069,69 @@
     
-// TODO: fetch stop info%0A//
+stopService.get(favoriteStop.getCode(), this, this);%0A
       %7D%0A
 //  
@@ -2126,35 +2126,27 @@
       %7D%0A
-//
     %7D%0A
-//
   %7D%0A
-//%0A//
+%0A
   @Overr
@@ -2141,34 +2141,32 @@
  %7D%0A%0A  @Override%0A
-//
   public void on
@@ -2169,74 +2169,30 @@
 d on
-Success(StopModel stopModel) %7B%0A//    if (stopModel != null
+Result(Stop result
 ) %7B%0A
-//  
     
@@ -2235,39 +2235,22 @@
 nce(
-stopModel);%0A//    %7D%0A//  %7D%0A//%0A//
+result);%0A  %7D%0A%0A
   @O
@@ -2257,18 +2257,16 @@
 verride%0A
-//
   public
@@ -2302,53 +2302,33 @@
 ) %7B%0A
-//
     
-Log.e(TAG, e.getLocalizedMessag
+e.printStackTrac
 e()
-)
 ;%0A
-//
   %7D%0A

Set geofences on boot