// Project Sword & Gun Copyright © 2021 Kevin Poretti

#pragma once

#include "CoreMinimal.h"

#include "SwordNGun/SNGTypes.h"
#include "Weapons/SNGWeaponBase.h"
#include "SNGRangedWeaponBase.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWeaponFiredSignature);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnWeaponReloadedSignature);

/**
 * 
 */
UCLASS()
class SWORDNGUN_API ASNGRangedWeaponBase : public ASNGWeaponBase
{
	GENERATED_BODY()

public:
	ASNGRangedWeaponBase();

	UFUNCTION(BlueprintCallable)
	void StartFire();

	UFUNCTION(BlueprintCallable)
	void StopFire();

	UFUNCTION(BlueprintCallable)
	void Reload();

	UFUNCTION(BlueprintCallable)
	bool CanReload();

	UFUNCTION(BlueprintCallable)
	bool CanFire();

	UFUNCTION(BlueprintGetter)
	int32 GetCurrentAmmoCount();
	
protected:
	UPROPERTY(BlueprintAssignable, Category="Events")
	FOnWeaponFiredSignature OnFired;

	UPROPERTY(BlueprintAssignable, Category="Events")
	FOnWeaponReloadedSignature OnReloaded;

	UPROPERTY(VisibleDefaultsOnly, Category="Effects")
	FName MuzzleSocketName;

	UPROPERTY(VisibleDefaultsOnly, Category="Effects")
	FName TracerTargetName;

	UPROPERTY(EditDefaultsOnly, Category="Effects")
	UParticleSystem* MuzzleEffect;

	UPROPERTY(EditDefaultsOnly, Category="Effects")
	UParticleSystem* TracerEffect;

	UPROPERTY(EditDefaultsOnly, Category="Sounds")
	USoundBase* FireSound;
	
	// In rounds per minute
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Properties")
	float RateOfFire;

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Properties")
	int32 MaxAmmoCount;

	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Properties")
	float VulnerableDamageMultiplier;
	
	UPROPERTY(BlueprintReadOnly)
	int32 CurrentAmmoCount;

	UPROPERTY(EditDefaultsOnly, Category="Properties")
	FSNGHitData HitData;

	FTimerHandle TimerHandle_TimeBetweenShots;

	float LastFireTime;

	float TimeBetweenShots;

	virtual void BeginPlay() override;

	virtual void PlayImpactEffects(EPhysicalSurface SurfaceType, FVector ImpactPoint) override;
	
	virtual void PlayFireEffect(FVector TraceEnd);

	void Fire();

	UFUNCTION(BlueprintNativeEvent)
	void DoFire();
	
	virtual void DoFire_Implementation();
};
