// Copyright Pacificao. All Rights Reserved.

#include "AgrarianFoliagePatch.h"

#include "AgrarianPerformanceStats.h"
#include "Components/HierarchicalInstancedStaticMeshComponent.h"
#include "Components/SceneComponent.h"
#include "ProfilingDebugging/CpuProfilerTrace.h"

namespace
{
void ConfigureFoliageComponent(UHierarchicalInstancedStaticMeshComponent* Component, const FName CollisionProfileName)
{
	if (!Component)
	{
		return;
	}

	Component->SetMobility(EComponentMobility::Static);
	Component->SetCollisionProfileName(CollisionProfileName);
	Component->SetGenerateOverlapEvents(false);
	Component->bCastDynamicShadow = true;
	Component->bCastStaticShadow = true;
	Component->InstanceStartCullDistance = 120000;
	Component->InstanceEndCullDistance = 180000;
}
}

AAgrarianFoliagePatch::AAgrarianFoliagePatch()
{
	PrimaryActorTick.bCanEverTick = false;

	SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("SceneRoot"));
	RootComponent = SceneRoot;
	SceneRoot->SetMobility(EComponentMobility::Static);

	TreeInstances = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("TreeInstances"));
	TreeInstances->SetupAttachment(SceneRoot);
	ConfigureFoliageComponent(TreeInstances, TEXT("BlockAll"));

	ShrubInstances = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("ShrubInstances"));
	ShrubInstances->SetupAttachment(SceneRoot);
	ConfigureFoliageComponent(ShrubInstances, TEXT("NoCollision"));

	GrassInstances = CreateDefaultSubobject<UHierarchicalInstancedStaticMeshComponent>(TEXT("GrassInstances"));
	GrassInstances->SetupAttachment(SceneRoot);
	ConfigureFoliageComponent(GrassInstances, TEXT("NoCollision"));
}

void AAgrarianFoliagePatch::ClearFoliage()
{
	SCOPE_CYCLE_COUNTER(STAT_AgrarianFoliageInstanceMutation);
	TRACE_CPUPROFILER_EVENT_SCOPE(AgrarianFoliageClear);

	if (TreeInstances)
	{
		TreeInstances->ClearInstances();
	}

	if (ShrubInstances)
	{
		ShrubInstances->ClearInstances();
	}

	if (GrassInstances)
	{
		GrassInstances->ClearInstances();
	}
}

int32 AAgrarianFoliagePatch::AddTreeInstance(const FTransform& InstanceTransform)
{
	SCOPE_CYCLE_COUNTER(STAT_AgrarianFoliageInstanceMutation);
	TRACE_CPUPROFILER_EVENT_SCOPE(AgrarianFoliageAddTreeInstance);

	return TreeInstances ? TreeInstances->AddInstance(InstanceTransform, true) : INDEX_NONE;
}

int32 AAgrarianFoliagePatch::AddShrubInstance(const FTransform& InstanceTransform)
{
	SCOPE_CYCLE_COUNTER(STAT_AgrarianFoliageInstanceMutation);
	TRACE_CPUPROFILER_EVENT_SCOPE(AgrarianFoliageAddShrubInstance);

	return ShrubInstances ? ShrubInstances->AddInstance(InstanceTransform, true) : INDEX_NONE;
}

int32 AAgrarianFoliagePatch::AddGrassInstance(const FTransform& InstanceTransform)
{
	SCOPE_CYCLE_COUNTER(STAT_AgrarianFoliageInstanceMutation);
	TRACE_CPUPROFILER_EVENT_SCOPE(AgrarianFoliageAddGrassInstance);

	return GrassInstances ? GrassInstances->AddInstance(InstanceTransform, true) : INDEX_NONE;
}

int32 AAgrarianFoliagePatch::GetTreeInstanceCount() const
{
	return TreeInstances ? TreeInstances->GetInstanceCount() : 0;
}

int32 AAgrarianFoliagePatch::GetShrubInstanceCount() const
{
	return ShrubInstances ? ShrubInstances->GetInstanceCount() : 0;
}

int32 AAgrarianFoliagePatch::GetGrassInstanceCount() const
{
	return GrassInstances ? GrassInstances->GetInstanceCount() : 0;
}
