package com.atlassian.bitbucket.rest.content;

import com.atlassian.bitbucket.content.DiffSegment;
import com.atlassian.bitbucket.content.DiffSegmentType;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.google.common.collect.Lists;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.List;
import java.util.function.Function;

@JsonSerialize
@JsonSurrogate(DiffSegment.class)
public class RestDiffSegment extends RestMapEntity {

    public static final Function<DiffSegment, RestDiffSegment> REST_TRANSFORM = RestDiffSegment::new;

    @SuppressWarnings("unused") //Required for Jersey
    public RestDiffSegment() {
    }

    public RestDiffSegment(DiffSegment segment) {
        this(segment.getType(), transform(segment.getLines(), RestDiffLine.REST_TRANSFORM), segment.isTruncated());
    }

    // Required for REST documentation
    private RestDiffSegment(DiffSegmentType type, List<RestDiffLine> lines, boolean truncated) {
        putIfNotNull("type", type);
        putIfNotNull("lines", lines);
        putIfNotNull("truncated", truncated);
    }

    // REST Documentation Examples

    public static final RestDiffSegment REMOVED_SOURCE_EXAMPLE = new RestDiffSegment(DiffSegmentType.REMOVED, Lists.newArrayList(RestDiffLine.SOURCE_EXAMPLE), false);
    public static final RestDiffSegment ADDED_DEST_EXAMPLE = new RestDiffSegment(DiffSegmentType.ADDED, Lists.newArrayList(RestDiffLine.DESTINATION_EXAMPLE, RestDiffLine.DESTINATION_2_EXAMPLE), false);
    public static final RestDiffSegment ADDED_DEST_EXAMPLE_COMMENTED = new RestDiffSegment(DiffSegmentType.ADDED, Lists.newArrayList(RestDiffLine.DESTINATION_EXAMPLE_WITH_COMMENTS, RestDiffLine.DESTINATION_2_EXAMPLE), false);
}
