// Fill out your copyright notice in the Description page of Project Settings.


#include "EVGameInstance.h"

void UEVGameInstance::OnStart()
{
	Super::OnStart();
	GlobalSessionID = FGuid::NewGuid().ToString().Left(5).ToUpper();
	IsValidSessionID(GlobalSessionID);
}

void UEVGameInstance::IsValidSessionID(const FString& SessionID)
{
	const auto Request = FHttpModule::Get().CreateRequest();
	Request->SetURL("https://ev.api.philipwhite.dev/api/session/" + SessionID);
	Request->SetVerb("GET");
	Request->OnProcessRequestComplete().BindUObject(this, &UEVGameInstance::OnResponseReceived);
	Request->ProcessRequest();
}

void UEVGameInstance::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnectedSuccessfully)
{
	if (Response->GetResponseCode() == 200)
	{
		GLog->Log("Session ID Exists Regenerating");
		GlobalSessionID = FGuid::NewGuid().ToString().Left(5).ToUpper();
		IsValidSessionID(GlobalSessionID);
		return;
	}
	GLog->Log("Session ID is Valid");
}
