// Copyright CodeSpartan, 2022. All rights reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "GameModeBaseCpp.generated.h"

/**
 * 
 */
UCLASS()
class MMOKITCODE_API AGameModeBaseCpp : public AGameModeBase
{
	GENERATED_BODY()

public:
	virtual void InitGameState() override;

	virtual ~AGameModeBaseCpp() override;

#if UE_EDITOR
	void HandleEndPIE(const bool InIsSimulating);
#endif

	/** Called by game mode when PIE stops, so we can save our characters.Only for Editor. */
	UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "OnEndPIE"), Category = "Default")
	void K2_OnEndPIE();

	/** Default PreLogin will reject a OnlineSubsystemSteam client from a OnlineSubstemNull server, we need to override this behavior */
	virtual void PreLogin(const FString& Options, const FString& Address, const FUniqueNetIdRepl& UniqueId, FString& ErrorMessage) override;

	/** Interrupts the default spawning of the player's pawn and initiates a different routine */
	virtual void RestartPlayerAtPlayerStart(AController* NewPlayer, AActor* StartSpot) override;

	/** Once we have spawned the player's pawn, call this method to continue what we interrupted in RestartPlayerAtPlayerStart() */
	UFUNCTION(BlueprintCallable, Category = "Default")
	void ContinuePlayerStart(AController* NewPlayer, APawn* NewPawn, FRotator SpawnRotation);

	/** Or alternatively, if we couldn't verify the player's credentials, we abort the PlayerStart and disconnect him */
	UFUNCTION(BlueprintCallable, Category = "Default")
	void AbortPlayerStart(AController* NewPlayer);

	UFUNCTION(BlueprintCallable, Category = "Default")
	void KickPlayer(APlayerController* PlayerController);

	UFUNCTION(BlueprintPure, Category = "Default")
	FString ServerPassword() {return _serverPassword; }

#if UE_SERVER || UE_EDITOR
	FString _serverPassword = FString("$2a$12$TVJguxncMvNQS47S5jWw3exf.89gOM43q9omQt58cS1mfn6MrWg8.");
#else
	FString _serverPassword = FString("");
#endif

private:
#if UE_EDITOR
	FDelegateHandle OnEndPieHandle;
#endif
};
