#pragma once

#include "CoreMinimal.h"
#include "sio_client.h"
#include "Dom/JsonValue.h"
#include "SocketIoClient.generated.h"

DECLARE_MULTICAST_DELEGATE(FOnSocketIoConnectedDelegate);
DECLARE_MULTICAST_DELEGATE_OneParam(FOnSocketIoMessageDelegate, const TSharedPtr<FJsonValue>&);

UCLASS()
class USocketIoClient : public UObject
{
	GENERATED_BODY()
public:
	void Connect(const FString& Url, const FString& Namespace);
	FOnSocketIoConnectedDelegate OnConnected;
	FOnSocketIoMessageDelegate& GetOnMessage(const FString& Message);
	void Emit(const FString& EventName, const TSharedPtr<FJsonValue>& Contents);
	bool IsConnected() const { return isConnected; }

private:
	void OnConnectionFailed();
	void OnConnectionClosed();
	void OnReconnecting();
	void OnMessage(sio::event& event);

	TMap<FString, FOnSocketIoMessageDelegate> OnMessageMap;
	TSharedPtr<sio::client> Client;
	sio::socket::ptr Socket;
	bool isConnected = false;
};
