package com.atlassian.bitbucket.task;

import com.atlassian.bitbucket.hamcrest.SimpleTypeSafeMatcher;
import com.atlassian.stash.internal.pull.InternalPullRequest;
import com.atlassian.stash.internal.task.InternalTaskSearchRequest;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestTaskSearchRequest;
import org.hamcrest.Description;
import org.hamcrest.Matcher;

import java.util.Objects;

public class TaskMatchers {

    public static Matcher<PullRequestTaskSearchRequest> hasContext(final PullRequest pullRequest) {
        return new SimpleTypeSafeMatcher<PullRequestTaskSearchRequest>() {

            @Override
            public void describeTo(Description description) {
                description.appendText("PullRequestTaskSearchRequest with context PR=")
                           .appendValue(pullRequest);
            }

            @Override
            protected boolean matchesSafely(PullRequestTaskSearchRequest item) {
                return pullRequest.getId() == item.getPullRequestId() &&
                        pullRequest.getToRef().getRepository().getId() == item.getRepositoryId();
            }
        };
    }

    public static Matcher<InternalTaskSearchRequest> hasInternalContext(final InternalPullRequest pullRequest) {
        return new SimpleTypeSafeMatcher<InternalTaskSearchRequest>() {

            @Override
            public void describeTo(Description description) {
                description.appendText("PullRequestTaskSearchRequest with context PR=")
                        .appendValue(pullRequest);
            }

            @Override
            protected boolean matchesSafely(InternalTaskSearchRequest item) {
                return Objects.equals(pullRequest.getId(), item.getContext().getId()) &&
                        Objects.equals(pullRequest.getScopeRepository().getId(), item.getContext().getScopeRepository().getId());
            }
        };
    }
}
