package com.atlassian.bitbucket.mesh.throttle;

import javax.annotation.Nonnull;
import java.time.Instant;

import static java.util.Objects.requireNonNull;

/**
 * Summarizes a set of useful statistics for a single bucket of {@link Ticket tickets}.
 */
public class TicketSummary {

    private final int available;
    private final Instant lastRejection;
    private final Instant oldestQueuedRequest;
    private final int queuedRequests;
    private final ThrottledResource resource;
    private final int total;
    private final int used;

    public TicketSummary(@Nonnull ThrottledResource resource, int available, Instant lastRejection,
                         Instant oldestQueuedRequest, int queuedRequests, int total, int used) {
        this.available = available;
        this.lastRejection = lastRejection;
        this.oldestQueuedRequest = oldestQueuedRequest;
        this.queuedRequests = queuedRequests;
        this.resource = requireNonNull(resource, "resource");
        this.total = total;
        this.used = used;
    }

    /**
     * @return the number of tickets currently available, out of the configured {@link #getTotal() total}
     */
    public int getAvailable() {
        return available;
    }

    /**
     * @return the timestamp of the last rejected ticket, or {@code null} if no tickets have been rejected
     */
    public Instant getLastRejection() {
        return lastRejection;
    }

    /**
     * @return the throttled resource the ticket bucket corresponds to
     */
    @Nonnull
    public ThrottledResource getResource() {
        return resource;
    }

    /**
     * @return the timestamp at which the oldest queued request started waiting, or {@code null} if there are no queued
     *         requests
     */
    public Instant getOldestQueuedRequest() {
        return oldestQueuedRequest;
    }

    /**
     * @return the number of requests currently waiting for an available ticket
     */
    public int getQueuedRequests() {
        return queuedRequests;
    }

    /**
     * @return the total number of tickets allowed for this bucket
     */
    public int getTotal() {
        return total;
    }

    /**
     * @return the number of tickets that are currently held by the application
     */
    public int getUsed() {
        return used;
    }
}
