package com.atlassian.bitbucket.internal.mirroring.rest.auth;

import com.atlassian.bitbucket.internal.mirroring.auth.Credentials;
import com.atlassian.bitbucket.rest.RestMapEntity;

import javax.validation.constraints.NotNull;

public class RestAuthenticationRequest extends RestMapEntity {
    // default ctor for jersey
    @SuppressWarnings("unused")
    public RestAuthenticationRequest() {
    }

    public RestAuthenticationRequest(RestUsernamePasswordCredentials credentials) {
        put("credentials", credentials);
    }
    
    public RestAuthenticationRequest(RestSshCredentials credentials) {
        put("credentials", credentials);
    }

    @NotNull(message = "{bitbucket.mirroring.authentication.type.unknown}")
    public Credentials getCredentials() {
        Credentials credentials = RestUsernamePasswordCredentials.valueOf(get("credentials"));
        if (credentials == null) {
            credentials = RestSshCredentials.valueOf(get("credentials"));
        }
        return credentials;
    }

    public static final RestAuthenticationRequest EXAMPLE_USERNAME_AND_PASSWORD_REQUEST =
            new RestAuthenticationRequest(RestUsernamePasswordCredentials.EXAMPLE);

    public static final RestAuthenticationRequest EXAMPLE_SSH_REQUEST =
            new RestAuthenticationRequest(RestSshCredentials.EXAMPLE);
}
