// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "NetworkSwitchComponent.h"
#include "NetworkDoor.h"
#include "NetworkSwitch.h"
#include "NetworkTrigger.h"
#include "Spawners/CreatureSpawn.h"

UNetworkSwitchComponent::UNetworkSwitchComponent(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	ShapeColor = FColor(223, 149, 157, 255);

	bUseEditorCompositing = true;

	networkDoodad = Cast<ANetworkDoodad>(GetAttachmentRootActor());
	creatureSpawn = Cast<ACreatureSpawn>(GetAttachmentRootActor());
}

FPrimitiveSceneProxy* UNetworkSwitchComponent::CreateSceneProxy()
{
	class FDrawConeSceneProxy : public FPrimitiveSceneProxy
	{
	public:
		const UNetworkSwitchComponent* component;
		FDrawConeSceneProxy(const UNetworkSwitchComponent* InComponent)
			: FPrimitiveSceneProxy(InComponent)
			, networkDoodad(InComponent->networkDoodad)
			, creatureSpawn(InComponent->creatureSpawn)
			, bDrawOnlyIfSelected(InComponent->bDrawOnlyIfSelected)
			, component(InComponent)
		{
			bWillEverBeLit = false;
		}


		virtual void GetDynamicMeshElements(const TArray<const FSceneView*>& Views, const FSceneViewFamily& ViewFamily, uint32 VisibilityMap, FMeshElementCollector& Collector) const override
		{
			QUICK_SCOPE_CYCLE_COUNTER(STAT_GetDynamicMeshElements_DrawDynamicElements);

			const FMatrix& LocalToWorld = GetLocalToWorld();
			
			for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++)
			{
				const FSceneView* View = Views[ViewIndex];
					
				FPrimitiveDrawInterface* PDI = Collector.GetPDI(ViewIndex);
				if (networkDoodad)
				{
					if (networkDoodad->IsA(ANetworkTrigger::StaticClass()))
					{
						const ANetworkTrigger* const obj = Cast<const ANetworkTrigger>(networkDoodad);
						for (int32 i = 0; i < obj->doorsToOpen.Num(); i++)
						{
							if (obj->doorsToOpen[i])
								PDI->DrawLine(obj->GetActorLocation() + FVector(0, 0, 100), obj->doorsToOpen[i]->GetActorLocation() + FVector(0, 0, 100), obj->doorsToOpen[i]->debugColorCode, SDPG_World);
						}
						for (int32 i = 0; i < obj->creaturesToSpawn.Num(); i++)
						{
							if (obj->creaturesToSpawn[i])
								PDI->DrawLine(obj->GetActorLocation() + FVector(0, 0, 100), obj->creaturesToSpawn[i]->GetActorLocation() + FVector(0, 0, 100), obj->creaturesToSpawn[i]->debugColorCode, SDPG_World);
						}
					}
					if (networkDoodad->IsA(ANetworkSwitch::StaticClass()))
					{
						const ANetworkSwitch* const obj = Cast<const ANetworkSwitch>(networkDoodad);
						for (int32 i = 0; i < obj->doorsToOpen.Num(); i++)
						{
							if (obj->doorsToOpen[i])
								PDI->DrawLine(obj->GetActorLocation() + FVector(0, 0, 100), obj->doorsToOpen[i]->GetActorLocation() + FVector(0, 0, 100), obj->doorsToOpen[i]->debugColorCode, SDPG_World);
						}
					}
				}
				if (creatureSpawn)
				{
			
					for (int32 i = 0; i < creatureSpawn->doorsToOpen.Num(); i++)
					{
						if (creatureSpawn->doorsToOpen[i])
							PDI->DrawLine(creatureSpawn->GetActorLocation() + FVector(0, 0, 100), creatureSpawn->doorsToOpen[i]->GetActorLocation() + FVector(0, 0, 100), creatureSpawn->doorsToOpen[i]->debugColorCode, SDPG_World);
					}
				}
			}
		}

		virtual FPrimitiveViewRelevance GetViewRelevance(const FSceneView* View) const override
		{
			FPrimitiveViewRelevance Result;
			Result.bDrawRelevance = IsSelected();
			Result.bDynamicRelevance = true;
			Result.bShadowRelevance = IsShadowCast(View);
			Result.bEditorPrimitiveRelevance = UseEditorCompositing(View);
			return Result;
		}

		virtual uint32 GetMemoryFootprint(void) const override { return(sizeof(*this) + GetAllocatedSize()); }
		uint32 GetAllocatedSize(void) const { return(FPrimitiveSceneProxy::GetAllocatedSize()); }

	private:
		const uint32	bDrawOnlyIfSelected : 1;
		const ANetworkDoodad* networkDoodad;
		const ACreatureSpawn* creatureSpawn;
		const FColor	ShapeColor = FColor(255, 0, 0, 255);
		const FTransform transform;
	};

	return new FDrawConeSceneProxy(this);
}
