package com.atlassian.bitbucket.rest.pull;

import com.atlassian.bitbucket.pull.PullRequestAction;
import com.atlassian.bitbucket.pull.PullRequestReviewersUpdatedActivity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.user.ExampleDetailedUser;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.util.MoreCollectors;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;
import java.util.Set;

import static com.google.common.collect.Sets.newHashSet;

@JsonSerialize
@JsonSurrogate(PullRequestReviewersUpdatedActivity.class)
public class RestPullRequestReviewersUpdatedActivity extends RestPullRequestActivity {

    public RestPullRequestReviewersUpdatedActivity(PullRequestReviewersUpdatedActivity activity) {
        this(activity.getId(), activity.getCreatedDate(), new RestApplicationUser(activity.getUser()), activity.getAction(),
                restReviewers(activity.getAddedReviewers()), restReviewers(activity.getRemovedReviewers()));
    }

    // Required for REST documentation
    private RestPullRequestReviewersUpdatedActivity(long id, Date createdDate, RestApplicationUser restStashUser,
                                                    PullRequestAction action, Set<RestApplicationUser> addedReviewers,
                                                    Set<RestApplicationUser> removedReviewers) {
        super(id, createdDate, restStashUser, action);

        put("addedReviewers", addedReviewers);
        put("removedReviewers", removedReviewers);
    }

    private static Set<RestApplicationUser> restReviewers(Set<ApplicationUser> reviewers) {
        return reviewers.stream()
                .map(RestApplicationUser.REST_TRANSFORM)
                .collect(MoreCollectors.toImmutableSet());
    }

    // REST Documentation Examples
    public static final RestPullRequestReviewersUpdatedActivity RESPONSE_EXAMPLE = new RestPullRequestReviewersUpdatedActivity(101L,
            new Date(1359065920), RestApplicationUser.RESPONSE_EXAMPLE, PullRequestAction.UPDATED,
            newHashSet(new RestApplicationUser(ExampleDetailedUser.newExample("Tom"))),
            newHashSet(new RestApplicationUser(ExampleDetailedUser.newExample("Steve"))));
}
