package com.atlassian.stash.internal.user;

import com.atlassian.bitbucket.permission.Permission;

import javax.annotation.Nonnull;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
import java.util.Objects;

@Entity
@Table(name = InternalGlobalPermission.TABLE, indexes = {
        @Index(name = "idx_global_permission_user", columnList = "user_id"),
        @Index(name = "idx_global_permission_group", columnList = "group_name")
})
public class InternalGlobalPermission extends InternalGrantedPermission {

    static final String TABLE = "sta_global_permission";

    @SuppressWarnings("unused") // for hibernate
    protected InternalGlobalPermission() {
        super();
    }

    private InternalGlobalPermission(Long id, Permission permission, String group,
                                     InternalApplicationUser user) {
        super(id, permission, group, user);
    }

    @Override
    public void accept(@Nonnull InternalGrantedPermissionVisitor visitor) {
        visitor.visit(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o instanceof InternalGlobalPermission) {
            InternalGlobalPermission that = (InternalGlobalPermission) o;

            return Objects.equals(getPermission(), that.getPermission()) &&
                    Objects.equals(getGroup(), that.getGroup()) &&
                    Objects.equals(getId(), that.getId()) &&
                    Objects.equals(getUser(), that.getUser());
        }

        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(getPermission(), getGroup(), getId(), getUser());
    }
    
    public static final class Builder extends AbstractBuilder<Builder> {
        
        public Builder() {
            super();
        }
        
        public Builder(InternalGlobalPermission source) {
            super(source);
        }

        public InternalGlobalPermission build() {
            return new InternalGlobalPermission(id, permission, group, user);
        }

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