#pragma once

#include <sodium.h>
#include <vector>
#include <array>
#include <stdexcept>
#include <fstream>
#include <ctime>
#include <iostream>
#include <xxhash.h>
#include <sstream>
#include <iomanip>
#include <algorithm>

#include "DataStruct.h"

class EncryptionManager
{
public:
	EncryptionManager();
	~EncryptionManager() = default;

	std::vector<char> encrypt(const std::vector<char>&);
	std::vector<char> decrypt(const std::vector<char>&);

	void saveKey(const std::string&, bool);
	void loadKey(const std::string&);

private:
	std::array<unsigned char, crypto_stream_chacha20_ietf_KEYBYTES> key{};
	std::array<unsigned char, crypto_stream_chacha20_ietf_NONCEBYTES> nonce{};
	bool keyReady;

	void generateKeys();
	std::string toHex(const std::vector<unsigned char>&);
	void saveCppHeadFile(const std::string&);

	template <size_t N>
	std::array<unsigned char, N> toArray(const std::vector<char>& vec) {
		if (vec.size() < N) {
			throw std::runtime_error("Too small vector to convert to array");
		}

		std::array<unsigned char, N> arr{};
		std::memcpy(arr.data(), vec.data(), N);
		return arr;
	}
};