package com.atlassian.stash.internal.notification.pull.activity;

import com.google.common.collect.ImmutableMap;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Map;

class CommentData extends Data {

    private static final String COMMENT_KEY = "comment";

    private final long commentId;

    public CommentData(long commentId) {
        this.commentId = commentId;
    }

    public long getCommentId() {
        return commentId;
    }

    @Override
    public String encode() throws IOException {
        return OBJECT_MAPPER.write(ImmutableMap.of(COMMENT_KEY, commentId));
    }

    @Nullable
    static CommentData tryDecode(Map<String, ?> map) {
        Object comment = map.get(COMMENT_KEY);
        if (comment instanceof Number) {
            return new CommentData(((Number) comment).longValue());
        }
        return null;
    }
}
