package com.atlassian.stash.internal.pull;

import com.atlassian.analytics.api.annotations.EventName;
import com.atlassian.bitbucket.event.ApplicationEvent;
import com.atlassian.bitbucket.project.Project;
import com.atlassian.bitbucket.pull.PullRequestMergeConfigType;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.event.api.AsynchronousPreferred;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Set;

import static java.util.Objects.requireNonNull;

/**
 * Event that is raised when the pull request merge config is updated.
 */
@AsynchronousPreferred
@EventName("stash.pullrequest.merge.config.updated")
class AnalyticsPullRequestMergeConfigUpdatedEvent extends ApplicationEvent {

    private final String defaultStrategyId;
    private final Set<String> enabledStrategyIds;
    private final Project project;
    private final Repository repository;
    private final String scmId;
    private final PullRequestMergeConfigType type;

    AnalyticsPullRequestMergeConfigUpdatedEvent(@Nonnull Object source, @Nonnull String scmId,
                                                @Nonnull String defaultStrategyId,
                                                @Nonnull Set<String> enabledStrategyIds) {
        this(source, scmId, null, null, PullRequestMergeConfigType.SCM, defaultStrategyId, enabledStrategyIds);
    }

    AnalyticsPullRequestMergeConfigUpdatedEvent(@Nonnull Object source, @Nonnull String scmId,
                                                @Nonnull Project project,
                                                @Nonnull String defaultStrategyId,
                                                @Nonnull Set<String> enabledStrategyIds) {
        this(source, scmId, project, null, PullRequestMergeConfigType.PROJECT, defaultStrategyId, enabledStrategyIds);
    }

    AnalyticsPullRequestMergeConfigUpdatedEvent(@Nonnull Object source, @Nonnull Repository repository,
                                                @Nonnull String defaultStrategyId,
                                                @Nonnull Set<String> enabledStrategyIds) {
        this(source, repository.getScmId(), repository.getProject(), repository,
                PullRequestMergeConfigType.REPOSITORY, defaultStrategyId, enabledStrategyIds);
    }

    private AnalyticsPullRequestMergeConfigUpdatedEvent(@Nonnull Object source, @Nonnull String scmId,
                                                        @Nullable Project project, @Nullable Repository repository,
                                                        @Nonnull PullRequestMergeConfigType type,
                                                        @Nonnull String defaultStrategyId,
                                                        @Nonnull Set<String> enabledStrategyIds) {
        super(source);
        this.project = project;
        this.repository = repository;
        this.defaultStrategyId = requireNonNull(defaultStrategyId, "defaultStrategyId");
        this.enabledStrategyIds = requireNonNull(enabledStrategyIds, "enabledStrategyIds");
        this.scmId = requireNonNull(scmId, "scmId");
        this.type = requireNonNull(type, "type");
    }

    @Nonnull
    public String getDefaultStrategyId() {
        return defaultStrategyId;
    }

    @Nonnull
    public Set<String> getEnabledStrategyIds() {
        return enabledStrategyIds;
    }

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

    @Nullable
    public Repository getRepository() {
        return repository;
    }

    @Nonnull
    public String getScmId() {
        return scmId;
    }

    @Nonnull
    public PullRequestMergeConfigType getType() {
        return type;
    }
}
