#include "StdAfx.h"
#include "fs/WritableFolderPack.h"
#include "fs/NativeFilesystem.h"
#include "Logger.h"

namespace fs
{
//////////////////////////////////////////////////////////////////////////

WritableFolderPack::WritableFolderPack(AbsPathView location)
	: FolderPack(std::move(location))
{
	MakeFolderResult result = fs::native.makeFolder(m_location);
	if (result.has_error())
		LOGW("Could not create WritablePackFolder {} due to error {} ", m_location, result.error());
}

//////////////////////////////////////////////////////////////////////////

WritableFolderPack::WritableFolderPack(tl::lent_ref<IFilesystem> filesystem, AbsPathView location)
	: FolderPack(std::move(filesystem), std::move(location))
{
	MakeFolderResult result = m_filesystem->makeFolder(m_location);
	if (result.has_error())
		LOGW("Could not create WritablePackFolder {} due to error {} ", m_location, result.error());
}

//////////////////////////////////////////////////////////////////////////

OpenSinkResult WritableFolderPack::openSink(AbsPathView path, Mode mode, SinkFlags flags)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->openSink(npath, mode, flags);
}

//////////////////////////////////////////////////////////////////////////

OpenStreamSinkResult WritableFolderPack::openStreamSink(AbsPathView path, Mode mode, SinkFlags flags)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->openStreamSink(npath, mode, flags);
}

//////////////////////////////////////////////////////////////////////////

OpenMapSinkResult WritableFolderPack::openMapSink(AbsPathView path, Mode mode, size_t size, SinkFlags flags)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->openMapSink(npath, mode, size, flags);
}

//////////////////////////////////////////////////////////////////////////

RemoveResult WritableFolderPack::remove(AbsPathView path)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->remove(npath);
}

//////////////////////////////////////////////////////////////////////////

RenameResult WritableFolderPack::rename(AbsPathView path, AbsPathView newPath)
{
	OUTCOME_TRY(const auto filePath, convertToNativePath(path));
	OUTCOME_TRY(const auto newFilePath, convertToNativePath(newPath));
	return m_filesystem->rename(filePath, newFilePath);
}

//////////////////////////////////////////////////////////////////////////

MakeFolderResult WritableFolderPack::makeFolder(AbsPathView path)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->makeFolder(npath);
}

//////////////////////////////////////////////////////////////////////////

RemoveRecursivelyResult WritableFolderPack::removeRecursively(AbsPathView path)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->removeRecursively(npath);
}

//////////////////////////////////////////////////////////////////////////

SetWriteTimeResult WritableFolderPack::setWriteTime(AbsPathView path, time_t time)
{
	OUTCOME_TRY(const auto npath, convertToNativePath(path));
	return m_filesystem->setWriteTime(npath, time);
}

//////////////////////////////////////////////////////////////////////////
}
