#pragma once
#include "PragmaGameServerSubsystem.h"
#include "PragmaResult.h"
#include "Dto/PragmaGameInstanceRpcDto.h"
#include "GameFramework/GameModeBase.h"
#include "Dom/JsonObject.h"
#include "CoreMinimal.h"
#include "Dom/JsonObject.h"
#include "Serialization/JsonSerializer.h"
#include "SampleGameGameModeBase.generated.h"

PRAGMA_FWD(PragmaServer);

class UAgonesSubsystem;

UCLASS()
class SAMPLEGAME_API APRGMSampleGameGameModeBase : public AGameModeBase
{
	GENERATED_BODY()

public:
	UFUNCTION()
	virtual void BeginPlay() override;

	void OnPlayersAddedTriggered(FString GameInstanceId, TArray<FPragma_GameInstance_GameServerPlayer> GameServerPlayers);
	void OnGameServerConnectedToPragma(const TPragmaResult<>& PragmaResult);
	void ShutdownServer();
	void ConnectToPragma();
	bool InitCommandLineParameters();
	void EndGameMethod();
	void GrantCurrency(FString PlayerId);

	void CheckForPayloadFile();
	bool ReadPayloadFile(const FString& FilePath);
	void OnAllocationPayloadReceived();

private:
	Pragma::FServerPtr PragmaServer;
	FString PragmaPartnerBackendAddress;
	float GameServerPort;
	FString GameServerHostname;
	FString PragmaGameInstanceId;
	FString PragmaPartnerSessionGameAuthToken;
	FString PragmaPartnerSessionSocialAuthToken;
	UPragmaGameServerSubsystem* ServerSubsystem;

	FTimerHandle PayloadPollTimerHandle;
	bool bAllocated = false;

	static constexpr const TCHAR* PayloadFilePath = TEXT("/app/shared/payload.json");
};
