package com.atlassian.bitbucket.scm;

import com.atlassian.bitbucket.content.DiffContext;
import com.atlassian.bitbucket.content.DiffWhitespace;
import org.apache.commons.lang3.StringUtils;

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

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

public class DiffCommandParameters extends AbstractDiffCommandParameters {

    private final String sinceId;
    private final String untilId;

    private DiffCommandParameters(int maxLines, int maxLineLength, int contextLines, DiffWhitespace whitespace,
                                  Set<String> paths, String untilId, String sinceId) {
        super(maxLineLength, maxLines, contextLines, whitespace, paths);
        this.sinceId = sinceId;
        this.untilId = untilId;
    }

    @Nullable
    public String getSinceId() {
        return sinceId;
    }

    @Nonnull
    public String getUntilId() {
        return untilId;
    }

    /**
     * Assembles a {@link DiffContext} from these parameters.
     *
     * @return a {@link DiffContext} describing these parameters
     */
    @Nonnull
    public DiffContext toContext() {
        return toContext(new DiffContext.Builder(getSinceId(), getUntilId()));
    }

    public static class Builder extends AbstractBuilder<Builder> {

        private String sinceId;
        private String untilId;

        public Builder() {
        }

        public DiffCommandParameters build() {
            if (StringUtils.isBlank(untilId)) {
                throw new IllegalStateException("An \"until\" ID is required to build a diff");
            }

            return new DiffCommandParameters(maxLines, maxLineLength, contextLines, whitespace, paths.build(), untilId, sinceId);
        }

        public Builder sinceId(String value) {
            sinceId = value;

            return this;
        }

        public Builder untilId(String value) {
            untilId = checkNotNull(value, "untilId");

            return this;
        }

        @Override
        protected Builder self() {
            return this;
        }
    }
}
