//======================================================================================================
// Company Name:       MotionTechnologies.inc
// Copyright Year:     2025
// File:               MoALiveLinkSettings.h
//======================================================================================================

#pragma once

#include "CoreMinimal.h"
#include "Engine/DeveloperSettings.h"
#include "Interfaces/IPv4/IPv4Endpoint.h"
#include "MoALiveLinkSettings.generated.h"

/**
 * Saved MoA LiveLink connection info
 */
USTRUCT()
struct FMoALiveLinkConnectionInfo
{
	GENERATED_BODY()

	/** IP Address of MoA server */
	UPROPERTY(config, EditAnywhere, meta = (DisplayName = "IP Address"))
	FString IPAddress = TEXT("127.0.0.1");

	/** Port number of MoA server */
	UPROPERTY(config, EditAnywhere, meta = (DisplayName = "Port", ClampMin = "1", ClampMax = "65535"))
	int32 Port = 7777;

	/** Enable this connection for auto-connect */
	UPROPERTY(config, EditAnywhere, meta = (DisplayName = "Auto Connect"))
	bool bEnabled = true;

	FIPv4Endpoint GetEndpoint() const
	{
		FIPv4Address Address;
		FIPv4Address::Parse(IPAddress, Address);
		return FIPv4Endpoint(Address, Port);
	}

	FString ToString() const
	{
		return FString::Printf(TEXT("%s:%d"), *IPAddress, Port);
	}
	
	bool operator==(const FMoALiveLinkConnectionInfo& Other) const
	{
		return IPAddress == Other.IPAddress && Port == Other.Port;
	}
};

/**
 * MoA LiveLink Plugin Settings
 * Configure automatic LiveLink source connection on editor startup
 */
UCLASS(config=MoALiveLink, defaultconfig, meta=(DisplayName="MoA LiveLink"))
class MOALIVELINK_API UMoALiveLinkSettings : public UDeveloperSettings
{
	GENERATED_BODY()

public:
	UMoALiveLinkSettings();

	/** Get the settings object */
	static UMoALiveLinkSettings* Get();

	/** Category name in Project Settings */
	virtual FName GetCategoryName() const override { return FName("Plugins"); }

	/** Enable automatic connection to MoA on editor startup */
	UPROPERTY(config, EditAnywhere, Category = "Connection", meta = (DisplayName = "Auto Connect on Startup"))
	bool bAutoConnectOnStartup = true;

	/** Remember last used connection settings */
	UPROPERTY(config, EditAnywhere, Category = "Connection", meta = (DisplayName = "Remember Connections"))
	bool bRememberLastConnection = true;

	/** Saved connections - supports multiple MoA sources */
	UPROPERTY(config, EditAnywhere, Category = "Connection", meta = (DisplayName = "Saved Connections", TitleProperty = "IPAddress"))
	TArray<FMoALiveLinkConnectionInfo> SavedConnections;

	/** Get all enabled endpoints */
	TArray<FIPv4Endpoint> GetEnabledEndpoints() const;

	/** Add or update a connection */
	void AddOrUpdateConnection(const FIPv4Endpoint& Endpoint);

	/** Remove a connection by endpoint */
	void RemoveConnection(const FIPv4Endpoint& Endpoint);

	void ClearAllConnections();

	/** Get the first enabled endpoint (for UI default) */
	FIPv4Endpoint GetDefaultEndpoint() const;

	/** Save connections to config file */
	void SaveConnectionsToConfig();

	/** Load connections from config file */
	void LoadConnectionsFromConfig();

#if WITH_EDITOR
	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
};

