/*
 * 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/Archive/ArchiveReader.hpp"
#include "IO/Files/Pfm.hpp"
#include "IO/Files/Pnm.hpp"
#include <string>
#include <fstream>

namespace OpenVulkano::AR::Playback
{
	struct DepthImage
	{
		PfmImage depth;
		PnmImage confidence;
	};

	struct ColorImg
	{
		enum ChannelCount { GRAY = 1, RGBA = 4, YUV = 2, NV12 = -2 };

		std::unique_ptr<uint8_t[]> dataPtr;
		uint8_t* data = nullptr;
		uint8_t* dataUV = nullptr;
		int cols = 0, rows = 0, channels = 0;
		
		void Allocate()
		{
			if (data) throw std::runtime_error("Data already set!");
			dataPtr.reset(new uint8_t[cols * rows * abs(channels)]);
			data = dataPtr.get();
		}

		bool UseRGB() const { return channels == RGBA; }
	};

	class ArPlaybackReader final
	{
		static constexpr std::string_view TAR_EXTENSIONS_REGEX = R"(\.(tar(\.gz|\.bz2|\.zst)?|tgz|tbz|tb2|tbz2))";

		ArchiveReader m_archiveMetadata, m_archiveColor, m_archiveDepth, m_archiveConfidence;
		size_t m_imgTotalSize = 0, m_imgReadSize = 0;
		
	public:
		ArPlaybackReader(const std::string& recDir)
		{
			std::string extensions = R"((_\d+|\.part\d+)?)" + std::string(TAR_EXTENSIONS_REGEX);
			m_archiveMetadata.Open(recDir, ".*meta(data)?" + extensions);
			m_archiveColor.Open(recDir, ".*(color|image)" + extensions, &m_imgTotalSize);
			m_archiveDepth.Open(recDir, ".*depth" + extensions);
			m_archiveConfidence.Open(recDir, ".*conf(idence)?" + extensions);
		}

		int GetNextFrameId()
		{
			std::string name = m_archiveMetadata.GetNextDescription().path;
			return std::stoi(name.substr(0, name.length() - 5));
		}

		Array<char> ReadMetadata()
		{
			return std::move(m_archiveMetadata.GetNextFile()->second);
		}

		ColorImg ReadColorImage()
		{
			ColorImg img;
			img.channels = ColorImg::RGBA; // TODO test if defaulting to NV12
			ReadColorImage(img);
			return img;
		}

		void ReadColorImage(ColorImg& img);

		DepthImage ReadDepthImage()
		{
			DepthImage img;
			m_archiveDepth.GetNextFileAsStream([&img](const FileDescription& desc, std::istream& stream) { img.depth.Read(stream); });
			m_archiveConfidence.GetNextFileAsStream([&img](const FileDescription&, std::istream& stream) { img.confidence.Read(stream); });
			
			return img;
		}
		
		[[nodiscard]] double GetProgress() const
		{
			return static_cast<double>(m_imgReadSize) / static_cast<double>(m_imgTotalSize);
		}

		[[nodiscard]] bool HasNext() const
		{
			return m_archiveMetadata.HasNext() && m_archiveDepth.HasNext() && m_archiveConfidence.HasNext() && m_archiveColor.HasNext();
		}
	};
}
