package sagan.site.team.support;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sagan.site.team.MemberProfile;

import java.util.List;

/**
 * Service providing high-level, selectively cached data access and other
 * {@link MemberProfile}-related operations.
 */
@Service
public class TeamService {

    private static Log logger = LogFactory.getLog(TeamService.class);

    private final TeamRepository teamRepository;

    @Autowired
    public TeamService(TeamRepository teamRepository) {
        this.teamRepository = teamRepository;
    }

    public MemberProfile fetchMemberProfile(Long id) {
        return teamRepository.findById(id);
    }

    public MemberProfile fetchMemberProfileUsername(String username) {
        MemberProfile profile = teamRepository.findByUsername(username);
        if (profile == null) {
            profile = MemberProfile.NOT_FOUND;
        }
        return profile;
    }

    public void updateMemberProfile(Long id, MemberProfile profile) {
        updateMemberProfile(profile, fetchMemberProfile(id));
    }

    public void updateMemberProfile(String username, MemberProfile updatedProfile) {
        updateMemberProfile(updatedProfile, fetchMemberProfileUsername(username));
    }

    private void updateMemberProfile(MemberProfile profile, MemberProfile existingProfile) {
        existingProfile.setBio(profile.getBio());
        existingProfile.setName(profile.getName());
        existingProfile.setJobTitle(profile.getJobTitle());
        existingProfile.setTwitterUsername(profile.getTwitterUsername());
        existingProfile.setSpeakerdeckUsername(profile.getSpeakerdeckUsername());
        existingProfile.setLanyrdUsername(profile.getLanyrdUsername());
        existingProfile.setLocation(profile.getLocation());
        existingProfile.setGeoLocation(profile.getGeoLocation());
        existingProfile.setVideoEmbeds(profile.getVideoEmbeds());
        existingProfile.setGravatarEmail(profile.getGravatarEmail());
        existingProfile.setHidden(profile.isHidden());
        updateAvatarUrlwithGravatar(existingProfile);

        teamRepository.save(existingProfile);
    }

    public List<MemberProfile> fetchActiveMembers() {
        return teamRepository.findByHiddenOrderByNameAsc(false);
    }

    public List<MemberProfile> fetchHiddenMembers() {
        return teamRepository.findByHiddenOrderByNameAsc(true);
    }

    public MemberProfile createOrUpdateMemberProfile(Long githubId, String username, String avatarUrl, String name) {
        MemberProfile profile = teamRepository.findByGithubId(githubId);

        if (profile == null) {
            profile = new MemberProfile();
            profile.setGithubId(githubId);
            profile.setUsername(username);
            profile.setHidden(true);
        }
        profile.setAvatarUrl(avatarUrl);
        profile.setName(name);
        profile.setGithubUsername(username);
        updateAvatarUrlwithGravatar(profile);
        return teamRepository.save(profile);
    }

    public void showOnlyTeamMembersWithIds(List<Long> userIds) {
        teamRepository.hideTeamMembersNotInIds(userIds);
    }

    private void updateAvatarUrlwithGravatar(MemberProfile profile) {
        if (!StringUtils.isEmpty(profile.getGravatarEmail())) {
            Md5PasswordEncoder encoder = new Md5PasswordEncoder();
            String hashedEmail = encoder.encodePassword(profile.getGravatarEmail(), null);
            profile.setAvatarUrl(String.format("https://gravatar.com/avatar/%s", hashedEmail));
        }
    }
}

@@ -179,88 +179,8 @@
 ed;%0A
-import org.springframework.security.authentication.encoding.Md5PasswordEncoder;%0A
 impo
@@ -322,17 +322,142 @@
 ava.
-util.List
+security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.util.List;%0A%0Aimport javax.xml.bind.DatatypeConverter
 ;%0A%0A/
@@ -3587,163 +3587,209 @@
 ) %7B%0A
-            Md5PasswordEncoder encoder = new Md5PasswordEncoder();%0A            String hashedEmail = encoder.encodePassword(profile.getGravatarEmail(), null
+%09%09%09try %7B%0A%09%09%09%09MessageDigest digest = MessageDigest.getInstance(%22MD5%22);%0A%09%09%09%09digest.update(profile.getGravatarEmail().getBytes());%0A%09%09%09%09String hashedEmail = DatatypeConverter.printHexBinary(digest.digest()
 );%0A 
@@ -3791,32 +3791,33 @@
 ));%0A            
+%09
 profile.setAvata
@@ -3880,24 +3880,128 @@
 hedEmail));%0A
+%09%09%09%7D%0A%09%09%09catch (NoSuchAlgorithmException e) %7B%0A%09%09%09%09logger.error(%22Could not find MD5 MessageDigest%22);%0A%09%09%09%7D%0A
         %7D%0A  

Use Java's MessageDigest for generating MD5 hashes