package com.atlassian.stash.experimental.user;

import com.atlassian.annotations.ExperimentalApi;
import com.atlassian.bitbucket.project.Project;
import com.google.common.base.Objects;

import javax.annotation.Nonnull;

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

/**
 * Used to query a specific user's permission on a specific project
 *
 * WARNING: this interface is experimental and is subject to change between minor versions of Stash up to and including
 * removal of this interface or parts thereof. It is not for consumption by non-Atlassian plugins and no consideration
 * will be given to nor guarantees made for its compatibility between releases. Basically, don't use it unless
 * you're a Stash developer.
 *
 * @since 2.12
 */
@ExperimentalApi
public class ProjectPermissionRequest extends ResourcePermissionRequest {
    private final Project project;

    private ProjectPermissionRequest(@Nonnull Builder builder) {
        super(builder);
        this.project = checkNotNull(builder.project, "project");
    }

    @Nonnull
    public Project getProject() {
        return project;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getUser(), project);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final ProjectPermissionRequest other = (ProjectPermissionRequest) obj;
        return Objects.equal(this.getUser(), other.getUser()) && Objects.equal(this.project, other.project);
    }

    public static class Builder extends AbstractBuilder<Builder, ProjectPermissionRequest> {
        private Project project;

        public Builder() {
        }

        public Builder(@Nonnull ProjectPermissionRequest request) {
            super(request);
            project = request.project;
        }

        public Builder project(@Nonnull Project project) {
            this.project = project;
            return self();
        }

        @Override
        public ProjectPermissionRequest build() {
            return new ProjectPermissionRequest(this);
        }

        @Override
        protected Builder self() {
            return this;
        }
    }
}
