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

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

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

import static com.atlassian.bitbucket.internal.branch.model.matchers.BranchTypeMatchers.withId;
import static org.hamcrest.Matchers.*;

public class BranchTypeConfigurationMatchers {

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

    @Nonnull
    public static Matcher<BranchTypeConfiguration> typeConfigurationWith(@Nullable String expectedId,
                                                                         @Nullable String expectedPrefix,
                                                                         boolean isEnabled) {
        return allOf(
                withId(BranchTypeConfiguration.class, expectedId),
                withPrefix(expectedPrefix),
                isEnabled(isEnabled)
        );
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> disabledTypeConfiguration(@Nullable String expectedId) {
        return allOf(
                withId(BranchTypeConfiguration.class, expectedId),
                withPrefixThat(isEmptyOrNullString()),
                isEnabled(false)
        );
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> disabledTypeConfiguration(@Nullable String expectedId,
                                                                             @Nullable String expectedPrefix) {
        return typeConfigurationWith(expectedId, expectedPrefix, false);
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> enabledTypeConfiguration(@Nullable String expectedId,
                                                                            @Nullable String expectedPrefix) {
        return typeConfigurationWith(expectedId, expectedPrefix, true);
    }


    @Nonnull
    public static Matcher<BranchTypeConfiguration> withDisplayName(@Nullable String expectedId) {
        return BranchTypeMatchers.withDisplayName(BranchTypeConfiguration.class, expectedId);
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> withPrefix(@Nullable String expectedPrefix) {
        return withPrefixThat(is(expectedPrefix));
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> withPrefixThat(@Nonnull Matcher<String> prefixMatcher) {
        return new FeatureMatcher<BranchTypeConfiguration, String>(prefixMatcher, "branch type prefix", "prefix") {

            @Override
            protected String featureValueOf(BranchTypeConfiguration actual) {
                return actual.getPrefix();
            }
        };
    }

    @Nonnull
    public static Matcher<BranchTypeConfiguration> isEnabled(boolean enabled) {
        return MoreMatchers.forPredicate(config -> config.isEnabled() == enabled, "Branch type enabled: " + enabled);
    }
}
