/*
 * 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/.
 */

#include "Version.hpp"
#include <charconv>
#include <sstream>
#include <regex>

namespace OpenVulkano
{
	namespace
	{
		struct ComponentDecodeHolder
		{
			std::vector<uint32_t> versionComponents;
			std::vector<std::string> tagComponents;
			std::vector<uint32_t> appleBuildComponents;

			ComponentDecodeHolder(const std::string_view& versionStr)
			{
				if (versionStr.empty()) return;
				size_t offset = 0;
				if (versionStr[0] == 'v' || versionStr[0] == 'V')
				{
					offset++;
					if (versionStr.size() == 1) return;
				}
				ReadVersionComponents(versionComponents, versionStr, offset);
				if (offset < versionStr.size() && versionStr[offset] == '-')
				{
					offset++;
					ReadTagComponents(tagComponents, versionStr, offset);
				}
				if (versionStr.size() > 3 && versionStr.size() - 3 > offset && versionStr[offset] == ' ' && versionStr[offset + 1] == '(' && versionStr.back() == ')')
				{
					ReadVersionComponents(appleBuildComponents, versionStr, offset);
					if (versionStr[offset] != ')') appleBuildComponents.clear();
				}
			}

		private:
			static int ToNumber(std::string_view numberStr)
			{
				if (numberStr.empty()) return 0;
				int tmp = 0;
				std::from_chars(numberStr.data(), numberStr.data() + numberStr.size(), tmp);
				return tmp;
			}

			static void ReadVersionComponents(std::vector<uint32_t>& comps, const std::string_view& versionStr, size_t& offset)
			{
				do
				{
					size_t blockStart = offset;
					while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++;
					comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart)));
					if (offset < versionStr.size() && versionStr[offset] != '.') break;
					offset++;
				} while(versionStr.size() > offset);
			}

			static void ReadTagComponents(std::vector<std::string>& tags, const std::string_view& versionStr, size_t& offset)
			{
				do
				{
					size_t blockStart = offset;
					while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++;
					tags.emplace_back(versionStr.data() + blockStart, offset - blockStart);
					if (offset >= versionStr.size() || versionStr[offset] == ' ' || versionStr[offset] == '\0') break;
					offset++;
				} while (versionStr.size() > offset);
			}
		};

		std::pair<char, uint64_t> ReadTagValue(const std::string& str)
		{
			std::pair<char, uint64_t> p;
			std::istringstream iss(str);
			iss >> p.first >> p.second;
			return p;
		}
	}

	Version::Version(uint32_t major, uint32_t minor, uint32_t patch, const uint32_t build)
			: m_versionComponents(build ? std::initializer_list<uint32_t>{major, minor, patch, build} : std::initializer_list<uint32_t>{major, minor, patch})
			, m_buildNumber(build)
			, m_versionString("v" + std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch) + (build ? "." + std::to_string(build) : ""))
	{}

	Version::Version(std::string_view versionString, bool ignoreTags)
		: m_versionString(versionString)
	{
		ComponentDecodeHolder helper(versionString);
		if (!helper.appleBuildComponents.empty())
			while (helper.versionComponents.size() < 3) helper.versionComponents.push_back(0);
		if (helper.versionComponents.size() == 4) m_buildNumber = helper.versionComponents[3];
		m_versionComponents = std::move(helper.versionComponents);
		for (auto val : helper.appleBuildComponents) { m_versionComponents.push_back(val); }
		if (!ignoreTags)
		{
			m_tagComponents = std::move(helper.tagComponents);
			const auto tagRegex = std::regex("[bBtT][0-9]+");
			for (const std::string& tag : m_tagComponents)
			{
				if (std::regex_match(tag, tagRegex))
				{
					auto t = ReadTagValue(tag);
					if (t.first == 't' || t.first == 'T')
						m_timestamp = t.second;
					else
						m_buildNumber = t.second;
				}
			}
		}
		if (m_tagComponents.empty() && !helper.appleBuildComponents.empty())
		{
			if (helper.appleBuildComponents.size() == 3)
			{
				m_timestamp = helper.appleBuildComponents[1] * 1000000uLL + helper.appleBuildComponents[2];
			}
			m_buildNumber = helper.appleBuildComponents[0];
		}
	}

	//region Compare functions
	int Version::CompareBuildNr(const OpenVulkano::Version& other) const
	{
		if (m_buildNumber == 0 || other.m_buildNumber == 0) return 0;
		if (m_buildNumber > other.m_buildNumber) return 1;
		if (m_buildNumber < other.m_buildNumber) return -1;
		return 0;
	}

	int Version::CompareTimestamp(const Version& other) const
	{
		if (m_timestamp == 0 || other.m_timestamp == 0) return 0;
		if (m_timestamp > other.m_timestamp) return 1;
		if (m_timestamp < other.m_timestamp) return -1;
		return 0;
	}

	int Version::CompareComponents(const Version& other) const
	{
		size_t minCount = std::min(m_versionComponents.size(), other.m_versionComponents.size());
		for(size_t i = 0; i < minCount; i++)
		{
			if (m_versionComponents[i] > other.m_versionComponents[i]) return 1;
			if (m_versionComponents[i] < other.m_versionComponents[i]) return -1;
		}
		if (m_versionComponents.size() != other.m_versionComponents.size())
		{
			for(size_t i = minCount; i < std::max(m_versionComponents.size(), other.m_versionComponents.size()); i++)
			{
				if (GetComponent(i) > other.GetComponent(i)) return 1;
				if (GetComponent(i) < other.GetComponent(i)) return -1;
			}
		}
		return 0;
	}

	int Version::Compare(const OpenVulkano::Version& other) const
	{
		int comp;
		if ((comp = CompareComponents(other)) != 0) return comp;
		if ((comp = CompareBuildNr(other)) != 0) return comp;
		return CompareTimestamp(other);
	}
	//endregion
}
