/*
 * 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 "ArchiveBase.hpp"
#include "Data/Containers/Array.hpp"
#include "ArchiveType.hpp"
#include <string_view>
#include <optional>
#include <functional>
#include <queue>

namespace OpenVulkano
{
	class ArchiveReader final : public ArchiveBase
	{
		std::optional<ArchiveType::Type> m_archiveType;
		std::queue<std::string> m_archivesToRead;
		std::function<const char*()> m_passwordCallback;
		std::function<std::string(std::string)> m_filePathRewrite;
		bool m_open = false;
		bool m_eof = false;

	public:
		explicit ArchiveReader(const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);

		explicit ArchiveReader(const char* archiveFile, const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);

		explicit ArchiveReader(const std::string& archiveFile, const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);

		explicit ArchiveReader(const std::filesystem::path& archiveFile, const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);

		ArchiveReader(const void* archiveBuffer, size_t size, const std::shared_ptr<spdlog::logger>& logger = nullptr, std::optional<ArchiveType::Type> archiveType = std::nullopt);

		~ArchiveReader() override;

		void SetPasswordCallback(const decltype(m_passwordCallback)& callback);

		bool Open(const std::filesystem::path& archiveFile);

		bool Open(const char* archiveFile);

		bool Open(const std::string& archiveFile);

		bool OpenMemory(const void* archiveBuffer, size_t size);

		bool Open(const std::vector<std::string>& archiveFiles);

		bool Open(const std::filesystem::path& dir, const std::string& fileNamePattern, size_t* outSize = nullptr);

		[[nodiscard]] bool IsOpen() const { return m_open; }

		[[deprecated]] size_t ExtractRemaining(std::string_view targetDir);
		
		size_t ExtractRemaining(const std::filesystem::path& targetDir);
		
		size_t ExtractRemaining(const std::filesystem::path& targetDir, const std::function<void(const FileDescription&)> extractionCallback);

		// Element wise operations
		[[nodiscard]] bool HasNext() const;

		void SkipNext();

		bool SkipTill(std::filesystem::file_type type = std::filesystem::file_type::regular);

		[[nodiscard]] FileDescription GetNextDescription() const;

		std::optional<FileDescription> GetNextDirectory();

		[[deprecated]] std::optional<FileDescription> ExtractNext(std::string_view targetDir) { return ExtractNext(std::filesystem::path(targetDir)); }
		
		std::optional<FileDescription> ExtractNext(const std::filesystem::path& targetDir);

		std::optional<std::pair<FileDescription, Array<char>>> GetNextFile();

		std::optional<std::pair<FileDescription, std::vector<char>>> GetNextFileAsVector();

		std::optional<std::pair<FileDescription, Array<char>>> GetFile(const std::string& path);

		std::optional<FileDescription> StreamNextFile(std::ostream& stream);

		bool GetNextFileAsStream(const std::function<void(const FileDescription&, std::istream&)>& streamReader);

		const decltype(m_passwordCallback)& GetPasswordCallback() const { return m_passwordCallback; }
		
		void SetPathRewriteFunction(const decltype(m_filePathRewrite)& rewriteFunc) { m_filePathRewrite = rewriteFunc; }
		
		const decltype(m_filePathRewrite)& GetPathRewriteFunction() const { return m_filePathRewrite; }

	private:
		void ReadNextHeader();
		std::function<int(archive*)> GetCurrentFormatReadFunc() const;
		void PrepOpen();
	};
}
