#ifndef DATA_UPDATE_PLANNER_HPP_
#define DATA_UPDATE_PLANNER_HPP_

#include <unordered_map>
#include <string>
#include <vector>
#include <cstdint>

class AtomicDataStore;

/**
 * @brief Planner of world data updates.
 */
class DataUpdatePlanner
{
private:
    std::unordered_map<std::string, std::vector<uint8_t>> plan;
    uint64_t currentGeneration = 1;

public:
    /**
     * @brief Overwrite a file.
     * 
     * @param name The file name (path relative to the world data dir).
     * @param data Pointer to the data.
     * @param size Size of the data.
     */
    void write(std::string name, const void *data, size_t size);

    /**
     * @brief Overwrite a file.
     * 
     * @param name The file name (path relative to the world data dir).
     * @param bytes The bytes to overwrite with.
     */
    void write(std::string name, std::vector<uint8_t> &bytes);

    /**
     * @brief Create a (or clear an existing) file and return the reference to the data.
     * 
     * @param name The name of the file to create or clear.
     * @return Reference to the data vector, initially clear.
     */
    std::vector<uint8_t>& newFile(std::string name);

    /**
     * @brief Clear a file.
     * 
     * This causes the file to be resized to zero bytes, which is equivalent to
     * deleting the file.
     * 
     * @param name The file name (path relative to the world data dir).
     */
    void clear(std::string name);

    /**
     * @brief Get the current generation of the plan.
     */
    uint64_t getCurrentGeneration();

    /**
     * @brief Return a copy of the current plan, then clear it and advance generation.
     */
    std::unordered_map<std::string, std::vector<uint8_t>> unpack();

    /**
     * @brief Read the specified file.
     */
    bool read(std::string name, AtomicDataStore &manager, std::vector<uint8_t> &outBytes);
};

#endif