#pragma once

#include <fstream>
#include "../data/mem.hpp"
#include "../utils/logger.hpp"

namespace data {

/// File loader
class file_content: public std::vector<char> {
public:
    /// Read first
    file_content(const std::vector<std::string>& paths, data::out_view prefix = data::out_view(nullptr, 0)):
        std::vector<char>(), prefix_size(prefix.size())
    {
        std::ifstream is = [&]() {
            for(auto& path: paths) {
                std::ifstream is(path, std::ios::in | std::ios::binary | std::ios::ate);
                if(is.good()) {
                    return is;
                }
                is.close();
            }
            FATAL("File not found " << paths.back());
        }();
        const auto end = is.tellg();
        is.seekg(0, std::ios::beg);
        resize(end - is.tellg() + prefix_size);
        memcpy(data(), prefix.ptr, prefix_size);
        is.read(data() + prefix_size, size());
        is.close();
    }

    size_t prefix_size;

    /// Data after prefix
    data::out_view content() const {
        return data::out_view((const uint8_t*)data() + prefix_size, size() - prefix_size);
    }
};

}