package com.atlassian.bitbucket.rest.pull;

import com.atlassian.bitbucket.rest.comment.RestDiffCommentAnchor;
import com.atlassian.bitbucket.content.DiffFileType;
import com.atlassian.bitbucket.content.DiffSegmentType;
import com.atlassian.bitbucket.pull.PullRequestDiffCommentAnchor;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.function.Function;

@JsonSerialize
@JsonSurrogate(PullRequestDiffCommentAnchor.class)
public class RestPullRequestDiffCommentAnchor extends RestDiffCommentAnchor {

    private static final String ORPHANED = "orphaned";

    public static final Function<PullRequestDiffCommentAnchor, RestPullRequestDiffCommentAnchor> REST_TRANSFORM =
            RestPullRequestDiffCommentAnchor::new;

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

    public RestPullRequestDiffCommentAnchor(PullRequestDiffCommentAnchor anchor) {
        super(anchor);
        put(ORPHANED, anchor.isOrphaned());
    }

    // Required for REST documentation
    private RestPullRequestDiffCommentAnchor(String fromHash, String toHash, String path, String srcPath, Integer line,
                                             DiffSegmentType lineType, DiffFileType fileType, boolean orphaned) {
        super(fromHash, toHash, path, srcPath, line, lineType, fileType);
        put(ORPHANED, orphaned);
    }

    // Required for REST documentation
    private RestPullRequestDiffCommentAnchor(String fromHash, String toHash, String path, String srcPath,
                                             boolean orphaned) {
        this(fromHash, toHash, path, srcPath, null, null, null, orphaned);
    }

    // REST Documentation Examples

    public static final RestPullRequestDiffCommentAnchor LINE_COMMENT_RESPONSE = new RestPullRequestDiffCommentAnchor(
            "8d51122def5632836d1cb1026e879069e10a1e13",
            "4b12658b6cd2bae179759c21537b4ec6627ff83b",
            "path/to/file",
            "path/to/file",
            1,
            DiffSegmentType.CONTEXT,
            DiffFileType.FROM,
            false
    );

    public static final RestPullRequestDiffCommentAnchor FILE_COMMENT_RESPONSE = new RestPullRequestDiffCommentAnchor(
            "8d51122def5632836d1cb1026e879069e10a1e13",
            "4b12658b6cd2bae179759c21537b4ec6627ff83b",
            "path/to/file",
            "path/to/file",
            false
    );
}
