package foodtruck.schedule;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.gdata.client.calendar.CalendarService;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

import org.joda.time.DateTimeZone;

import foodtruck.config.TruckConfigParser;
import foodtruck.geolocation.GeoLocator;
import foodtruck.model.Trucks;
import twitter4j.TwitterFactory;

/**
 * @author aviolette@gmail.com
 * @since Jul 19, 2011
 */
public class ScheduleModule extends AbstractModule {
  @Override
  protected void configure() {
    // TODO: use assisted inject
    bind(CalendarQueryFactory.class).to(CalendarQueryFactoryImpl.class);
  }

  @Provides @Named("calendar.feed.url")
  public URL provideCalendarUrl() throws MalformedURLException {
    return new URL(System.getProperty("calendar.feed.url"));
  }

  @Provides @Named("foodtruck.twitter.list")
  public int provideTwitterListId() {
    return Integer.parseInt(System.getProperty("foodtruck.twitter.list"));
  }

  @Provides
  public CalendarService provideCalendarService() {
    return new CalendarService("foodtruck-app");
  }

  @Provides
  public GoogleCalendar provideGoogleCalendarStrategy(CalendarService service,
      CalendarQueryFactory queryProvider, DateTimeZone zone, GeoLocator geoLocator) {
    return new GoogleCalendar(service, queryProvider, zone, geoLocator);
  }

  @Provides @Singleton
  public TwitterFactoryWrapper provideTwitterFactory() {
    return new TwitterFactoryWrapper(new TwitterFactory());
  }

  @Provides @Singleton
  public Trucks providesTrucks(TruckConfigParser parser)
      throws FileNotFoundException {
    String url =
        Thread.currentThread().getContextClassLoader().getResource("trucks.yaml").getFile();
    return parser.parse(url);
  }
}


@@ -1180,30 +1180,49 @@
 ice() %7B%0A    
-return
+CalendarService service =
  new Calenda
@@ -1248,16 +1248,73 @@
 -app%22);%0A
+    service.setConnectTimeout(6000);%0A    return service;%0A
   %7D%0A%0A  @

Set connection timeout higher than default so that it doesn't crap out when syncing the cache.