#pragma once

#include "File.h"

#include <filesystem>
#include <memory>
#include <unordered_map>
#include <string>

using Path = std::filesystem::path;

class TomlTable
{
public:
	using Comment = std::pair<unsigned, std::string>;
	using KeyValuePairs = std::unordered_map<std::string, std::string>;

	TomlTable(const std::string& header);

	void addComment(const Comment& comment);

	void addTable(std::unique_ptr<TomlTable> table);

	void addKeyValuePair(const std::string& key, const std::string& value);

	std::string getHeader() const;

	TomlTable* getTable(const std::string& path);

	KeyValuePairs getKeyValuePairs() const;

private:
	unsigned mLineOffset{ 0 };
	std::string mHeader;
	std::unordered_map<std::string, std::unique_ptr<TomlTable> > mTables;
	KeyValuePairs mMap;
	std::vector<Comment> mComments;
};

class TomlContent
{
public:
	TomlContent();

	TomlTable* getRootTable() const;

	TomlTable* getTable(const std::string& path) const;

private:
	std::unique_ptr<TomlTable> mRootTable;
};

class TomlReader
{
public:
	TomlReader();

	TomlContent* getContent() const;

	void read(const Path& input_path);

	void processLine(const std::string& line);

	void onHeader(const std::string& header);

	void onKeyValuePair(const std::string key, const std::string value);

private:
	unsigned mLastSectionOffset{ 0 };
	std::unique_ptr<TomlContent> mContent;
	TomlTable* mWorkingTable{nullptr};
};