package com.atlassian.bitbucket.internal.mirroring.mirror;

import com.atlassian.bitbucket.hamcrest.MatcherBuilder;
import com.atlassian.bitbucket.hamcrest.OptionalMatcher;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

/**
 * @since 4.6
 */
public class UpstreamInstallationParametersMatcher extends TypeSafeDiagnosingMatcher<UpstreamInstallationParameters> {

    private String apiBaseUrl;
    private String issuerId;
    private String secret;
    private String teamId;
    private String teamName;
    private String upstreamId;

    public static UpstreamInstallationParametersMatcher parameters() {
        return new UpstreamInstallationParametersMatcher();
    }

    public UpstreamInstallationParametersMatcher() {
    }

    public UpstreamInstallationParametersMatcher(UpstreamInstallationParameters parameters) {
        apiBaseUrl = parameters.getApiBaseUrl().orElse(null);
        issuerId = parameters.getIssuerId();
        secret = parameters.getSecret();
        teamId = parameters.getTeamId().orElse(null);
        teamName = parameters.getTeamName().orElse(null);
        upstreamId = parameters.getUpstreamId();
    }

    public UpstreamInstallationParametersMatcher apiBaseUrl(String value) {
        apiBaseUrl = value;
        return this;
    }

    public UpstreamInstallationParametersMatcher issuerId(String value) {
        issuerId = value;
        return this;
    }

    public UpstreamInstallationParametersMatcher secret(String value) {
        secret = value;
        return this;
    }

    public UpstreamInstallationParametersMatcher teamId(String value) {
        teamId = value;
        return this;
    }

    public UpstreamInstallationParametersMatcher teamName(String value) {
        teamName = value;
        return this;
    }

    public UpstreamInstallationParametersMatcher upstreamId(String value) {
        upstreamId = value;
        return this;
    }

    @Override
    protected boolean matchesSafely(UpstreamInstallationParameters item, Description mismatchDescription) {
        return new MatcherBuilder()
                .matches("apiBaseUrl", OptionalMatcher.fromNullable(apiBaseUrl), item.getApiBaseUrl())
                .equal("issuerId", issuerId, item.getIssuerId())
                .equal("secret", secret, item.getSecret())
                .matches("teamId", OptionalMatcher.fromNullable(teamId), item.getTeamId())
                .matches("teamName", OptionalMatcher.fromNullable(teamName), item.getTeamName())
                .equal("upstreamId", upstreamId, item.getUpstreamId())
                .collect(mismatchDescription);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(String.format("{apiBaseUrl: %s, issuerId: %s, secret: %s, teamUser: %s, upstreamId: %s}",
                apiBaseUrl, issuerId, secret, teamName, upstreamId));
    }
}
