package com.atlassian.bitbucket.rest.content;

import com.atlassian.bitbucket.content.DiffHunk;
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(DiffHunk.class)
public class RestDiffHunk extends RestMapEntity {

    public static final Function<DiffHunk, RestDiffHunk> REST_TRANSFORM = RestDiffHunk::new;

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

    public RestDiffHunk(DiffHunk hunk) {
        this(hunk.getSourceLine(), hunk.getSourceSpan(), hunk.getDestinationLine(), hunk.getDestinationSpan(), transform(hunk.getSegments(), RestDiffSegment.REST_TRANSFORM), hunk.isTruncated());
    }

    // Required for REST documentation
    private RestDiffHunk(int sourceLine, int sourceSpan, int destinationLine, int destinationSpan, List<RestDiffSegment> segments, boolean isTruncated) {
        putIfNotNull("sourceLine", sourceLine);
        putIfNotNull("sourceSpan", sourceSpan);
        putIfNotNull("destinationLine", destinationLine);
        putIfNotNull("destinationSpan", destinationSpan);
        putIfNotEmpty("segments", segments);
        putIfNotNull("truncated", isTruncated);
    }

    // REST Documentation Examples

    public static final RestDiffHunk HUNKY_EXAMPLE = new RestDiffHunk(1, 1, 1, 2, Lists.newArrayList(RestDiffSegment.REMOVED_SOURCE_EXAMPLE, RestDiffSegment.ADDED_DEST_EXAMPLE), false);
    public static final RestDiffHunk HUNKY_EXAMPLE_COMMENTED = new RestDiffHunk(1, 1, 1, 2, Lists.newArrayList(RestDiffSegment.REMOVED_SOURCE_EXAMPLE, RestDiffSegment.ADDED_DEST_EXAMPLE_COMMENTED), false);
}
