package com.atlassian.stash.internal.repository.sync.rest;

import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.scm.git.command.merge.conflict.AbstractPathGitMergeConflict;
import com.atlassian.bitbucket.scm.git.command.merge.conflict.DivergentRenameGitMergeConflict;
import com.atlassian.bitbucket.scm.git.command.merge.conflict.GitMergeConflict;
import com.google.common.base.Function;

/**
 * @since 2.6
 */
public class RestMergeConflict extends RestMapEntity {

    public static final Function<GitMergeConflict, RestMergeConflict> REST_TRANSFORM =
            new Function<GitMergeConflict, RestMergeConflict>() {

                @Override
                public RestMergeConflict apply(GitMergeConflict conflict) {
                    return new RestMergeConflict(conflict);
                }
            };

    public RestMergeConflict(GitMergeConflict conflict) {
        put("message", conflict.getMessage());
        put("ourChange", conflict.getOurChange());
        put("theirChange", conflict.getTheirChange());
        put("type", conflict.getType());

        //The conflict instance can provide a huge amount of data, but for our uses here very little of it is
        //really important. The primary point of interest is the path where the conflict happened, so that is
        //the only piece that we bother to get. For more details, we could pass the conflict through a visitor
        if (conflict instanceof AbstractPathGitMergeConflict) {
            put("path", ((AbstractPathGitMergeConflict) conflict).getPath());
        } else if (conflict instanceof DivergentRenameGitMergeConflict) {
            put("path", ((DivergentRenameGitMergeConflict) conflict).getSrcPath());
        }
    }
}
