package com.atlassian.bitbucket.test.rest;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

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

public class RestAuthentication {

    private final String password;
    private final String username;

    private RestAuthentication() {
        password = username = null;
    }

    private RestAuthentication(String username, String password) {
        this.password = password;
        this.username = username;
    }

    public static RestAuthentication anonymous() {
        return new RestAuthentication();
    }

    /**
     * @param username the username, also used as the password
     * @return an {@link RestAuthentication authentication} directive that will authenticate as the specified user
     * @see #authenticated(String, String)
     */
    public static RestAuthentication authenticated(@Nonnull String username) {
        return authenticated(username, username);
    }

    public static RestAuthentication authenticated(@Nonnull String username, @Nullable String password) {
        checkArgument(!checkNotNull(username, "username").isEmpty(),
                "A username is required for an authenticated request.");

        return new RestAuthentication(username, password);
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public boolean isAuthenticated() {
        return username != null;
    }
}
