#pragma once
#include "UTemplate/Type.hpp"
#include "../std/sarray.h"
#include "any.h"
#include <variant>
#include <functional>
using Ubpa::Name;
using Ubpa::type_name;
consteval Name FName(const char* buf) {
	return Name(buf);
}
namespace refl {
	using Offset = uint32_t;
	using Method = void*;
	struct MemberData {
		Offset offset{0};
		Any value;
		Any meta;
		constexpr MemberData() :value(), meta() {}
		constexpr MemberData(const Any& value, const Any& meta = {}) : value(value), meta(meta) {}
		constexpr MemberData(Offset offset,const Any& value,const Any& meta) : offset(offset), value(value), meta(meta) {}
	};
	struct MethodData {
		Method fptr{nullptr};
		sarray<Any> value;
		Any meta;
		constexpr MethodData() :value(), meta() {}
		constexpr MethodData(const sarray<Any>& value, const Any& meta = {}) : value(value), meta(meta) {}
		constexpr MethodData(Method fptr,const sarray<Any>& value, const Any& meta) : fptr(fptr), value(value), meta(meta) {}
	};
	enum FieldFlag:uint32_t {
		FIELD_NONE_FLAG				= 0,
		FIELD_MEMBER_FLAG			= 1 << 0,
		FIELD_ATTRIBUTE_FLAG		= 1 << 1,
		FIELD_METHOD_FLAG			= 1 << 2,
		FIELD_METHOD_VALUE_FLAG     = 1 << 3
	};
	using enum FieldFlag;
	struct FieldPtr {
		union Data
		{
			MemberData member;
			MethodData method;
			constexpr Data() : member() {};
			constexpr Data(const MemberData& member) :member(member) {}
			constexpr Data(const MethodData& method) : method(method) {}
			constexpr Data(Offset offset) :member(offset, {}, {}) {}
			constexpr Data(Offset offset, const Any& value, const Any& meta):member(offset, value, meta){}
			constexpr Data(Method fptr, const sarray<Any>& value, const Any& meta) : method(fptr, value, meta) {}
		};
		Name name;
		const UClass* type{};
		Data data{};
		uint32_t flag{};
		
		//unsafe
		bool Invoke(const sarray<Any>& ArgsList)const;
		//safe
		bool Invoke(svector<Any>& ArgsList)const;

		template<typename Func, typename... Args>
		auto Call(Func func, Args&&... args)const;

		sarray<const UClass*> GetParams() const;
	};
}