/*
 * 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 <cstdint>
#include <string>
#include <string_view>
#include <vector>

namespace OpenVulkano
{
	class Version
	{
		std::vector<uint32_t> m_versionComponents;
		std::vector<std::string> m_tagComponents;
		uint64_t m_timestamp = 0, m_buildNumber = 0;
		std::string m_versionString;
		bool m_preRelease = false;

	public:
		Version(uint32_t major, uint32_t minor = 0, uint32_t patch = 0, uint32_t build = 0);

		Version(std::string_view versionString, bool ignoreTags = false);

		[[nodiscard]] uint32_t GetComponent(size_t compId) const
		{
			if (m_versionComponents.size() < compId + 1) return 0u;
			return m_versionComponents[compId];
		}

		[[nodiscard]] uint32_t Major() const { return GetComponent(0); }

		[[nodiscard]] uint32_t Minor() const { return GetComponent(1); }

		[[nodiscard]] uint32_t Patch() const { return GetComponent(2); }

		[[nodiscard]] uint64_t Build() const { return m_buildNumber; }

		[[nodiscard]] bool IsPreRelease() const { return m_preRelease; }

		[[nodiscard]] const std::vector<std::string>& GetTags() const { return m_tagComponents; }

		[[nodiscard]] const std::vector<uint32_t>& GetVersionComponents() const { return m_versionComponents; }

		//region Conversion operators
		[[nodiscard]] explicit operator uint32_t() const { return (Major() << 20) | ((Minor() & 0x3FF) << 10) | (Patch() & 0x3FF); }

		[[nodiscard]] operator const std::string&() const { return m_versionString; }
		//endregion

		//region Comparison operators
		[[nodiscard]] bool operator ==(const Version& rhs) const { return Compare(rhs) == 0; }
		[[nodiscard]] bool operator !=(const Version& rhs) const { return Compare(rhs); }
		[[nodiscard]] bool operator < (const Version& rhs) const { return Compare(rhs) < 0; }
		[[nodiscard]] bool operator <=(const Version& rhs) const { return Compare(rhs) < 1; }
		[[nodiscard]] bool operator > (const Version& rhs) const { return Compare(rhs) > 0; }
		[[nodiscard]] bool operator >=(const Version& rhs) const { return Compare(rhs) > -1; }
		//endregion

	private:
		[[nodiscard]] int Compare(const Version& other) const;
		[[nodiscard]] int CompareBuildNr(const Version& other) const;
		[[nodiscard]] int CompareTimestamp(const Version& other) const;
		[[nodiscard]] int CompareComponents(const Version& other) const;
	};
}
