// Fill out your copyright notice in the Description page of Project Settings.

#include "PDB_viewer.h"
#include "FileBrowser.h"


TArray<FString> UFileBrowser::GetFoldersAt(FString path)
{

	FPaths::NormalizeDirectoryName(path); //turns 'C:\' into 'C:/'
	IFileManager& fileManager = IFileManager::Get();

	TArray<FString> out;
	if (path.IsEmpty()) {
		//show hard drive letters - C:, D: etc
		for (char c = 'A'; c <= 'Z'; c++)
		{
			FString DiskLetter;
			DiskLetter.AppendChar(c);
			DiskLetter.AppendChar(':');
			if (fileManager.DirectoryExists(*DiskLetter))
				out.Add(DiskLetter);
		}
	}
	else {
		//show subfolders located under "path"
		out.Add("..");

		TArray<FString> folders;
		FString queryFolders = path + "/*";
		fileManager.FindFiles(folders, *queryFolders, false, true);

		out.Append(folders);
	}
	return out;
}

TArray<FString> UFileBrowser::GetFilesAt(FString path)
{
	FPaths::NormalizeDirectoryName(path); //turns 'C:\' into 'C:/'
	IFileManager& fileManager = IFileManager::Get();

	TArray<FString> out;
	if (path.IsEmpty()) {
		//showing drives - no files should be shown
		return out;
	}
	else {
		//show files located under "path"
		TArray<FString> files;
		FString queryFiles = path + "/*"; //TODO many extensions at once
		fileManager.FindFiles(files, *queryFiles, true, false);

		//out.Append(files);
		for (FString file : files)
		{
			if (file.EndsWith(".pdb"))
				out.Add(file);
		}
	}
	return out;
}