package com.atlassian.bitbucket.internal.defaultreviewers.rest;

import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;

import static java.util.stream.Collectors.toList;

@JsonSerialize
public class RestDefaultReviewersRequest extends RestMapEntity {
    protected static final String REQUIRED_APPROVALS = "requiredApprovals";
    protected static final String SOURCE_MATCHER = "sourceMatcher";
    protected static final String TARGET_MATCHER = "targetMatcher";
    protected static final String REVIEWERS = "reviewers";

    public Collection<RestApplicationUser> getReviewers() {
        ArrayList<LinkedHashMap> users = (ArrayList<LinkedHashMap>) get(REVIEWERS);
        return users
                .stream()
                .map(RestApplicationUser::valueOf)
                .collect(toList());
    }

    public RestRefMatcher getSourceMatcher() {
        return RestRefMatcher.valueOf(get(SOURCE_MATCHER));
    }

    public RestRefMatcher getTargetMatcher() {
        return RestRefMatcher.valueOf(get(TARGET_MATCHER));
    }

    public int getRequiredApprovals() {
        return getIntProperty(REQUIRED_APPROVALS);
    }

}
