// Project Sword & Gun Copyright © 2021 Kevin Poretti


#include "Components/SNGWeaponInventoryComponent.h"

// Sets default values for this component's properties
USNGWeaponInventoryComponent::USNGWeaponInventoryComponent()
{
	// don't tick...for now
	PrimaryComponentTick.bCanEverTick = false;

	// ...
}

int32 USNGWeaponInventoryComponent::GetNumRangedWeapons()
{
	return RangedWeapons.Num();
}

int32 USNGWeaponInventoryComponent::GetCurrentRangedWeaponIndex()
{
	return CurrRangedWeaponIdx;
}

ASNGRangedWeaponBase* USNGWeaponInventoryComponent::GetCurrentRangedWeapon()
{
	return RangedWeapons[CurrRangedWeaponIdx];
}

ASNGRangedWeaponBase* USNGWeaponInventoryComponent::SetCurrentRangedWeapon(int WeaponIndex)
{
	if(WeaponIndex > RangedWeapons.Num() - 1 || WeaponIndex < 0)
	{
		UE_LOG(LogTemp, Error, TEXT("SNGWeaponInventoryComponent :: Weapon index %d is not valid"), WeaponIndex);
		return nullptr;
	}

	CurrRangedWeaponIdx = WeaponIndex;
	return GetCurrentRangedWeapon();
}

int32 USNGWeaponInventoryComponent::GetNumMeleeWeapons()
{
	return MeleeWeapons.Num();
}

int32 USNGWeaponInventoryComponent::GetCurrentMeleeWeaponIndex()
{
	return CurrMeleeWeaponIdx;
}

ASNGMeleeWeaponBase* USNGWeaponInventoryComponent::GetCurrentMeleeWeapon()
{
	return MeleeWeapons[CurrMeleeWeaponIdx];
}

ASNGMeleeWeaponBase* USNGWeaponInventoryComponent::SetCurrentMeleeWeapon(int WeaponIndex)
{
	if(WeaponIndex > MeleeWeapons.Num() - 1 || WeaponIndex < 0)
	{
		UE_LOG(LogTemp, Error, TEXT("SNGWeaponInventoryComponent :: Weapon index %d is not valid"), WeaponIndex);
		return nullptr;
	}

	CurrMeleeWeaponIdx = WeaponIndex;
	return GetCurrentMeleeWeapon();
}

// Called when the game starts
void USNGWeaponInventoryComponent::BeginPlay()
{
	Super::BeginPlay();

	// ...
	for (auto WeaponClass : RangedWeaponClasses)
	{
		AActor* WeaponOwner = GetOwner();
		
		FActorSpawnParameters SpawnParams;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		SpawnParams.Owner = WeaponOwner;
		ASNGRangedWeaponBase* TempWeapon = GetWorld()->SpawnActor<ASNGRangedWeaponBase>(WeaponClass, WeaponOwner->GetActorLocation(), WeaponOwner->GetActorRotation(), SpawnParams);
		TempWeapon->SetVisibility(false);
		RangedWeapons.Add(TempWeapon);
	}
	
	for (auto WeaponClass : MeleeWeaponClasses)
	{
		AActor* WeaponOwner = GetOwner();
		
		FActorSpawnParameters SpawnParams;
		SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		SpawnParams.Owner = WeaponOwner;
		ASNGMeleeWeaponBase* TempWeapon = GetWorld()->SpawnActor<ASNGMeleeWeaponBase>(WeaponClass, WeaponOwner->GetActorLocation(), WeaponOwner->GetActorRotation(), SpawnParams);
		TempWeapon->SetVisibility(false);
		MeleeWeapons.Add(TempWeapon);
	}
}

