//------------------------------------------------------------------------------
/*
    This file is part of clio: https://github.com/XRPLF/clio
    Copyright (c) 2022, the clio developers.

    Permission to use, copy, modify, and distribute this software for any
    purpose with or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL,  DIRECT,  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

/** @file */
#pragma once

#include <xrpl/basics/base_uint.h>

#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>
#include <vector>

namespace etl {

// TODO: does the note make sense? lockfree queues provide the same blocking behaviour just without mutex, don't they?
/**
 * @brief Generic thread-safe queue with a max capacity.
 *
 * @note (original note) We can't use a lockfree queue here, since we need the ability to wait for an element to be
 * added or removed from the queue. These waits are blocking calls.
 */
template <typename T>
class ThreadSafeQueue {
    std::queue<T> queue_;

    mutable std::mutex m_;
    std::condition_variable cv_;
    uint32_t maxSize_;

public:
    /**
     * @brief Create an instance of the queue.
     *
     * @param maxSize maximum size of the queue. Calls that would cause the queue to exceed this size will block until
     * free space is available.
     */
    ThreadSafeQueue(uint32_t maxSize) : maxSize_(maxSize)
    {
    }

    /**
     * @brief Push element onto the queue.
     *
     * Note: This method will block until free space is available.
     *
     * @param elt Element to push onto queue
     */
    void
    push(T const& elt)
    {
        std::unique_lock lck(m_);
        cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
        queue_.push(elt);
        cv_.notify_all();
    }

    /**
     * @brief Push element onto the queue.
     *
     * Note: This method will block until free space is available
     *
     * @param elt Element to push onto queue. Ownership is transferred
     */
    void
    push(T&& elt)
    {
        std::unique_lock lck(m_);
        cv_.wait(lck, [this]() { return queue_.size() <= maxSize_; });
        queue_.push(std::move(elt));
        cv_.notify_all();
    }

    /**
     * @brief Pop element from the queue.
     *
     * Note: Will block until queue is non-empty.
     *
     * @return Element popped from queue
     */
    T
    pop()
    {
        std::unique_lock lck(m_);
        cv_.wait(lck, [this]() { return !queue_.empty(); });

        T ret = std::move(queue_.front());
        queue_.pop();

        cv_.notify_all();
        return ret;
    }

    /**
     * @brief Attempt to pop an element.
     *
     * @return Element popped from queue or empty optional if queue was empty
     */
    std::optional<T>
    tryPop()
    {
        std::scoped_lock const lck(m_);
        if (queue_.empty())
            return {};

        T ret = std::move(queue_.front());
        queue_.pop();

        cv_.notify_all();
        return ret;
    }

    /**
     * @brief Get the size of the queue
     *
     * @return The size of the queue
     */
    std::size_t
    size() const
    {
        return queue_.size();
    }
};

/**
 * @brief Partitions the uint256 keyspace into numMarkers partitions, each of equal size.
 *
 * @param numMarkers Total markers to partition for
 * @return The markers
 */
std::vector<ripple::uint256>
getMarkers(size_t numMarkers);

}  // namespace etl
