package com.atlassian.bitbucket.rest.content;

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

    public static final Function<DiffLine, RestDiffLine> REST_TRANSFORM = RestDiffLine::new;

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

    public RestDiffLine(DiffLine line) {
        this(line.getSource(), line.getDestination(), line.getLine(), line.isConflicting(), line.getConflictMarker(), line.getCommentIds(), line.isTruncated());
    }

    // Required for REST documentation
    private RestDiffLine(int source, int destination, String line, boolean conflicted, ConflictMarker conflictMarker, List<Long> commentIds, boolean truncated) {
        putIfNotNull("destination", destination);
        putIfNotNull("source", source);
        putIfNotNull("line", line);
        putIfNotNull("truncated", truncated);
        if (conflicted) {
            put("conflictMarker", conflictMarker);
        }
        putIfNotEmpty("commentIds", commentIds);
    }

    // REST Documentation Examples

    public static final RestDiffLine SOURCE_EXAMPLE = new RestDiffLine(1, 1, "import sys", false, null, null, false);
    public static final RestDiffLine DESTINATION_EXAMPLE = new RestDiffLine(2, 1, "import re", false, null, null, false);
    public static final RestDiffLine DESTINATION_EXAMPLE_WITH_COMMENTS = new RestDiffLine(2, 1, "import re", true, ConflictMarker.OURS, Lists.newArrayList(1L), false);
    public static final RestDiffLine DESTINATION_2_EXAMPLE = new RestDiffLine(3, 2, "import os", false, null, null, false);

}
