// Project Lab - NHTV Igad

#pragma once

#include "GUI/Menu/SubMenu.h"
#include "CharacterSettings.h"
#include "LobbyCharacterSelect.generated.h"

/**
 * 
 */
UCLASS()
class UNREALPROJECT_API ULobbyCharacterSelect : public USubMenu
{
	GENERATED_BODY()
	
public:
	virtual void NativeConstruct() override;
	virtual void NativeDestruct() override;
	virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void Init();

	// On next/previous player character
	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void OnNextCharacter();
	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void OnPreviousCharacter();

	// Called to set the name of the selected character
	UFUNCTION(BlueprintNativeEvent, Category = "Lobby")
	void UpdateCharacter(FCharacterSave save);

	// Called to update all visual characters in the lobby (specify a controller if one player only requires a visual update, or nullptr if all characters need to be updated)
	UFUNCTION(BlueprintCallable, Category = "Lobby")
	void UpdateVisualCharacters(class APlayerControllerBase* playerController);

	UPROPERTY(EditAnywhere, Category = "Lobby")
	TSubclassOf<class ACharacterBase> characterBlueprint;

	UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Character Sub Menu")
	class UCharacterClassPropertySet* characterClassProperties;

private:
	void m_OnPlayerJoined(APlayerController* pc);
	FDelegateHandle m_onPlayerJoinedDH;

	TArray<FCharacterSave> m_playerCharacterSaves;
	int32 m_selection;

	TMap<int32, TArray<ALobbySpawn*>> m_spawnPoints;
	struct VisualSlot
	{
		VisualSlot(int32 team, int32 slot, ACharacterBase* character) : team(team), slot(slot), character(character) {}
		int32 team;
		int32 slot;
		ACharacterBase* character;
	};
	TMap<APlayerStateBase*, VisualSlot> m_visualCharacters;

	void m_ApplyCharacter();
	bool m_requireUpdate;
	float m_updateInterval;
};
