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;

public class SimpleDiffSegment implements DiffSegment {

    private final List<DiffLine> lines;
    private final boolean truncated;
    private final DiffSegmentType type;

    private SimpleDiffSegment(DiffSegmentType type, boolean truncated, List<DiffLine> lines) {
        this.type = type;
        this.lines = lines;
        this.truncated = truncated;
    }

    @Nonnull
    @Override
    public List<DiffLine> getLines() {
        return lines;
    }

    @Nonnull
    @Override
    public DiffSegmentType getType() {
        return type;
    }

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

    public static final class Builder {

        private ImmutableList.Builder<DiffLine> lines;
        private boolean truncated;
        private DiffSegmentType type;

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

        public Builder(DiffSegment segment) {
            this();

            truncated = segment.isTruncated();
            type = segment.getType();

            List<DiffLine> l = segment.getLines();
            if (CollectionUtils.isNotEmpty(l)) {
                lines.addAll(l);
            }
        }

        public SimpleDiffSegment build() {
            return new SimpleDiffSegment(type, truncated, lines.build());
        }

        public Builder line(DiffLine value) {
            lines.add(value);

            return this;
        }

        public Builder lines(DiffLine value, DiffLine... values) {
            lines.add(value)
                    .add(values);

            return this;
        }

        public Builder lines(Iterable<DiffLine> value) {
            lines.addAll(value);

            return this;
        }

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

            return this;
        }

        public Builder type(DiffSegmentType value) {
            type = value;

            return this;
        }
    }
}
