// Project Lab - NHTV Igad

#include "UnrealProject.h"
#include "UnrealNetwork.h"
#include "AbilityIndicator.h"


// Sets default values
AAbilityIndicator::AAbilityIndicator()
{
	PrimaryActorTick.bCanEverTick = true;
	m_followActor = nullptr;
	m_duration = 0.0f;
	bReplicates = true;

}

AFillingAbilityIndicator::AFillingAbilityIndicator()
{

}
void AAbilityIndicator::BeginPlay()
{
	FVector pos;
	FRotator rot;

	if (m_followActor)
	{
		pos = m_followActor->GetActorLocation();
		rot = m_followActor->GetActorRotation() + m_rotationOffset;
	}
	else
	{
		pos = GetActorLocation();
		rot = GetActorRotation() + m_rotationOffset;
	}

	m_outlineDecal = GetWorld()->SpawnActor<ADecalActor>(pos, rot);
	m_outlineDecal->SetDecalMaterial(outlineMat);
	m_outlineMat = m_outlineDecal->CreateDynamicMaterialInstance();
	m_outlineMat->SetTextureParameterValue(FName("OutlineTexture"), outlineTexture);
	m_outlineDecal->SetActorScale3D(m_scale);
	m_outlineDecal->AddActorLocalOffset(-m_offset);
	m_outlineDecal->SetLifeSpan(m_duration);
	m_outlineMat->SetVectorParameterValue("TeamColor", m_color);
	Super::BeginPlay();
}


void AAbilityIndicator::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	//	m_outlineDecal->Destroy();

	if (IsValid(m_outlineDecal))
		m_outlineDecal->Destroy();
	Super::EndPlay(EndPlayReason);
}
void AFillingAbilityIndicator::EndPlay(const EEndPlayReason::Type EndPlayReason)
{

//		m_fillDecal->Destroy();

	if (IsValid(m_fillDecal))
		m_fillDecal->Destroy();
	Super::EndPlay(EndPlayReason);
}

void AFillingAbilityIndicator::BeginPlay()
{
	Super::BeginPlay();
	FVector pos ;
	FRotator rot;
	if (m_followActor)
	{
		pos = m_followActor->GetActorLocation();
		rot = m_followActor->GetActorRotation() + m_rotationOffset;
	}
	else
	{
		pos = GetActorLocation();
		rot = GetActorRotation() + m_rotationOffset;
	}
	m_fillDecal = GetWorld()->SpawnActor<ADecalActor>(pos, rot);
	m_fillDecal->SetDecalMaterial(fillMat);
	m_fillMat = m_fillDecal->CreateDynamicMaterialInstance();
	m_fillMat->SetTextureParameterValue(FName("FillTexture"), fillTexture);

	m_fillDecal->SetActorScale3D(m_scale);
	m_fillDecal->AddActorLocalOffset(-m_offset);
	m_fillDecal->SetLifeSpan(m_duration);

}

void AAbilityIndicator::CalculatePosition_Implementation()
{
	if (Role != ROLE_Authority)
		return;
	if (IsValid(m_followActor))
	{
		FTransform newtrans = m_followActor->GetTransform();
		SetActorTransform(newtrans);
	}

}

void AAbilityIndicator::ForceDestroy()
{
	if (Role != ROLE_Authority)
		return;
	m_lifeTime = m_duration;
	m_duration += 0.1f;
}


void AAbilityIndicator::Init(float duration, FVector offset, FRotator rotationOffset, FVector scale, bool center, AActor* followActor, FLinearColor color)
{
	m_duration = duration;
	m_followActor = followActor;
	m_offset = offset;
	m_rotationOffset = rotationOffset;
	m_scale = scale;
	m_color = color;
	m_center = center;
}

void AAbilityIndicator::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	CalculatePosition();
	SetDecalLocations();
	if (m_duration > 0.0f) // Only use timer if duration was set
	{
		m_lifeTime += DeltaTime;
		if (m_lifeTime >= m_duration)
		{
			if (Role == ROLE_Authority)
			{
				Destroy();
			}
		}
	}
}

void AAbilityIndicator::SetDecalLocations()
{

	if (Role != ROLE_Authority)
		return;
	if (IsValid(m_followActor))
	{
		if (m_center)
		{
			m_outlineDecal->SetActorLocation(m_followActor->GetActorLocation());
		}
		else if (IsValid(m_followActor))
		{

			m_outlineDecal->SetActorLocation(m_followActor->GetActorLocation());
			m_outlineDecal->AddActorLocalOffset(-m_offset);
		}
	}
	else
	{
		if (m_center)
		{
			m_outlineDecal->SetActorLocation(GetActorLocation());
		}
		else
		{
			m_outlineDecal->SetActorLocation(GetActorLocation());
			m_outlineDecal->AddActorLocalOffset(-m_offset);
		}
	}
}

void AFillingAbilityIndicator::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (m_duration > 0.0f) // Only use timer if duration was set
	{
		if (m_fillMat != nullptr)
			m_fillMat->SetScalarParameterValue(FName("AlphaMultiplier"), m_lifeTime / m_duration);
		if (m_lifeTime >= m_duration)
		{
			if (Role == ROLE_Authority)
			{
			}
		}
	}
}
void AAbilityIndicator::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
	Super::GetLifetimeReplicatedProps(OutLifetimeProps);

	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_followActor, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_duration, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_scale, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_offset, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_rotationOffset, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_color, COND_InitialOnly);
	DOREPLIFETIME_CONDITION(AAbilityIndicator, m_center, COND_InitialOnly);
}
void AAbilityIndicator::SetOffset(FVector offset)
{
	m_offset = offset;
}
void AAbilityIndicator::SetRotationOffset(FRotator rotationOffset)
{
	m_rotationOffset = rotationOffset;
}
void AFillingAbilityIndicator::SetDecalLocations()
{
	Super::SetDecalLocations();

	if (IsValid(m_followActor))
	{
		if (m_center)
		{
			m_fillDecal->SetActorLocation(m_followActor->GetActorLocation());
		}
		else if (IsValid(m_followActor))
		{
			m_fillDecal->SetActorLocation(m_followActor->GetActorLocation());
			m_fillDecal->AddActorLocalOffset(-m_offset);
		}
	}
	else
	{
		if (m_center)
		{
			m_fillDecal->SetActorLocation(GetActorLocation());
		}
		else
		{
			m_fillDecal->SetActorLocation(GetActorLocation());
			m_fillDecal->AddActorLocalOffset(-m_offset);
		}
	}
}