package com.atlassian.stash.internal.user;

import com.atlassian.bitbucket.project.Project;
import com.atlassian.stash.experimental.user.ProjectPermission;
import com.atlassian.stash.internal.HibernateUtils;
import com.atlassian.stash.internal.Initializable;
import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;

import javax.annotation.Nonnull;

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

public class InternalGrantedProjectPermission extends InternalPermittedEntity implements ProjectPermission, Initializable {

    private Project project;

    public InternalGrantedProjectPermission(@Nonnull Project project, int weight) {
        super(weight);
        this.project = checkNotNull(project, "project");
    }

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

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

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

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("project", project)
                .add("permission", permission)
                .toString();
    }

    @Override
    public void initialize() {
        project = HibernateUtils.initialize(getProject());
    }
}
