// Project Sword & Gun Copyright © 2021 Kevin Poretti


#include "Weapons/SNGRangedWeaponBase.h"

ASNGRangedWeaponBase::ASNGRangedWeaponBase()
{
	MuzzleSocketName = "Muzzle";
	TracerTargetName = "TraceEnd";

	RateOfFire = 60.0f;
	MaxAmmoCount = 6;

	VulnerableDamageMultiplier = 2.0f;
}

void ASNGRangedWeaponBase::StartFire()
{
	// see if weapon is still in cooldown based on last time weapon was fired
	float FirstDelay = FMath::Max((LastFireTime + TimeBetweenShots) - GetWorld()->TimeSeconds, 0.0f);
	GetWorldTimerManager().SetTimer(TimerHandle_TimeBetweenShots, this, &ASNGRangedWeaponBase::Fire,
		TimeBetweenShots, true, FirstDelay);
}

void ASNGRangedWeaponBase::StopFire()
{
	GetWorldTimerManager().ClearTimer(TimerHandle_TimeBetweenShots);
}

void ASNGRangedWeaponBase::Reload()
{
	CurrentAmmoCount = MaxAmmoCount;
}

bool ASNGRangedWeaponBase::CanReload()
{
	return CurrentAmmoCount < MaxAmmoCount;
}

bool ASNGRangedWeaponBase::CanFire()
{
	return CurrentAmmoCount > 0;
}

int32 ASNGRangedWeaponBase::GetCurrentAmmoCount()
{
	return CurrentAmmoCount;
}

void ASNGRangedWeaponBase::BeginPlay()
{
	Super::BeginPlay();

	TimeBetweenShots = 60 / RateOfFire;
	CurrentAmmoCount = MaxAmmoCount;
}

void ASNGRangedWeaponBase::PlayImpactEffects(EPhysicalSurface SurfaceType, FVector ImpactPoint)
{
	Super::PlayImpactEffects(SurfaceType, ImpactPoint);

}

void ASNGRangedWeaponBase::PlayFireEffect(FVector TraceEnd)
{
}

void ASNGRangedWeaponBase::Fire()
{
	if(CanFire())
	{
		// sub class implementation goes here...but how do we want to do this?
		DoFire();
		
		LastFireTime = GetWorld()->TimeSeconds;
		CurrentAmmoCount--;
		// on weapon fired
		// on ammo count updated
	}
}

void ASNGRangedWeaponBase::DoFire_Implementation()
{
}
