#include "EmoShapeSubSystem/EmoShapeWebUtility/Public/ESFileComponent.h"
#include "HAL/PlatformFilemanager.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "UObject/UObjectGlobals.h"

#if PLATFORM_ANDROID
#include "Android/AndroidPlatformMisc.h"
#endif

UESFileComponent::UESFileComponent(const FObjectInitializer &init) : UActorComponent(init)
{
	bWantsInitializeComponent = true;
	bAutoActivate = true;
}

FString UESFileComponent::ProjectContentsDirectory()
{
	return FPaths::ProjectContentDir();
}

FString UESFileComponent::ProjectDirectory()
{
	return FPaths::ProjectDir();
}

FString UESFileComponent::ProjectSavedDirectory()
{
	return FPaths::ProjectSavedDir();
}

FString UESFileComponent::ExternalSaveDirectory()
{
#if PLATFORM_ANDROID
	return  FString(FAndroidMisc::GamePersistentDownloadDir());
#else
	return FPaths::ProjectSavedDir();
#endif
}

void UESFileComponent::SplitFullPath(const FString& InFullPath, FString& OutDirectory, FString& OutFileName)
{
	bool bDidSplit = InFullPath.Split(TEXT("/"), &OutDirectory, &OutFileName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);

	if (!bDidSplit) 
	{
		//search by backslash
		InFullPath.Split(TEXT("\\\\"), &OutDirectory, &OutFileName, ESearchCase::CaseSensitive, ESearchDir::FromEnd);
	}
}

void UESFileComponent::ProjectRelativePath(const FString& InFullPath, FString& OutProjectRelativePath)
{
	const FString PathToProject = ProjectDirectory();

	FString Before, After;

	InFullPath.Split(PathToProject, &Before, &After);

	OutProjectRelativePath = After;
}

bool UESFileComponent::SaveBytesToFile(const TArray<uint8>& Bytes, const FString& Directory, const FString& FileName,
	bool bLogSave)
{
	//bool AllowOverwriting = false;

	IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();

	if (PlatformFile.CreateDirectoryTree(*Directory))
	{
		FString AbsoluteFilePath;

		// Get absolute file path
		if (Directory.EndsWith(TEXT("/")))
		{
			AbsoluteFilePath = FPaths::ConvertRelativePathToFull(Directory + FileName);
		}
		else
		{
			AbsoluteFilePath = FPaths::ConvertRelativePathToFull(Directory + "/" + FileName);
		}

		// Allow overwriting or file doesn't already exist
		bool bSaveSuccesful = FFileHelper::SaveArrayToFile(Bytes, *AbsoluteFilePath);

		if (bLogSave)
		{
			if (bSaveSuccesful)
			{
				UE_LOG(LogTemp, Log, TEXT("Saved: %s with %d bytes"), *AbsoluteFilePath, Bytes.Num());
			}
			else
			{
				UE_LOG(LogTemp, Log, TEXT("Failed to save: %s"), *AbsoluteFilePath);
			}
		}

		return bSaveSuccesful;
	}

	return false;
}

bool UESFileComponent::ReadBytesFromFile(const FString& Directory, const FString& FileName, TArray<uint8>& OutBytes)
{
	// Get absolute file path
	FString AbsoluteFilePath = Directory + "/" + FileName;

	// Allow overwriting or file doesn't already exist
	return FFileHelper::LoadFileToArray(OutBytes, *AbsoluteFilePath);
}
