// Project Sword & Gun Copyright © 2021 Kevin Poretti

#pragma once

#include "CoreMinimal.h"
#include "SNGState.h"
#include "Components/ActorComponent.h"
#include "Interfaces/SNGEventProcessorInterface.h"

#include "SNGStateMachineComponent.generated.h"

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class SWORDNGUN_API USNGStateMachineComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	USNGStateMachineComponent();

	bool PushEvent(FSNGEvent Event);

	float GetTimeInState();
	
protected:
	USNGState* CurrentState;

	UPROPERTY(EditDefaultsOnly, Category="State Machine Config")
	TSubclassOf<USNGState> RootStateClass;

	UPROPERTY()
	TMap<FName, USNGState*> States;
	
	USNGState* CreateAndInitializeState(TSubclassOf<USNGState> StateClass);

	TQueue<FSNGEvent, EQueueMode::Mpsc> EventQueue;
	
	void AddStateToMap(USNGState* State);

	void TransitionToState(FName NextStateName);
	
	// Called when the game starts
	virtual void BeginPlay() override;
	
public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
