// Copyright (c) coherence ApS.
// For all coherence generated code, the coherence SDK license terms apply. See the license file in the coherence Package root folder for more information.

// <auto-generated>
// Generated file. DO NOT EDIT!
// </auto-generated>
namespace Coherence.Toolkit
{
	using UnityEngine;
	using System;
	using Coherence.Entity;
	using Coherence.Generated;
	using Coherence.ProtocolDef;
	using ConnectionType = Coherence.Connection.ConnectionType;
	using ClientID = Coherence.Connection.ClientID;

	public class CoherenceMonoBridgeImpl
	{
		[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
		static void OnRuntimeMethodLoad()
		{
			Impl.GenericPrefabReferenceTypeID = GenericPrefabReferenceTypeID;
			Impl.GenericPrefabIdTypeID = GenericPrefabIdTypeID;
			Impl.GetSpawnInfo = GetSpawnInfo;
			Impl.GetRootDefinition = GetRootDefinition;
		}

		private static uint GenericPrefabReferenceTypeID()
		{
			return Definition.InternalGenericPrefabReference;
		}

		private static uint GenericPrefabIdTypeID()
		{
			return Definition.InternalGenericPrefabId;
		}

		static (bool, CoherenceMonoBridge.SpawnInfo) GetSpawnInfo(IClient client, EntityUpdate entityUpdate)
		{
			var info = new CoherenceMonoBridge.SpawnInfo();
			var gotPosition = false;
			var gotPrefab = false;

			foreach (var comp in entityUpdate.Components.Updates.Store)
			{
				switch(comp.Value.Data)
				{
					case WorldPosition pos:
						info.position = pos.value;
						gotPosition = true;
						break;
					case WorldOrientation rot:
						info.rotation = rot.value;
						break;
#if !COHERENCE_DISABLE_NAME_BASED_PREFAB_LOADING
					case GenericPrefabReference prefabRef:
						info.prefabName = prefabRef.prefab;
						gotPrefab = true;
						break;
#endif
					case GenericPrefabId prefabId:
						info.prefabId = new PrefabId(prefabId.value);
						gotPrefab = true;
						break;
					case ConnectedEntity connectedEntity:
						info.connectedEntity = connectedEntity.value;
						break;
					case Connection con:
						info.clientId = (ClientID)con.id;
						info.connectionType = (ConnectionType)con.type;
						break;
					case UniqueID uid:
						info.uniqueId = uid.uuid;
						break;
				}
			}

			var shouldSpawn = (gotPosition && gotPrefab) || info.clientId.HasValue;

			return (shouldSpawn, info);
		}

		private static IDefinition GetRootDefinition()
		{
			return new Definition();
		}
	}
}
