#pragma once
#include "detail/uclass.inl"
#include "detail/view.inl"
#include "detail/field.inl"
#include "detail/convert.inl"
#include "detail/meta.inl"
#include "std/sarray.h"
#include "macro.h"
namespace refl {
	template<typename T, typename Obj>
	consteval FieldPtr StaticMemberField(T Obj::* ptr, const Name& name, const MemberData& data = {}) {
		Offset offset = data.value.IsValid() ? sizeof(T) : 0;
		if (data.meta.IsValid()) {
			offset += data.meta.Size();
		}
		FieldPtr::Data member(offset);
		uint32_t flag = FIELD_MEMBER_FLAG | FIELD_ATTRIBUTE_FLAG;
		return { name,&TypeInfo<T>::StaticClass, member,flag};
	}
	template<typename R, typename ...Args>
	consteval FieldPtr StaticMethodField(R(*ptr)(Args...), const Name& name, const MethodData& data = {}) {
		uint32_t flag = FIELD_METHOD_FLAG;
		auto cls = &TypeInfo<real_type_t<R>(*)(real_type_t<Args>...)>::StaticClass;
		Offset offset = AnyArgs::GetArgsSize(data.value, cls->GetParams());
		if (data.meta.IsValid()) {
			offset += data.meta.Size();
		}
		FieldPtr::Data method(offset);
		return { name,cls,method, flag };
	}
	template<typename T, typename R, typename ...Args>
	consteval FieldPtr StaticMethodField(R(T::* ptr)(Args...), const Name& name, const MethodData& data = {}) {
		uint32_t flag = FIELD_MEMBER_FLAG | FIELD_METHOD_FLAG;
		auto cls = &TypeInfo<real_type_t<R>(*)(const void*, real_type_t<Args>...)>::StaticClass;
		Offset offset = AnyArgs::GetArgsSize(data.value, cls->GetParams());
		if (data.meta.IsValid()) {
			offset += data.meta.Size();
		}
		FieldPtr::Data method(offset);
		return { name, cls,method,flag };
	}
	//用处不大---------------begin
	template<_ReflCheck_Meta T>
	consteval int GetStaticFieldID(const Name& name) {
		auto fields = T::MyStatic::__MakeStaticFields();
		auto first = fields.begin();
		for (auto& field : fields) {
			if (field.name == name) {
				return &field - &*first;
			}
		}
		return 0;
	}
	template<_ReflCheck_Meta T>
	consteval int GetStaticFieldID(bool(* fptr)(FieldPtr)) {
		auto fields = T::MyStatic::__MakeStaticFields();
		auto first = fields.begin();
		for (auto& field : fields) {
			if (fptr(field)) {
				return &field - &*first;
			}
		}
		return 0;
	}
	template<_ReflCheck_Meta T, auto name>
	consteval auto GetStaticField() {
		constexpr auto fields = T::MyMeta::__StaticFields();
		using AutoType = decltype(name);
		if constexpr (std::is_same_v<AutoType, int>) {
			return std::get<name>(fields);
		}
		else {
			constexpr auto id = GetStaticFieldID<T>(name.Data());
			return std::get<id>(fields);
		}
	}
	//用处不大---------------end
}