#pragma once

#include "CoreMinimal.h"
#include "WingServer.h"
#include "WingBasics.h"
#include "Misc/Paths.h"
#include "Misc/PackageName.h"
#include "HAL/FileManager.h"
#include "Asset_Backup.generated.h"


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

UCLASS()
class UWing_Asset_Backup : public UWingHandler
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, meta=(Description="Asset to back up"))
	FString Asset;

	virtual void Register() override
	{
		UWingServer::AddHandler(this,
			TEXT("Copy an asset's .uasset file to a .uasset.bak backup."));
	}
	virtual void Handle() override
	{
		FString Filename = FPaths::ConvertRelativePathToFull(
			FPackageName::LongPackageNameToFilename(Asset, FPackageName::GetAssetPackageExtension()));

		if (!IFileManager::Get().FileExists(*Filename))
		{
			WingOut::Stdout.Printf(TEXT("ERROR: Asset file not found: %s\n"), *Filename);
			return;
		}

		FString BackupFilename = Filename + TEXT(".bak");
		uint32 CopyResult = IFileManager::Get().Copy(*BackupFilename, *Filename, true);
		if (CopyResult != COPY_OK)
		{
			WingOut::Stdout.Printf(TEXT("ERROR: Failed to copy %s to %s\n"), *Filename, *BackupFilename);
			return;
		}

		WingOut::Stdout.Printf(TEXT("Backed up to %s\n"), *BackupFilename);
	}
};
