package com.atlassian.webdriver.bitbucket.hamcrest;

import com.atlassian.webdriver.bitbucket.element.IdOption;
import com.atlassian.webdriver.bitbucket.element.UserOption;
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;

public class OptionMatchers {

    private OptionMatchers() {
        throw new UnsupportedOperationException("Do not instantiate " + getClass().getSimpleName());
    }

    @Nonnull
    public static Matcher<IdOption> withId(@Nullable String expectedId) {
        return withIdThat(is(expectedId));
    }

    @Nonnull
    public static Matcher<IdOption> withIdThat(@Nonnull Matcher<String> idMatcher) {
        checkNotNull(idMatcher, "idMatcher");

        return new FeatureMatcher<IdOption, String>(idMatcher, "ID that", "id") {
            @Override
            protected String featureValueOf(IdOption actual) {
                return actual.getId();
            }
        };
    }

    @Nonnull
    public static Matcher<UserOption> withUsername(@Nonnull String expectedUsername) {
        checkNotNull(expectedUsername, "expectedUsername");

        return withUsernameThat(is(expectedUsername));
    }

    @Nonnull
    public static Matcher<UserOption> withUsernameThat(@Nonnull Matcher<String> usernameMatcher) {
        checkNotNull(usernameMatcher, "usernameMatcher");

        return new FeatureMatcher<UserOption, String>(usernameMatcher, "username that", "username") {
            @Override
            protected String featureValueOf(UserOption actual) {
                return actual.getUsername();
            }
        };
    }
}
