package com.atlassian.stash.internal.pull.rescope;

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.pull.BulkRescopeCommandParameters;
import com.atlassian.bitbucket.scm.pull.PullRequestRescope;
import com.atlassian.bitbucket.user.ApplicationUser;

import javax.annotation.Nonnull;
import java.util.*;

import static java.util.Objects.requireNonNull;

/**
 * Implementation of {@link PullRequestRescope} that represents all details around the details, including the
 * user, date and outcome of the SCM
 * {@link com.atlassian.stash.internal.scm.InternalScmService#bulkRescope(BulkRescopeCommandParameters) analysis}
 *
 * @since 4.5
 */
public class SimplePullRequestRescope implements PullRequestRescope {

    private final Date date;
    private final String newFromHash;
    private final String newToHash;
    private final String oldFromHash;
    private final String oldToHash;
    private final SimpleMinimalPullRequest pullRequest;
    private final ApplicationUser user;

    private RescopeOutcome outcome;

    public SimplePullRequestRescope(@Nonnull SimpleMinimalPullRequest pullRequest, @Nonnull Date date,
                                    ApplicationUser user, @Nonnull String oldFromHash, @Nonnull String oldToHash,
                                    String newFromHash, String newToHash) {
        this.date = requireNonNull(date, "date");
        this.pullRequest = requireNonNull(pullRequest, "pullRequest");
        this.newFromHash = newFromHash;
        this.newToHash = newToHash;
        this.oldFromHash = requireNonNull(oldFromHash, "oldFromHash");
        this.oldToHash = requireNonNull(oldToHash, "oldToHash");
        this.user = user;

        if (newToHash == null) {
            outcome = new DeclineOutcome();
        }
    }

    @Nonnull
    public Date getDate() {
        return date;
    }

    @Nonnull
    @Override
    public Repository getFromRepository() {
        return pullRequest.getFromRef().getRepository();
    }

    @Override
    public String getNewFromHash() {
        return newFromHash;
    }

    @Override
    public String getNewToHash() {
        return newToHash;
    }

    @Nonnull
    @Override
    public String getOldFromHash() {
        return oldFromHash;
    }

    @Nonnull
    @Override
    public String getOldToHash() {
        return oldToHash;
    }

    public RescopeOutcome getOutcome() {
        return outcome;
    }

    @Nonnull
    public SimpleMinimalPullRequest getPullRequest() {
        return pullRequest;
    }

    @Nonnull
    @Override
    public Repository getToRepository() {
        return pullRequest.getToRef().getRepository();
    }

    @Nonnull
    public ApplicationUser getUser() {
        return user;
    }

    public void setOutcome(RescopeOutcome outcome) {
        this.outcome = outcome;
    }

    public String toString() {
        return "PullRequestRescope {pullRequest: " + pullRequest + ", user: " + user +
                ", from: " + oldFromHash + "->" + newFromHash +
                ", to: " + oldToHash + "->" + newToHash + "}";
    }

}
