#pragma once

#include "CoreMinimal.h"
#include "UIActivatableScreen_Base.h"
#include "ZMMOCharSummary.h"
#include "UIUserLobbyScreen_Base.generated.h"

class UCommonTextBlock;
class UBorder;
class UPanelWidget;
class UWidgetSwitcher;
class UUIButton_Base;
class UUICharCard_Base;
class UUICharacterCreatePage_Base;
class UZeusCharServerSubsystem;

/**
 * Tela Lobby (host) — Fase 1.5 do ARQUITETURA_SERVER_SELECT.
 *
 * Fluxo:
 *  1. NativeOnActivated: pega `SelectedWorldId` do FlowSubsystem, envia
 *     C_CHAR_LIST_REQUEST filtrado por esse mundo
 *  2. Parse S_CHAR_LIST -> Chars[]; instancia 1 WBP_CharCard por entry
 *  3. Card "Selecionar" -> C_CHAR_SELECT(charId) -> S_CHAR_SELECT_OK ->
 *     handoff UDP pro WorldServer
 *  4. Botao "Criar Personagem" -> mostra page interna `CharacterCreate`
 *  5. Botao "Voltar" -> ClearSelectedWorld + Flow.SetState(ServerSelect)
 *
 * Pages internas via UWidgetSwitcher:
 *   - PageIndex 0: Lista de chars (CardContainer)
 *   - PageIndex 1: Form de criar personagem (CreatePage)
 */
UCLASS(Abstract, Blueprintable)
class ZMMO_API UUIUserLobbyScreen_Base : public UUIActivatableScreen_Base
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable, Category = "Zeus|Lobby")
	void RequestCharList();

	UFUNCTION(BlueprintCallable, Category = "Zeus|Lobby")
	void ShowCharSelectPage();

	UFUNCTION(BlueprintCallable, Category = "Zeus|Lobby")
	void ShowCharCreatePage();

	UFUNCTION(BlueprintCallable, Category = "Zeus|Lobby")
	void BackToServerSelect();

	UPROPERTY(BlueprintReadOnly, Category = "Zeus|Lobby")
	TArray<FZMMOCharSummary> Chars;

	UFUNCTION(BlueprintImplementableEvent, Category = "Zeus|Lobby")
	void OnCharListReceived();

protected:
	virtual void NativeOnActivated() override;
	virtual void NativeOnDeactivated() override;

	UFUNCTION()
	void HandleCharRawMessage(int32 Opcode, const TArray<uint8>& Payload);

	UFUNCTION()
	void HandleCardSelected(FString CharId);

	UFUNCTION()
	void HandleCardDeleteRequest(FString CharId);

	UZeusCharServerSubsystem* GetCharServer() const;
	void ParseCharList(const TArray<uint8>& Payload);
	void HandleCharSelectOk(const TArray<uint8>& Payload);
	void HandleCharSelectReject(const TArray<uint8>& Payload);
	void HandleCharCreateOk(const TArray<uint8>& Payload);
	void HandleCharCreateReject(const TArray<uint8>& Payload);
	void HandleCharDeleteAck(const TArray<uint8>& Payload);
	void HandleCharDeleteAcceptAck(const TArray<uint8>& Payload);
	void HandleCharDeleteCancelAck(const TArray<uint8>& Payload);

	UFUNCTION()
	void HandleCardAcceptDeleteRequest(FString CharId);

	UFUNCTION()
	void HandleCardCancelDeleteRequest(FString CharId);

	void SendCharIdOpcode(int32 Opcode, const FString& CharId);
	void RebuildCards();

	/** Container de cards (ScrollBox/GridPanel/UniformGridPanel). */
	UPROPERTY(meta = (BindWidget)) TObjectPtr<UPanelWidget> CardContainer;

	/** Switcher entre Lista (0) e Criar (1). */
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UWidgetSwitcher> PageSwitcher;

	/** Page de criacao (WBP_CharacterCreate) — opcional, pode estar embedado. */
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUICharacterCreatePage_Base> CharCreatePage;

	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_Title;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UCommonTextBlock> Text_WorldName;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUIButton_Base>   Btn_Back;
	UPROPERTY(meta = (BindWidgetOptional)) TObjectPtr<UUIButton_Base>   Btn_Create;

	/** Classe do card a instanciar. */
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Zeus|Lobby")
	TSubclassOf<UUICharCard_Base> CharCardClass;

private:
	bool bSubscribed = false;
	bool bButtonsBound = false;

	UPROPERTY(Transient)
	TArray<TObjectPtr<UUICharCard_Base>> SpawnedCards;
};
