package com.atlassian.bitbucket.internal.branch.model.matchers;

import com.atlassian.bitbucket.internal.branch.model.configuration.BranchConfiguration;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

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

import static org.hamcrest.Matchers.*;

public class BranchConfigurationMatchers {

    private BranchConfigurationMatchers() {
        throw new UnsupportedOperationException(BranchConfigurationMatchers.class.getName()
                + " should not be instantiated");
    }

    @Nonnull
    public static Matcher<BranchConfiguration> isConcreteRefId(@Nullable String expectedRefId) {
        return allOf(withRefIdThat(is(expectedRefId)), isDefaultBranch(false));
    }

    @Nonnull
    public static Matcher<BranchConfiguration> isDefaultBranch() {
        return allOf(withRefIdThat(isEmptyOrNullString()), isDefaultBranch(true));
    }

    @Nonnull
    public static Matcher<BranchConfiguration> withRefId(@Nullable String expectedRefId) {
        return withRefIdThat(is(expectedRefId));
    }

    @Nonnull
    public static Matcher<BranchConfiguration> withRefIdThat(@Nonnull Matcher<String> refIdMatcher) {
        return new FeatureMatcher<BranchConfiguration, String>(refIdMatcher, "ref ID that", "refId") {
            @Override
            protected String featureValueOf(BranchConfiguration actual) {
                return actual.getRefId();
            }
        };
    }

    @Nonnull
    public static Matcher<BranchConfiguration> isDefaultBranch(boolean isDefaultBranch) {
        return new FeatureMatcher<BranchConfiguration, Boolean>(is(isDefaultBranch),
                "branch configuration use default branch", "useDefaultBranch") {
            @Override
            protected Boolean featureValueOf(BranchConfiguration actual) {
                return actual.isUseDefault();
            }
        };
    }
}
