package com.atlassian.bitbucket.content;

import com.google.common.collect.ImmutableList;
import org.apache.commons.collections.CollectionUtils;

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

import static java.util.Objects.requireNonNull;

public class SimpleDiffLine implements DiffLine {

    private final List<Long> commentIds;
    private final ConflictMarker conflictMarker;
    private final int destination;
    private final String line;
    private final int source;
    private final boolean truncated;

    private SimpleDiffLine(Builder builder) {
        this.commentIds = requireNonNull(builder.commentIds.build(), "commentIds");
        this.conflictMarker = builder.conflictMarker;
        this.destination = builder.destination;
        this.line = builder.line;
        this.source = builder.source;
        this.truncated = builder.truncated;
    }

    @Nonnull
    @Override
    public List<Long> getCommentIds() {
        return commentIds;
    }

    @Override
    public ConflictMarker getConflictMarker() {
        return conflictMarker;
    }

    @Override
    public int getDestination() {
        return destination;
    }

    @Nonnull
    @Override
    public String getLine() {
        return line;
    }

    @Override
    public int getSource() {
        return source;
    }

    @Override
    public boolean isConflicting() {
        return conflictMarker != null;
    }

    @Override
    public boolean isTruncated() {
        return truncated;
    }

    public static final class Builder {

        private ImmutableList.Builder<Long> commentIds;
        private ConflictMarker conflictMarker;
        private int destination;
        private String line;
        private int source;
        private boolean truncated;

        public Builder() {
            commentIds = ImmutableList.builder();
        }

        public Builder(DiffLine diffLine) {
            this();

            destination = diffLine.getDestination();
            line = diffLine.getLine();
            source = diffLine.getSource();
            truncated = diffLine.isTruncated();

            List<Long> ids = diffLine.getCommentIds();
            if (CollectionUtils.isNotEmpty(ids)) {
                commentIds.addAll(ids);
            }
        }

        public SimpleDiffLine build() {
            return new SimpleDiffLine(this);
        }

        public Builder commentId(long value) {
            commentIds.add(value);

            return this;
        }

        public Builder commentIds(long value, long... values) {
            commentIds.add(value);
            for (long v : values) {
                commentIds.add(v);
            }

            return this;
        }

        public Builder commentIds(Iterable<Long> value) {
            if (value != null) {
                commentIds.addAll(value);
            }

            return this;
        }

        public Builder conflictMarker(ConflictMarker value) {
            conflictMarker = value;

            return this;
        }

        public Builder destination(int value) {
            destination = value;

            return this;
        }

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

            return this;
        }

        public Builder source(int value) {
            source = value;

            return this;
        }

        public Builder truncated(boolean value) {
            truncated = value;

            return this;
        }
    }
}
