package com.atlassian.stash.internal.build.rest;

import com.atlassian.bitbucket.build.BuildSummary;
import com.atlassian.bitbucket.build.BuildStatus;
import com.atlassian.bitbucket.rest.RestMapEntity;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import static com.atlassian.stash.internal.build.rest.RestBuildStatus.REST_TRANSFORM;

@JsonSerialize
public class RestBuildStats extends RestMapEntity {

    public RestBuildStats(BuildSummary stats) {
        this(stats.getSuccessfulCount(), stats.getInProgressCount(), stats.getFailedCount());
    }

    // Constructor used for the REST documentation
    protected RestBuildStats(int successful, int inProgress, int failed) {
        put("successful", successful);
        put("inProgress", inProgress);
        put("failed", failed);
    }

    protected RestBuildStats include(BuildStatus result) {
        if (result != null) {
            put("results", new RestBuildStatus[]{REST_TRANSFORM.apply(result)});
        }
        return this;
    }

    // REST Documentation Example
    public static final RestBuildStats EXAMPLE = new RestBuildStats(3, 0, 1);

}
