package com.atlassian.bitbucket.internal.importer.event;

import com.atlassian.analytics.api.annotations.EventName;
import com.atlassian.bitbucket.event.ApplicationEvent;
import com.atlassian.bitbucket.internal.importer.SourceType;
import com.atlassian.bitbucket.internal.importer.repository.ImportJob;
import com.atlassian.bitbucket.internal.importer.repository.ImportTaskFailureType;
import com.atlassian.bitbucket.internal.importer.repository.ImportTaskState;
import com.atlassian.event.api.AsynchronousPreferred;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

/**
 * This event keeps track of {@link RepositoryImportTaskStateChangedEvent}s being created and modified. It contains the
 * jobId so that it can be linked with each ({@link ImportJobCreatedEvent}). At this stage, we have already dropped the
 * {@link SourceType} so we are unable to know the external source for this {@link ImportJob}.
 */
@AsynchronousPreferred
@EventName("stash.importer.task.changed")
public class RepositoryImportTaskStateChangedEvent extends ApplicationEvent {
    private final ImportTaskFailureType importTaskFailureType;
    private final ImportTaskState importTaskState;
    private final long jobId;
    private final int repoId;
    private final long taskId;

    public RepositoryImportTaskStateChangedEvent(@Nonnull Object source,
                                                 @Nonnull ImportTaskState importTaskState,
                                                 @Nullable ImportTaskFailureType importTaskFailureType,
                                                 long jobId,
                                                 int repositoryId,
                                                 long taskId) {
        super(source);
        this.importTaskFailureType = importTaskFailureType;
        this.importTaskState = requireNonNull(importTaskState);
        this.jobId = jobId;
        this.repoId = repositoryId;
        this.taskId = taskId;
    }

    @Nullable
    public ImportTaskFailureType getImportTaskFailureType() {
        return importTaskFailureType;
    }

    @Nonnull
    public ImportTaskState getImportTaskState() {
        return importTaskState;
    }

    public long getJobId() {
        return jobId;
    }

    public int getRepoId() {
        return repoId;
    }

    public long getTaskId() {
        return taskId;
    }
}
