/*
 * Copyright 2015 - 2025 gary@drinkingtea.net
 *
 * 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 <ox/model/modelhandleradaptor.hpp>
#include <ox/model/optype.hpp>
#include <ox/model/types.hpp>
#include <ox/std/error.hpp>
#include <ox/std/string.hpp>
#include <ox/std/types.hpp>

namespace ox {

template<typename PlatSpec>
class UnionSizeCatcher: public ModelHandlerBase<UnionSizeCatcher<PlatSpec>, OpType::Reflect> {
	private:
		std::size_t m_size = 0;

	public:

		template<typename T>
		constexpr ox::Error setTypeInfo(
				const char* = T::TypeName,
				int = T::TypeVersion) noexcept {
			return {};
		}

		template<typename T>
		constexpr ox::Error setTypeInfo(
				const char*,
				int,
				const Vector<String>&,
				std::size_t) noexcept {
			return {};
		}

		template<typename T, bool force>
		constexpr ox::Error field(StringViewCR, const UnionView<T, force> val) noexcept {
			UnionSizeCatcher<PlatSpec> sc;
			OX_RETURN_ERROR(model(sc.interface(), val.get()));
			m_size += sc.size();
			return {};
		}

		template<typename T>
		constexpr ox::Error field(StringViewCR, const T *val) noexcept;

		template<typename T>
		constexpr ox::Error field(StringViewCR, const T **val, std::size_t cnt) noexcept;

		[[nodiscard]]
		constexpr auto size() const noexcept {
			return m_size;
		}

	private:
		template<typename T, std::size_t SmallVecSize>
		constexpr ox::Error fieldStr(StringViewCR, const ox::BasicString<SmallVecSize> *val) noexcept;

		template<typename T, std::size_t SmallVecSize>
		constexpr ox::Error fieldVector(StringViewCR, const ox::Vector<T, SmallVecSize> *val) noexcept;

		constexpr void setSize(std::size_t sz) noexcept;
};

template<typename PlatSpec>
template<typename T>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(StringViewCR, const T *val) noexcept {
	setSize(sizeOf<PlatSpec>(val));
	return {};
}

template<typename PlatSpec>
template<typename T>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::field(StringViewCR, const T **val, std::size_t cnt) noexcept {
	for (std::size_t i = 0; i < cnt; ++i) {
		OX_RETURN_ERROR(field("", &val[i]));
	}
	return {};
}

template<typename PlatSpec>
template<typename T, std::size_t SmallStrSize>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldStr(StringViewCR, const ox::BasicString<SmallStrSize>*) noexcept {
	ox::VectorMemMap<PlatSpec> v;
	setSize(sizeOf<PlatSpec>(v));
	return {};
}

template<typename PlatSpec>
template<typename T, std::size_t SmallVecSize>
constexpr ox::Error UnionSizeCatcher<PlatSpec>::fieldVector(StringViewCR, const ox::Vector<T, SmallVecSize>*) noexcept {
	ox::VectorMemMap<PlatSpec> v;
	setSize(sizeOf<PlatSpec>(v));
	return {};
}

template<typename PlatSpec>
constexpr void UnionSizeCatcher<PlatSpec>::setSize(std::size_t sz) noexcept {
	m_size = ox::max(m_size, sz);
}

}
