///////////////////////////////////////////////////////////////////////////////
//  FileSystem.cpp

#ifdef WIN32
#include <windows.h>
#endif

#include "../../Independent/CharacterCode/IsCode.h"
#include "../../Specific/FileSystem/LFileName.h"
#include "CFindFile.h"
#include "FileSystem.h"

namespace filesystem {

bool CreateFolder(const std::string& FolderName)
{
	if (CFindFile::IsExistDir(FolderName)) return true;

	// ђeѓtѓHѓ‹ѓ_‚ЙЌД‹A‚·‚й
	const std::string ParentPath = LFileName::GetParent(FolderName);
	if (!ParentPath.empty()) {
		if (!CreateFolder(ParentPath)) return false;
	}

	if (FolderName.length() == 3 && IsAlpha(FolderName[0]) && FolderName[1] == ':' && FolderName[2] == '\\') {
		// ѓhѓ‰ѓCѓuѓ‹Ѓ[ѓg‚НЌмђ¬‚Е‚«‚И‚ў‚М‚ЕЃA‘¶ЌЭ‚·‚й‚Ж‚Э‚И‚·
		// ‚±‚МѓЃѓ\ѓbѓhђж“Є‚МѓtѓHѓ‹ѓ_‘¶ЌЭЉm”F‚Еtrue‚Є•Ф‚й‚ЖЋv‚¤‚ЄЃA”O‚М‚Ѕ‚ЯЃB
		return true;
	}

	// ѓTЃ[ѓoѓ‹Ѓ[ѓg‚НЌмђ¬‚Е‚«‚И‚ў‚М‚ЕЃA‘¶ЌЭ‚·‚й‚Ж‚Э‚И‚·
	// ‚±‚МѓЃѓ\ѓbѓhђж“Є‚МѓtѓHѓ‹ѓ_‘¶ЌЭЉm”F‚Еtrue‚Є•Ф‚й‚ЖЋv‚¤‚ЄЃA”O‚М‚Ѕ‚ЯЃB

	// ѓtѓHѓ‹ѓ_‚МЌмђ¬
#ifdef WIN32
	if (!::CreateDirectory(FolderName.c_str(), NULL)) return false;
#endif
	return true;
}

bool DeleteFolder(const std::string& FolderName)
{
	if (!CFindFile::IsExistDir(FolderName)) return true;

	// ѓtѓ@ѓCѓ‹ѓЉѓXѓg‚рЋж“ѕ‚·‚й
	std::vector<std::string> vecszList;
	CFindFile::GetFileList(FolderName, vecszList);

	//
	std::vector<std::string>::const_iterator it = vecszList.begin();
	for (; it != vecszList.end(); ++it) {
		const std::string szPath = LFileName::AddPunct(FolderName) + *it;

		if (CFindFile::IsExistDir(szPath)) {
			// ЌД‹A
			if (!DeleteFolder(szPath)) return false;
		}
		else {
			if (!DelFile(szPath)) return false;
		}
	}

	// Ћ©•ЄЋ©ђg‚МѓtѓHѓ‹ѓ_‚рЌнЏњ‚·‚й
#ifdef WIN32
	if (!::RemoveDirectory(FolderName.c_str())) return false;
#endif

	return true;
}

bool DelFile(const std::string& FileName)
{
#ifdef WIN32
	return (::DeleteFile(FileName.c_str()) != FALSE);
#else
	return false;
#endif
}

bool CopyFolder(const std::string& DestFolderName, const std::string& SrcFolderName)
{
	if (!CFindFile::IsExistDir(SrcFolderName)) return false;

	if (!CreateFolder(DestFolderName)) return false;

	// ЋqѓtѓHѓ‹ѓ_
	std::vector<std::string> FolderNameList;
	CFindFile::GetFolderList(SrcFolderName, FolderNameList);

	for (const auto& FolderName : FolderNameList) {
		if (!CopyFolder(DestFolderName + "\\" + FolderName, SrcFolderName + "\\" + FolderName)) return false;
	}

	// Ћqѓtѓ@ѓCѓ‹
	std::vector<std::string> FileNameList;
	CFindFile::GetFileList(SrcFolderName, FileNameList);

	for (const auto& FileName : FileNameList) {
		if (!CopyOneFile(DestFolderName + "\\" + FileName, SrcFolderName + "\\" + FileName)) return false;
	}

	return true;
}

bool CopyOneFile(const std::string& DestFileName, const std::string& SrcFileName)
{
#ifdef WIN32
	if (!::CopyFile(SrcFileName.c_str(), DestFileName.c_str(), FALSE)) return false;
#endif

	return true;
}

std::string GetCurrentFolderName()
{
#ifdef WIN32
	char FolderName[MAX_PATH + 1] = "";

	::GetCurrentDirectory(MAX_PATH, FolderName);
	return std::string(FolderName);
#else
	return "";
#endif
}

} // end of namespace filesystem

