#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "Http.h"
#include "SocketIONative.h"
#include "CMGameInstance.generated.h"

/**
 * 
 */
UCLASS()
class CORRUPTEDMEMORY_API UCMGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:
	DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLoginComplete, const bool, bWasSuccessful);

	DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRegisterComplete, const bool, bWasSuccessful);

	DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnLogoutComplete, const bool, bWasSuccessful);

	DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnJoinLobbyComplete, const bool, bWasSuccessful);

	UPROPERTY(BlueprintAssignable, Category = "Account")
	FOnLoginComplete OnLoginComplete;
	UPROPERTY(BlueprintAssignable, Category = "Account")
	FOnRegisterComplete OnRegisterComplete;
	UPROPERTY(BlueprintAssignable, Category = "Account")
	FOnLogoutComplete OnLogoutComplete;
	UPROPERTY(BlueprintAssignable, Category = "Lobby")
	FOnJoinLobbyComplete OnJoinLobbyComplete;

	UPROPERTY(BlueprintReadOnly, Category = "Account")
	FString AccountUsername;
	UPROPERTY(BlueprintReadOnly, Category = "Account")
	FString JwtToken;

	UPROPERTY(BlueprintReadOnly, Category = "Lobby")
	FString ServerFQDN = "cm.api.philipwhite.dev";
	UPROPERTY(BlueprintReadOnly, Category = "Lobby")
	FString LobbyID;
	UPROPERTY(BlueprintReadOnly, Category = "Lobby")
	int ServerPort;

	UFUNCTION(BlueprintCallable, Category = "Account")
	void Login(const FString& Username, const FString& Password);
	UFUNCTION(BlueprintCallable, Category = "Account")
	void Register(const FString& Username, const FString& Password);
	UFUNCTION(BlueprintCallable, Category = "Account")
	void Logout();
	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void CreateLobby();
	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void JoinLobby(const int Port);

	virtual void Init() override;
	virtual void Shutdown() override;
	TSharedPtr<FSocketIONative> Socket;

private:
	FString LobbyAPI = "https://cm.api.philipwhite.dev/api";
	void OnRequestToLoginComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
	void OnRequestToRegisterComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful) const;
	void OnRequestToLogoutComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
	void OnRequestToCreateLobbyComplete(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
};
