/*
* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "IO/MemMappedFile.hpp"
#include <filesystem>

namespace OpenVulkano
{
	class MemMappedFileWriteHelper
	{
	public:
		MemMappedFileWriteHelper(const std::filesystem::path& path, size_t maxFileSize);
		~MemMappedFileWriteHelper();

		[[nodiscard]] void* Data() const { return m_file->Data(); }
		[[nodiscard]] size_t Size() const { return m_file->Size(); }

		bool Resize(size_t newSize);
		void Close();
		bool Trim(size_t finalFileSize);

		static constexpr size_t USE_CURRENT_FILE_SIZE = 0;

	private:
		MemMappedFile* m_file;
		std::filesystem::path m_path;
	};

	class MemMappedFileWriter final
	{
	public:
		MemMappedFileWriter(const std::filesystem::path& path, size_t maxFileSize);
		~MemMappedFileWriter();

		void Write(const char* data, size_t size);
		void SetOffset(size_t offset) { m_writtenSize = offset; }
		[[nodiscard]] size_t GetOffset() const { return m_writtenSize; }

	private:
		MemMappedFileWriteHelper m_helper;
		size_t m_writtenSize = 0;
	};
}