#pragma once

#include <blobs-ipmid/blobs.hpp>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * This method must be declared as extern C for blob manager to lookup the
 * symbol.
 */
std::unique_ptr<blobs::GenericBlobInterface> createHandler();

#ifdef __cplusplus
}
#endif

namespace blobs
{

constexpr int kBufferSize = 1024;

struct ExampleBlob
{
    ExampleBlob() = default;
    ExampleBlob(uint16_t id, uint16_t flags) :
        sessionId(id), flags(flags),
        state(StateFlags::open_read | StateFlags::open_write), length(0)
    {
    }

    /* The blob handler session id. */
    uint16_t sessionId;

    /* The flags passed into open. */
    uint16_t flags;

    /* The current state. */
    uint16_t state;

    /* The buffer is a fixed size, but length represents the number of bytes
     * expected to be used contiguously from offset 0.
     */
    uint32_t length;

    /* The staging buffer. */
    uint8_t buffer[kBufferSize];
};

class ExampleBlobHandler : public GenericBlobInterface
{
  public:
    /* We want everything explicitly default. */
    ExampleBlobHandler() = default;
    ~ExampleBlobHandler() = default;
    ExampleBlobHandler(const ExampleBlobHandler&) = default;
    ExampleBlobHandler& operator=(const ExampleBlobHandler&) = default;
    ExampleBlobHandler(ExampleBlobHandler&&) = default;
    ExampleBlobHandler& operator=(ExampleBlobHandler&&) = default;

    bool canHandleBlob(const std::string& path) override;
    std::vector<std::string> getBlobIds() override;
    bool deleteBlob(const std::string& path) override;
    bool stat(const std::string& path, struct BlobMeta* meta) override;
    bool open(uint16_t session, uint16_t flags,
              const std::string& path) override;
    std::vector<uint8_t> read(uint16_t session, uint32_t offset,
                              uint32_t requestedSize) override;
    bool write(uint16_t session, uint32_t offset,
               const std::vector<uint8_t>& data) override;
    bool writeMeta(uint16_t session, uint32_t offset,
                   const std::vector<uint8_t>& data) override;
    bool commit(uint16_t session, const std::vector<uint8_t>& data) override;
    bool close(uint16_t session) override;
    bool stat(uint16_t session, struct BlobMeta* meta) override;
    bool expire(uint16_t session) override;

    constexpr static char supportedPath[] = "/dev/fake/command";

  private:
    ExampleBlob* getSession(uint16_t id);

    std::unordered_map<uint16_t, ExampleBlob> sessions;
};

} // namespace blobs