package com.atlassian.bitbucket.hamcrest;

import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.permission.PermittedGroup;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

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

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;

public class PermittedGroupMatchers {

    @Nonnull
    public static Matcher<PermittedGroup> permittedGroup(@Nullable String expectedGroup,
                                                         @Nullable Permission expectedPermission) {
        return allOf(withGroup(expectedGroup), withPermission(expectedPermission));
    }

    @Nonnull
    public static Matcher<PermittedGroup> withGroup(@Nullable String expectedGroup) {
        return withGroupThat(is(expectedGroup));
    }

    @Nonnull
    public static Matcher<PermittedGroup> withGroupThat(@Nonnull Matcher<String> groupMatcher) {
        return new FeatureMatcher<PermittedGroup, String>(groupMatcher, "group that", "group") {
            @Override
            protected String featureValueOf(PermittedGroup actual) {
                return actual.getGroup();
            }
        };
    }

    @Nonnull
    public static Matcher<PermittedGroup> withPermission(@Nullable Permission expectedPermission) {
        return withPermissionThat(is(expectedPermission));
    }

    @Nonnull
    public static Matcher<PermittedGroup> withPermissionThat(@Nonnull Matcher<Permission> permissionMatcher) {
        return new FeatureMatcher<PermittedGroup, Permission>(permissionMatcher, "permission that", "permission") {
            @Override
            protected Permission featureValueOf(PermittedGroup actual) {
                return actual.getPermission();
            }
        };
    }
}
