#include <DataUpdatePlanner/DataUpdatePlanner.hpp>
#include <AtomicDataStore/AtomicDataStore.hpp>

void DataUpdatePlanner::write(std::string name, const void *data, size_t size)
{
    plan[name] = std::vector<uint8_t>((const uint8_t*) data, (const uint8_t*) data + size);
}

void DataUpdatePlanner::write(std::string name, std::vector<uint8_t> &bytes)
{
    plan[name] = bytes;
}

std::vector<uint8_t>& DataUpdatePlanner::newFile(std::string name)
{
    std::vector<uint8_t> &data = plan[name];
    data.clear();
    return data;
}

void DataUpdatePlanner::clear(std::string name)
{
    plan[name].clear();
}

uint64_t DataUpdatePlanner::getCurrentGeneration()
{
    return currentGeneration;
}

std::unordered_map<std::string, std::vector<uint8_t>> DataUpdatePlanner::unpack()
{
    std::unordered_map<std::string, std::vector<uint8_t>> planCopy = plan;
    plan.clear();
    currentGeneration++;
    return planCopy;
}

bool DataUpdatePlanner::read(std::string name, AtomicDataStore &manager, std::vector<uint8_t> &outBytes)
{
    if (plan.count(name) == 1)
    {
        outBytes = plan[name];
        return true;
    }

    return manager.read(name, outBytes);
}