package com.atlassian.stash.internal;

import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.permission.Permission;
import com.atlassian.bitbucket.util.PageRequest;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;

import java.util.Collection;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public final class CommonValidations {

    public static void validatePermission(Permission permission) {
        checkNotNull(permission, "Permission is null");
    }

    public static void validateGrantablePermission(Permission permission) {
        validatePermission(permission);
        checkArgument(permission.isGrantable(), "Permission is not a grantable permission");
    }

    public static void validateGlobalPermission(Permission permission) {
        validatePermission(permission);
        checkArgument(permission.isGlobal(), "Permission is not a global permission");
    }

    public static void validateProjectPermission(Permission permission) {
        validatePermission(permission);
        checkArgument(permission.isResource(Project.class), "Permission is not a project permission");
    }

    public static void validateRepositoryPermission(Permission permission) {
        validatePermission(permission);
        checkArgument(permission.isResource(Repository.class), "Permission is not a repository permission");
    }

    public static void validateResourcePermission(Permission permission) {
        validatePermission(permission);
        checkArgument(permission.isResource(), "Permission is not a resource-based permission");
    }

    public static void validateUser(ApplicationUser user) {
        checkNotNull(user, "User is null");
    }

    public static void validateUser(String username) {
        checkArgument(NOT_NULL_OR_EMPTY.apply(username), "Username is empty");
    }

    public static void validateGroup(String group) {
        checkArgument(NOT_NULL_OR_EMPTY.apply(group), "Group is empty");
    }

    public static void validateGroups(Collection<String> groups) {
        checkArgument(groups != null, "Groups is null");
        checkArgument(Iterables.all(groups, NOT_NULL_OR_EMPTY), "Groups has empty groups");
    }

    public static void validateProject(Project project) {
        checkNotNull(project, "Project is null");
    }

    public static void validateRepository(Repository repository) {
        checkNotNull(repository, "Repository is null");
    }

    public static void validatePageRequest(PageRequest request) {
        checkNotNull(request, "PageRequest is null");
    }

    private static final Predicate<String> NOT_NULL_OR_EMPTY = new Predicate<String>() {
        public boolean apply(String s) {
            return !Strings.isNullOrEmpty(s);
        }
    };

}
