package com.atlassian.bitbucket.mesh.git.pull;

import com.atlassian.bitbucket.mesh.repository.Repository;

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

import static java.util.Objects.requireNonNull;

public class PullRequestRescope {

    private final Repository fromRepository;
    private final long id;
    private final String newFromCommit;
    private final String newToCommit;
    private final String oldFromCommit;
    private final String oldToCommit;
    private final Repository toRepository;

    private PullRequestRescope(Builder builder) {
        fromRepository = builder.fromRepository;
        id = builder.id;
        newFromCommit = builder.newFromCommit;
        newToCommit = builder.newToCommit;
        oldFromCommit = requireNonNull(builder.oldFromCommit, "oldFromCommit");
        oldToCommit = requireNonNull(builder.oldToCommit, "oldToCommit");
        toRepository = builder.toRepository;
    }

    @Nonnull
    public Repository getFromRepository() {
        return fromRepository;
    }

    public long getId() {
        return id;
    }

    /**
     * @return the new commit hash for the pull request 'from' ref; {@code null} if the branch was deleted
     */
    @Nullable
    public String getNewFromCommit() {
        return newFromCommit;
    }

    /**
     * @return the new commit hash for the pull request 'to' ref; {@code null} if the branch was deleted
     */
    @Nullable
    public String getNewToCommit() {
        return newToCommit;
    }

    /**
     * @return the old commit hash for the pull request 'from' ref
     */
    @Nonnull
    public String getOldFromCommit() {
        return oldFromCommit;
    }

    /**
     * @return the old commit hash for the pull request 'to' ref
     */
    @Nonnull
    public String getOldToCommit() {
        return oldToCommit;
    }

    @Nonnull
    public Repository getToRepository() {
        return toRepository;
    }

    public static class Builder {

        private final Repository fromRepository;
        private final long id;
        private final Repository toRepository;

        private String oldFromCommit;
        private String oldToCommit;
        private String newFromCommit;
        private String newToCommit;

        public Builder(long id, @Nonnull Repository fromRepository, @Nonnull Repository toRepository) {
            this.id = id;
            this.fromRepository = requireNonNull(fromRepository, "fromRepository");
            this.toRepository = requireNonNull(toRepository, "toRepository");
        }

        @Nonnull
        public PullRequestRescope build() {
            return new PullRequestRescope(this);
        }

        @Nonnull
        public Builder newHashes(@Nullable String fromHash, @Nullable String toHash) {
            newFromCommit = fromHash;
            newToCommit = toHash;
            return this;
        }

        @Nonnull
        public Builder oldHashes(@Nonnull String fromHash, @Nonnull String toHash) {
            oldFromCommit = requireNonNull(fromHash, "fromHash");
            oldToCommit = requireNonNull(toHash, "toHash");
            return this;
        }
    }
}
