package com.springsource.greenhouse.connect;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.core.Authentication;
import org.springframework.social.facebook.FacebookAccessToken;
import org.springframework.social.facebook.FacebookLink;
import org.springframework.social.facebook.FacebookOperations;
import org.springframework.social.facebook.FacebookUserId;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.flash.FlashMap;

import com.springsource.greenhouse.account.Account;
import com.springsource.greenhouse.account.AccountRepository;
import com.springsource.greenhouse.members.ProfilePictureService;

@Controller
@RequestMapping("/connect/facebook")
public class FacebookConnectController {

	private final FacebookAccountProvider accountProvider;
	
	private final ProfilePictureService profilePictureService;

	private final AccountRepository accountRepository;

	@Inject
	public FacebookConnectController(AccountRepository accountRepository, FacebookAccountProvider accountProvider,
			ProfilePictureService profilePictureService) {
		this.accountRepository = accountRepository;
		this.accountProvider = accountProvider;
		this.profilePictureService = profilePictureService;
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String connectView(Account account, @FacebookUserId String facebookUserId, Model model) {
		if (accountProvider.isConnected(account.getId())) {
			model.addAttribute("facebookUserId", facebookUserId);
			return "connect/facebookConnected";
		} else {
			return "connect/facebookConnect";
		}
	}
	
	@RequestMapping(method=RequestMethod.POST) 
	public String connectAccountToFacebook(Account account, @FacebookAccessToken String accessToken,
			@FacebookUserId String facebookUserId, HttpServletRequest request) {
		accountProvider.connect(account.getId(), new ConnectionDetails(accessToken, "", facebookUserId));
		if (request.getParameter("postIt") != null) {
			postToWall(account);
		}
		if (request.getParameter("useFBPic") != null) {
			useFacebookProfilePicture(account, facebookUserId);
		}
		FlashMap.setSuccessMessage("Your Greenhouse account is now connected to your Facebook account!");
		return "redirect:/connect/facebook";
	}
	
	@RequestMapping(method=RequestMethod.DELETE)
	public String disconnectFacebook(Account account, HttpServletRequest request, Authentication authentication) {
		accountProvider.disconnect(account.getId());
		return "redirect:/connect/facebook";
	}
	
	private void postToWall(Account account) {
		accountProvider.getFacebookApi(account.getId()).postToWall("Join me at the Greenhouse!",
			new FacebookLink(account.getProfileUrl(), "Greenhouse", "Where Spring developers hang out.", 
					"We help you connect with fellow application developers and take advantage of everything the Spring community has to offer."));
	}
	
	private void useFacebookProfilePicture(Account account, String facebookUserId) {
		try {
			FacebookOperations facebookApi = accountProvider.getFacebookApi(account.getId());			
			byte[] imageBytes = facebookApi.getProfilePicture(facebookUserId);
	        profilePictureService.saveProfilePicture(account.getId(), imageBytes);
			accountRepository.markProfilePictureSet(account.getId());
		} catch (IOException e) {
			FlashMap.setWarningMessage("Greenhouse was unable to use your Facebook profile picture.");
		}
	}
	
}
@@ -2024,24 +2024,80 @@
  request) %7B%0A
+%09%09if (facebookUserId != null && accessToken != null) %7B%0A%09
 %09%09accountPro
@@ -2186,16 +2186,17 @@
 Id));%0A%09%09
+%09
 if (requ
@@ -2236,16 +2236,17 @@
 l) %7B%0A%09%09%09
+%09
 postToWa
@@ -2260,18 +2260,20 @@
 unt);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09if (re
@@ -2317,16 +2317,17 @@
 l) %7B%0A%09%09%09
+%09
 useFaceb
@@ -2372,18 +2372,20 @@
 rId);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09FlashM
@@ -2476,16 +2476,21 @@
 unt!%22);%0A
+%09%09%7D%0A%0A
 %09%09return

Handle case where user clicks Cancel at Facebook connect time--don't try to create connection unless you have a Facebook user ID and access token.