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

import javax.annotation.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PullRequestBatchId {

    private static final Pattern PATTERN = Pattern.compile("(\\d+),(\\d+)");

    private final int repositoryId;
    private final long pullRequestId;

    private PullRequestBatchId(int repositoryId, long pullRequestId) {
        this.repositoryId = repositoryId;
        this.pullRequestId = pullRequestId;
    }

    public static PullRequestBatchId of(int repositoryId, long pullRequestId) {
        return new PullRequestBatchId(repositoryId, pullRequestId);
    }

    @Nullable
    public static PullRequestBatchId decode(String batchId) {
        Matcher matcher = PATTERN.matcher(batchId);
        if (matcher.matches()) {
            int repositoryId = Integer.parseInt(matcher.group(1));
            long pullRequestId = Long.parseLong(matcher.group(2));
            return new PullRequestBatchId(repositoryId, pullRequestId);
        } else {
            return null;
        }
    }

    public String encode() {
        return repositoryId + "," + pullRequestId;
    }

    public int getRepositoryId() {
        return repositoryId;
    }

    public long getPullRequestId() {
        return pullRequestId;
    }
}
