package qa.qcri.aidr.manager.social.configuration;

import java.sql.Timestamp;
import java.util.UUID;

import javax.annotation.Resource;
import javax.inject.Inject;

import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.support.OAuth1Connection;
import org.springframework.social.connect.support.OAuth2Connection;
import org.springframework.stereotype.Component;

import qa.qcri.aidr.manager.persistence.entities.UserAccount;
import qa.qcri.aidr.manager.persistence.entities.UserConnection;
import qa.qcri.aidr.manager.service.UserConnectionService;
import qa.qcri.aidr.manager.service.UserService;
import qa.qcri.aidr.manager.util.ConstantUtils;

@Component("userConnectionSignUp")
public class UserConnectionSignUp implements ConnectionSignUp {

	@Inject
	private UserConnectionService userConnectionService;
	
	@Resource(name="userService")
	private UserService userService;
	
	@Override
    public String execute(Connection<?> connection) {
		if(connection instanceof OAuth1Connection){
			OAuth1Connection<?> oauthConnection = (OAuth1Connection<?>) connection;
			ConnectionData data = oauthConnection.createData();
	        UserProfile profile = oauthConnection.fetchUserProfile();
	        UserConnection userConnection = new UserConnection();
	        userConnection.setUserId(profile.getUsername());
	        userConnection.setImageUrl(data.getImageUrl());
	        userConnection.setProfileUrl(data.getProfileUrl());
	        userConnection.setAccessToken(data.getAccessToken());
	        userConnection.setSecret(data.getSecret());
	        userConnection.setRefreshToken(data.getRefreshToken());
	        userConnection.setProviderUserId(data.getProviderUserId());
	        /**
	         * This is custom SignUp and for it providerID should be springSecurity
	         */
	        userConnection.setProviderId("springSocialSecurity");
	        userConnection.setDisplayName(data.getDisplayName());
	        userConnection.setRank(1);
	        userConnectionService.register(userConnection);
	        Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
	        UserAccount user = new UserAccount();
	        // TODO move to hibernate level
	        user.setApiKey(UUID.randomUUID().toString());
	        user.setCreatedAt(currentTimestamp);
	        user.setUpdatedAt(currentTimestamp);
	        user.setProvider(data.getProviderId());
	        user.setUserName(data.getProviderId() + ConstantUtils.USER_NAME_SPLITTER + profile.getUsername());
	        userService.save(user);
	        return profile.getUsername();
	        
		}else if (connection instanceof OAuth2Connection) {
			OAuth2Connection<?> oauthConnection = (OAuth2Connection<?>) connection;
			ConnectionData data = oauthConnection.createData();
			UserProfile profile = oauthConnection.fetchUserProfile();
			
			UserConnection userConnection = new UserConnection();
			userConnection.setUserId(profile.getEmail());
			userConnection.setImageUrl(data.getImageUrl());
			userConnection.setProfileUrl(data.getProfileUrl());
			userConnection.setAccessToken(data.getAccessToken());
			userConnection.setSecret(data.getSecret());
			userConnection.setRefreshToken(data.getRefreshToken());
			userConnection.setProviderUserId(data.getProviderUserId());
			userConnection.setProviderId("springSocialSecurity");
			userConnection.setDisplayName(data.getDisplayName());
			userConnection.setRank(1);
			userConnectionService.register(userConnection);
			
			Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
			UserAccount user = new UserAccount();
			user.setApiKey(UUID.randomUUID().toString());
			user.setCreatedAt(currentTimestamp);
			user.setUpdatedAt(currentTimestamp);
			user.setProvider(data.getProviderId());
			user.setUserName(data.getProviderId() + ConstantUtils.USER_NAME_SPLITTER + profile.getEmail());
			user.setEmail(profile.getEmail());
			userService.save(user);
			
			return profile.getEmail();
		}
		
		return connection.fetchUserProfile().getUsername();
    }

}

@@ -2272,16 +2272,160 @@
         
+%0D%0A%09        if(userService.fetchByUserName(data.getProviderId() + ConstantUtils.USER_NAME_SPLITTER + profile.getUsername()) == null)%7B%0D%0A%09        %09
 Timestam
@@ -2485,24 +2485,25 @@
 Millis());%0D%0A
+%09
 %09        Use
@@ -2543,49 +2543,8 @@
 ;%0D%0A%09
-        // TODO move to hibernate level%0D%0A
 %09   
@@ -2591,24 +2591,25 @@
 String());%0D%0A
+%09
 %09        use
@@ -2635,32 +2635,33 @@
 entTimestamp);%0D%0A
+%09
 %09        user.se
@@ -2687,24 +2687,25 @@
 imestamp);%0D%0A
+%09
 %09        use
@@ -2734,32 +2734,33 @@
 ProviderId());%0D%0A
+%09
 %09        user.se
@@ -2844,32 +2844,33 @@
 etUsername());%0D%0A
+%09
 %09        userSer
@@ -2883,24 +2883,36 @@
 ave(user);%0D%0A
+%09        %7D%0D%0A
 %09        ret
@@ -3854,16 +3854,140 @@
 ;%0D%0A%09%09%09%0D%0A
+%09%09%09if(userService.fetchByUserName(data.getProviderId() + ConstantUtils.USER_NAME_SPLITTER + profile.getEmail()) == null)%7B%0D%0A%09
 %09%09%09Times
@@ -4053,24 +4053,25 @@
 lis());%0D%0A%09%09%09
+%09
 UserAccount 
@@ -4093,24 +4093,25 @@
 Account();%0D%0A
+%09
 %09%09%09user.setA
@@ -4143,32 +4143,33 @@
 oString());%0D%0A%09%09%09
+%09
 user.setCreatedA
@@ -4182,32 +4182,33 @@
 entTimestamp);%0D%0A
+%09
 %09%09%09user.setUpdat
@@ -4231,24 +4231,25 @@
 stamp);%0D%0A%09%09%09
+%09
 user.setProv
@@ -4273,24 +4273,25 @@
 iderId());%0D%0A
+%09
 %09%09%09user.setU
@@ -4377,24 +4377,25 @@
 ail());%0D%0A%09%09%09
+%09
 user.setEmai
@@ -4417,24 +4417,25 @@
 ail());%0D%0A%09%09%09
+%09
 userService.
@@ -4443,24 +4443,30 @@
 ave(user);%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%09%0D%0A%09%09%09retu

Fix sign up issue