package com.atlassian.bitbucket.rest.pull;

import com.atlassian.bitbucket.property.PropertyMap;
import com.atlassian.bitbucket.pull.PullRequestMergeability;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import static com.atlassian.bitbucket.rest.property.RestPropertySupport.PROPERTIES;

@JsonSerialize
@JsonSurrogate(PullRequestMergeability.class)
public class RestPullRequestMergeability extends RestMapEntity {

    public RestPullRequestMergeability(PullRequestMergeability result) {
        this(result.canMerge(), result.isConflicted(),

                Collections2.transform(result.getVetoes(), RestPullRequestMergeVeto.REST_TRANSFORM::apply),
                result.getProperties());
    }

    // Required for REST documentation
    private RestPullRequestMergeability(boolean canMerge, boolean conflicted, Iterable<RestPullRequestMergeVeto> vetoes,
                                        PropertyMap properties) {
        put("canMerge", canMerge);
        put("conflicted", conflicted);
        put("vetoes", ImmutableList.copyOf(vetoes));

        putIfNotEmpty(PROPERTIES, properties);
    }

    // REST Documentation Examples

    public static final RestPullRequestMergeability RESPONSE_EXAMPLE = new RestPullRequestMergeability(false, false,
            ImmutableList.of(RestPullRequestMergeVeto.RESPONSE_EXAMPLE), PropertyMap.EMPTY);
}
