package com.atlassian.stash.internal.sal.user;

import com.atlassian.sal.api.user.UserKey;
import com.atlassian.sal.api.user.UserProfile;
import com.atlassian.bitbucket.avatar.AvatarRequest;
import com.atlassian.bitbucket.avatar.AvatarService;
import com.atlassian.bitbucket.nav.NavBuilder;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.user.ApplicationUserEquality;
import com.atlassian.bitbucket.util.UrlUtils;

import javax.annotation.Nonnull;
import java.net.URI;

import static com.google.common.base.Preconditions.checkNotNull;

public class DefaultUserProfile implements UserProfile {

    private static final int DEFAULT_AVATAR_SIZE = 128;

    private final AvatarService avatarService;
    private final NavBuilder navBuilder;
    private final ApplicationUser user;

    public DefaultUserProfile(@Nonnull ApplicationUser user, @Nonnull NavBuilder navBuilder,
                              @Nonnull AvatarService avatarService) {
        this.avatarService = checkNotNull(avatarService, "avatarService");
        this.navBuilder = checkNotNull(navBuilder, "navBuilder");
        this.user = checkNotNull(user, "user");
    }

    @Override
    public String getEmail() {
        return user.getEmailAddress();
    }

    @Override
    public String getFullName() {
        return user.getDisplayName();
    }

    @Override
    public UserKey getUserKey() {
        return UserKey.fromLong(user.getId());
    }

    @Override
    public String getUsername() {
        return user.getName();
    }

    @Override
    public URI getProfilePictureUri(int width, int height) {
        // our avatars are always square, ignore the "preferred" height
        return getUrlForPerson(width);
    }

    @Override
    public URI getProfilePictureUri() {
        return getUrlForPerson(DEFAULT_AVATAR_SIZE);
    }

    @Override
    public URI getProfilePageUri() {
        return URI.create(navBuilder.user(user).buildRelNoContext());
    }

    /**
     * @return the {@link ApplicationUser} backing the {@link UserProfile}.
     */
    public ApplicationUser getUser() {
        return user;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        DefaultUserProfile that = (DefaultUserProfile) o;

        return ApplicationUserEquality.equals(getUser(), that.getUser());
    }

    @Override
    public int hashCode() {
        return user.hashCode();
    }

    /**
     * Protected access for testing.
     *
     * @param width the width (and height, as it's square) of the avatar
     * @return an absolute URI to the avatar for this user
     */
    protected URI getUrlForPerson(final int width) {
        boolean useHttps = navBuilder.buildBaseUrl().startsWith("https");
        String avatarUrl = avatarService.getUrlForPerson(user, new AvatarRequest(useHttps, width));
        return UrlUtils.uncheckedCreateURI(avatarUrl);
    }
}
