package com.atlassian.bitbucket.hamcrest;

import com.atlassian.bitbucket.comment.DiffCommentAnchor;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

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

import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.is;

/**
 * Generic matchers for {@link DiffCommentAnchor} and its extensions and implementations.
 * Use the {@code anchorClass} parameter to obtain a matcher for the specific required sub-type of
 * {@link DiffCommentAnchor}.
 */
@SuppressWarnings("UnusedParameters")
public class DiffCommentAnchorMatchers {

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

    @Nonnull
    public static <A extends DiffCommentAnchor> Matcher<A> withPath(@Nullable String path, @Nonnull Class<A> anchorClass) {
        return withPathThat(is(path), anchorClass);
    }

    @Nonnull
    public static <A extends DiffCommentAnchor> Matcher<A> withPathThat(@Nonnull Matcher<String> pathMatcher,
                                                                        @Nonnull Class<A> anchorClass) {
        checkNotNull(pathMatcher, "pathMatcher");
        return new FeatureMatcher<A, String>(pathMatcher, "path", "path") {
            @Override
            protected String featureValueOf(A anchor) {
                return anchor.getPath();
            }
        };
    }

    @Nonnull
    public static <A extends DiffCommentAnchor> Matcher<A> withSrcPath(@Nullable String srcPath,
                                                                       @Nonnull Class<A> anchorClass) {
        return withSrcPathThat(is(srcPath), anchorClass);
    }

    @Nonnull
    public static <A extends DiffCommentAnchor> Matcher<A> withSrcPathThat(@Nonnull Matcher<String> srcPathMatcher,
                                                                           @Nonnull Class<A> anchorClass) {
        checkNotNull(srcPathMatcher, "srcPathMatcher");
        return new FeatureMatcher<A, String>(srcPathMatcher, "srcPath", "srcPath") {
            @Override
            protected String featureValueOf(A anchor) {
                return anchor.getSrcPath();
            }
        };
    }
}
