package com.atlassian.bitbucket.rest.commit;

import com.atlassian.bitbucket.commit.CommitDiscussionParticipant;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import com.atlassian.bitbucket.rest.user.RestPerson;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.function.Function;

@JsonSerialize
@JsonSurrogate(CommitDiscussionParticipant.class)
public class RestCommitDiscussionParticipant extends RestMapEntity {

    public static final Function<CommitDiscussionParticipant, RestCommitDiscussionParticipant> REST_TRANSFORM =
            RestCommitDiscussionParticipant::new;

    public static final String USER = "user";

    @SuppressWarnings("unused") //Required for Jersey
    public RestCommitDiscussionParticipant() {
    }

    public RestCommitDiscussionParticipant(CommitDiscussionParticipant participant) {
        this(new RestApplicationUser(participant.getUser()));
    }

    private RestCommitDiscussionParticipant(Map<String, Object> values) {
        super(values);
    }

    // Required for REST documentation
    private RestCommitDiscussionParticipant(RestPerson user) {
        putIfNotNull(USER, user);
    }

    public RestApplicationUser getUser() {
        return RestApplicationUser.valueOf(get(USER));
    }

    @Nullable
    @SuppressWarnings("unchecked")
    public static RestCommitDiscussionParticipant valueOf(@Nullable Object value) {
        if (value instanceof RestCommitDiscussionParticipant) {
            return (RestCommitDiscussionParticipant) value;
        } else if (value instanceof Map) {
            return new RestCommitDiscussionParticipant((Map<String, Object>) value);
        } else {
            return null;
        }
    }

    // REST Documentation Examples
    public static final RestCommitDiscussionParticipant REQUEST_EXAMPLE_USER_ONLY =
            new RestCommitDiscussionParticipant(RestApplicationUser.EXAMPLE_NAME_ONLY);
}
