package org.springframework.social.greenhouse;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.social.oauth.OAuthSigningClientHttpRequestFactory;
import org.springframework.social.oauth1.ScribeOAuth1RequestSigner;
import org.springframework.social.twitter.TwitterErrorHandler;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class GreenhouseTemplate implements GreenhouseOperations {
	private RestTemplate restOperations;

	public GreenhouseTemplate(String apiKey, String apiSecret, String accessToken, String accessTokenSecret) {
		RestTemplate restTemplate = new RestTemplate(new OAuthSigningClientHttpRequestFactory(
				new SimpleClientHttpRequestFactory(),
				new ScribeOAuth1RequestSigner(apiKey, apiSecret, accessToken, accessTokenSecret)));
		restTemplate.setErrorHandler(new TwitterErrorHandler());
		this.restOperations = restTemplate;
		jsonAcceptingHeaders = new LinkedMultiValueMap<String, String>();
		jsonAcceptingHeaders.add("Accept", "application/json");
	}
	

	public GreenhouseProfile getUserProfile() {
		return restOperations.exchange(PROFILE_URL, HttpMethod.GET,
				new HttpEntity<Object>(jsonAcceptingHeaders), GreenhouseProfile.class, "@self").getBody();
	}

	public List<Event> getUpcomingEvents() {
		return Arrays.asList(restOperations.exchange(EVENTS_URL, HttpMethod.GET,
				new HttpEntity<Object>(jsonAcceptingHeaders), Event[].class).getBody());
	}

	public List<Event> getEventsAfter(Date date) {
		String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000-00:00").format(date);
		return Arrays.asList(restOperations.exchange(EVENTS_URL + "?after={dateTime}", HttpMethod.GET,
				new HttpEntity<Object>(jsonAcceptingHeaders), Event[].class, isoDate).getBody());
	}

	public List<EventSession> getSessionsOnDay(long eventId, Date date) {
		String isoDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
		return Arrays.asList(restOperations.exchange(SESSIONS_FOR_DAY_URL, HttpMethod.GET,
				new HttpEntity<Object>(jsonAcceptingHeaders), EventSession[].class, eventId, isoDate).getBody());
	}


	private static final String BASE_URL = "http://localhost:8080/greenhouse";
	private static final String PROFILE_URL = BASE_URL + "/members/{id}";
	private static final String EVENTS_URL = BASE_URL + "/events";
	private static final String SESSIONS_FOR_DAY_URL = BASE_URL + "/events/{eventId}/sessions/{day}";
	private MultiValueMap<String, String> jsonAcceptingHeaders;
}

@@ -772,16 +772,41 @@
 rations;
+%0A%09private String baseUrl;
 %0A%0A%09publi
@@ -907,16 +907,234 @@
 cret) %7B%0A
+        this(apiKey, apiSecret, accessToken, accessTokenSecret, DEFAULT_BASE_URL);%0A    %7D%0A    %0A%09public GreenhouseTemplate(String apiKey, String apiSecret, String accessToken, String accessTokenSecret, String baseUrl) %7B%0A
 %09%09RestTe
@@ -1567,16 +1567,42 @@
 json%22);%0A
+%09%09this.baseUrl = baseUrl;%0A
 %09%7D%0A%09%0A%0A%09p
@@ -1677,24 +1677,34 @@
 xchange(
+baseUrl + 
 PROFILE_
 URL, Htt
@@ -1695,19 +1695,20 @@
 PROFILE_
-URL
+PATH
 , HttpMe
@@ -1905,26 +1905,37 @@
 xchange(
+baseUrl + 
 EVENTS_
-URL
+PATH
 , HttpMe
@@ -2209,26 +2209,37 @@
 xchange(
+baseUrl + 
 EVENTS_
-URL
+PATH
  + %22?aft
@@ -2546,16 +2546,26 @@
 xchange(
+baseUrl + 
 SESSIONS
@@ -2573,19 +2573,20 @@
 FOR_DAY_
-URL
+PATH
 , HttpMe
@@ -2731,16 +2731,24 @@
  String 
+DEFAULT_
 BASE_URL
@@ -2759,36 +2759,39 @@
 http
-://localhost:8080/greenhouse
+s://greenhouse.springsource.org
 %22;%0A%09
@@ -2826,32 +2826,22 @@
 PROFILE_
-URL = BASE_URL +
+PATH =
  %22/membe
@@ -2886,32 +2886,22 @@
  EVENTS_
-URL = BASE_URL +
+PATH =
  %22/event
@@ -2954,24 +2954,14 @@
 DAY_
-URL = BASE_URL +
+PATH =
  %22/e

Enable the base URL to be set for test/dev environments