package com.atlassian.stash.internal.repository.ref.restriction.analytics;

import com.atlassian.bitbucket.event.repository.RepositoryEvent;
import com.atlassian.bitbucket.event.annotation.TransactionAware;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.ref.restriction.RefRestriction;
import com.atlassian.bitbucket.repository.ref.restriction.RefRestrictionType;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;

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

@TransactionAware
public abstract class RefRestrictionAnalyticsEvent extends RepositoryEvent {

    private final List<String> affectedGroups;
    private final List<ApplicationUser> affectedUsers;
    private final RefRestriction restriction;

    public RefRestrictionAnalyticsEvent(@Nonnull Object source,
                                        @Nonnull Repository repository,
                                        @Nonnull RefRestriction restriction,
                                        @Nonnull Collection<ApplicationUser> affectedUsers,
                                        @Nonnull Collection<String> affectedGroups) {
        super(source, repository);

        this.affectedGroups = ImmutableList.copyOf(checkNotNull(affectedGroups, "affectedGroups"));
        this.affectedUsers = ImmutableList.copyOf(checkNotNull(affectedUsers, "affectedUsers"));
        this.restriction = checkNotNull(restriction, "restriction");
    }

    @SuppressWarnings("unused")
    @Nullable
    public Integer getAffectedGroupCount() {
        if (affectedGroups.size() > 0) {
            return affectedGroups.size();
        }

        return null;
    }

    @SuppressWarnings("unused")
    @Nullable
    public Integer getAffectedUserCount() {
        if (affectedUsers.size() > 0) {
            return affectedUsers.size();
        }

        return null;
    }

    @SuppressWarnings("unused")
    @Nonnull
    public String getMatcherType() {
        return restriction.getMatcher().getType().getDisplayId();
    }

    @SuppressWarnings("unused")
    @Nonnull
    public RefRestrictionType getRestrictionType() {
        return restriction.getType();
    }

    @SuppressWarnings("unused")
    @Nonnull
    public Integer getRestrictionId() {
        return restriction.getId();
    }
}
