#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "ZMMOGameInstance.generated.h"

class UZeusNetworkSubsystem;

/**
 *  UZMMOGameInstance
 *
 *  Game instance do cliente Zeus MMO. O `UZeusNetworkSubsystem` (do plugin
 *  `ZeusNetwork`) e um `UGameInstanceSubsystem` e portanto e criado
 *  automaticamente em qualquer `UGameInstance`; esta classe existe sobretudo
 *  como ponto de extensao para:
 *
 *    - Auto-connect ao servidor Zeus em `Init()` (`bAutoConnectOnStart`).
 *    - Expor a configuracao default (`Host`, `Port`) ao Project Settings.
 *    - Hooks de logout / shutdown limpos em `Shutdown()`.
 *
 *  Marcada `Blueprintable, BlueprintType` para permitir um BP filho com
 *  overrides de configuracao por build (dev / staging / prod).
 */
UCLASS(Blueprintable, BlueprintType)
class ZMMO_API UZMMOGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:
	virtual void Init() override;
	virtual void Shutdown() override;

	/**
	 *  Se `true`, tenta ligar ao servidor Zeus em `Init()` usando
	 *  `ZeusServerHost`/`ZeusServerPort`. Default `false`: a conexao agora e
	 *  dirigida pelo `UUIFrontEndFlowSubsystem` (fluxo Boot→Connecting→Login).
	 *  Pôr `true` apenas para smoke test legado sem front-end.
	 */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Config, Category = "ZMMO|Network")
	bool bAutoConnectOnStart = false;

	/** Host (IPv4 ou hostname) do servidor Zeus. */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Config, Category = "ZMMO|Network")
	FString ZeusServerHost = TEXT("127.0.0.1");

	/** Porta UDP do servidor Zeus (1..65535). */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Config, Category = "ZMMO|Network", meta = (ClampMin = "1", ClampMax = "65535"))
	int32 ZeusServerPort = 27777;

protected:
	/** Devolve o subsystem Zeus desta game instance (criado automaticamente pelo plugin). */
	UFUNCTION(BlueprintPure, Category = "ZMMO|Network")
	UZeusNetworkSubsystem* GetZeusNetwork() const;

	void BindZeusDelegates(UZeusNetworkSubsystem* Zeus);
	void UnbindZeusDelegates(UZeusNetworkSubsystem* Zeus);

	UFUNCTION()
	void HandleZeusConnected();

	UFUNCTION()
	void HandleZeusConnectionFailed(FString Reason);

	UFUNCTION()
	void HandleZeusDisconnected();

	UFUNCTION()
	void HandleZeusNetworkLog(FString Message);
};
