package org.springframework.social.kakao.connect;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.social.ApiException;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.UserProfileBuilder;
import org.springframework.social.kakao.api.Kakao;
import org.springframework.social.kakao.api.KakaoProfile;

public class KakaoAdaptor implements ApiAdapter<Kakao> {

    @Override
    public boolean test(Kakao kakao) {
        try {
            kakao.userOperations().isStoryUser();
            return true;
        } catch (ApiException e) {
            return false;
        }
    }

    @Override
    public void setConnectionValues(Kakao kakao, ConnectionValues values) {
        KakaoProfile profile = fetchPrimaryProfile(kakao);
        if (profile != null) {
            values.setProviderUserId(profile.getId());
            values.setDisplayName(profile.getUsername());
            values.setProfileUrl(profile.getProfileUrl());
            values.setImageUrl(profile.getImageUrl());
        }
    }

    @Override
    public UserProfile fetchUserProfile(Kakao kakao) {
        UserProfileBuilder profileBuilder = new UserProfileBuilder();

        KakaoProfile profile = fetchPrimaryProfile(kakao);
        if (profile != null) {
            profileBuilder.setUsername(profile.getUsername());
        }

        return profileBuilder.build();

    }

    private KakaoProfile fetchPrimaryProfile(Kakao kakao) {
        KakaoProfile profile = kakao.userOperations().getTalkUserProfile();

        if (profile == null) {
            if (kakao.userOperations().isStoryUser()) {
                profile = kakao.userOperations().getStoryUserProfile();
            } else {
                profile = new KakaoProfile();
                profile.setUsername("카카오-" + RandomStringUtils.randomAlphanumeric(5));
            }
        }

        if (profile != null) {
            profile.setId(Integer.toString(kakao.userOperations().getUserId()));
        }

        return profile;
    }

    @Override
    public void updateStatus(Kakao kakao, String message) {
        kakao.storyOperations().postStory(message, null);
    }
}

@@ -1619,32 +1619,75 @@
 rofile profile =
+ null;%0A%0A        try %7B%0A            profile =
  kakao.userOpera
@@ -1715,16 +1715,54 @@
 ofile();
+%0A        %7D catch (Exception ignore) %7B%7D
 %0A%0A      
@@ -2022,11 +2022,13 @@
 me(%22
-%EC%B9%B4%EC%B9%B4%EC%98%A4
+Kakao
 -%22 +

Fix to normally retrieve other profile for who is not connected to Kakao Talk