package autoChirp.webController;

import autoChirp.DBConnector;
import java.util.Hashtable;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.social.twitter.api.UserOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AccountController {

private ConnectionRepository connectionRepository;
private Twitter twitter;

@Inject
public AccountController(ConnectionRepository connectionRepository, Twitter twitter) {
        this.connectionRepository = connectionRepository;
        this.twitter = twitter;
}

@RequestMapping("/account")
public ModelAndView account(HttpSession session) {
        ModelAndView mv = new ModelAndView("account");

        if (session.getAttribute("account") != null) {
                int userID = Integer.parseInt(((Hashtable<String,String>)session.getAttribute("account")).get("userID"));
                int groups = DBConnector.getGroupIDsForUser(userID).size();
                int tweets = DBConnector.getTweetsForUser(userID).size();

                mv.addObject("groups", groups);
                mv.addObject("tweets", tweets);
        }

        return mv;
}

@RequestMapping("/account/login")
public String login(HttpSession session) {
        if (connectionRepository.findPrimaryConnection(Twitter.class) != null) {
                UserOperations userOperations = twitter.userOperations();
                TwitterProfile twitterProfile = userOperations.getUserProfile();
                Connection<Twitter> twitterConnection = connectionRepository.getPrimaryConnection(Twitter.class);

                long twitterID = userOperations.getProfileId();
                int userID = DBConnector.checkForUser(twitterID);

                if (userID == -1) {
                        ConnectionData twitterConnectionData = twitterConnection.createData();
                        String token = twitterConnectionData.getAccessToken();
                        String secret = twitterConnectionData.getSecret();
                        userID = DBConnector.insertNewUser(twitterID, token, secret);
                }

                Hashtable<String, String> account = new Hashtable<String, String>();
                account.put("userID", Integer.toString(userID));
                account.put("twitterID", Long.toString(twitterID));
                account.put("name", twitterProfile.getName());
                account.put("handle", twitterProfile.getScreenName());
                account.put("description", twitterProfile.getDescription());
                account.put("url", twitterProfile.getProfileUrl());
                account.put("image", twitterProfile.getProfileImageUrl());
                account.put("protected", String.valueOf(twitterProfile.isProtected()));
                session.setAttribute("account", account);
        }

        return "redirect:/account";
}

@RequestMapping("/account/logout")
public String logout(SessionStatus sessionStatus) {
        connectionRepository.removeConnections("twitter");
        sessionStatus.setComplete();

        return "redirect:/account";
}

@RequestMapping(value = "/account/delete")
public String delete(HttpSession session, SessionStatus sessionStatus) {
        if (session.getAttribute("account") == null) return "redirect:/account";
        int userID = Integer.parseInt(((Hashtable<String,String>)session.getAttribute("account")).get("userID"));

        connectionRepository.removeConnections("twitter");
        sessionStatus.setComplete();
        DBConnector.deleteUser(userID);

        return "redirect:/home";
}

}

@@ -553,24 +553,61 @@
 Controller;%0A
+import org.springframework.ui.Model;%0A
 import org.s
@@ -657,16 +657,19 @@
 apping;%0A
+// 
 import o
@@ -3515,37 +3515,35 @@
  logout(
+Http
 Session
-Status
  session
 Status) 
@@ -3526,38 +3526,45 @@
 pSession session
-Status
+, Model model
 ) %7B%0A        conn
@@ -3625,34 +3625,58 @@
  session
-Status.setComplete
+.invalidate();%0A        model.asMap().clear
 ();%0A%0A   
@@ -3690,39 +3690,36 @@
 turn %22redirect:/
-account
+home
 %22;%0A%7D%0A%0A@RequestMa
@@ -3797,35 +3797,19 @@
 on, 
-SessionStatus sessionStatus
+Model model
 ) %7B%0A
@@ -4082,26 +4082,50 @@
 sion
-Status.setComplete
+.invalidate();%0A        model.asMap().clear
 ();%0A

Fix logout, error handling