/*
* Изтрива всички директории в параметрите.
* Root директорият не може да се изтрие.
*/
void CommandPrompt::deleteDirectory(string parameters){
	/*
	* Извличаме адресите от параметрите.
	*/
	queue<string> dirPaths = getPaths(parameters);

	/*
	* Намираме директория по дадения път и викаме deleteFile().
	* Ако е NULL извеждаме грешка. 
	* Ако текущата директория се съдържа в директорията която трием, я применяме на root.
	* Root директорията не може да се изтрие.
	*/
	Directory* dir;
	while (!dirPaths.empty()) {
		dir = this->determinePathAndGetDir(dirPaths.front());
		if (dir != NULL) {
			if(currentDir->getFSysPath().find(dir->getFSysPath()) != string::npos){
				currentDir = fs.getRoot();
				cout << "Current directory now root!" << endl;
			}
			if (dir == fs.getRoot())
				cerr << "Cannot delete root directory!" << endl;
			else
				dir->deleteFile();
		}
		else
			cerr << "Directory not found!" << endl;
		dirPaths.pop();
	}
}
/*
* Помощтна функция, за запазване/въвеждане на файлове.
*/
void CommandPrompt::saveConcatenatedFile(string path, string data){
	/*
	* Ако няма данни и няма подаден адрес за запис връща.
	*/
	if (path.empty() && data.empty())
		return;

	/*
	* Ако няма данни се въвеждат.
	*/
	if (data.empty()) {
		char line[1000];
		string sline;
		while (sline != string(".")) {
			cin.getline(line, 1000);
			sline = line;
			if(sline != ".")
				data += sline;
		}
	}

	/*
	* Ако няма outputFile се извеждат данните, ако има
	* се намира/създава директория и се създава вайл в нея.
	* Наследява промените по големината на директориите.
	*/
	if (path.empty())
		cout << data << endl;
	else {
		string fileName = path;
		Directory* dir = fs.getRoot();
		int pos = path.rfind('/') + 1;
		if (pos - 1 != string::npos) {
			fileName = "";
			while (pos < path.size())
				fileName += path[pos++];
			pos = path.rfind('/');
			path = path.erase(pos, path.size());
			dir = this->createNestedDirectories(path);
		}
		else
			dir = currentDir;
		if (dir->nameAvailable(fileName)) {
			dir->addChild(new TextFile(dir->getFSysPath(),
				fs.getNextNumber(),
				data,
				fileName));
			dir->inheritSize(data.size());
		}
		else {
			dir->findFileByName(fileName)->concatData(data);
			dir->inheritSize(data.size());
		}
	}
}
/*
* Създава линкове на файловете в подадена директория.
*/
void CommandPrompt::createLinksInDirectory(string parameters){
	stack<string> filePaths;
	string tempPath;
	/*
	* Извлича адресите в стек (защото последния адрес е директория).
	*/
	for (int i = 0; i <= parameters.size(); i++) {
		if (parameters[i] == ' ' || i == parameters.size()) {
			filePaths.push(tempPath);
			tempPath = "";
		}
		else {
			tempPath += parameters[i];
		}
	}

	/*
	* Намира директорията. Ако не я намери извежда грешка и връща.
	*/
	Directory* destinationDir = this->determinePathAndGetDir(filePaths.top());
	filePaths.pop();
	if (destinationDir == NULL) {
		cerr << "Destination directory not found!" << endl;
		return;
	}

	/*
	* Намира файловете по адрес и създава линкове към тях в директорията.
	*/
	TextFile* tempFile;
	while (!filePaths.empty()) {
		tempFile = this->determinePathAndGetFile(filePaths.top());
		if (tempFile == NULL) {
			cerr << "File not found!" << endl;
		}
		else {
			destinationDir->addChild(new SymLink(tempFile, fs.getNextNumber(), destinationDir->getFSysPath()));
		}
		filePaths.pop();
	}
}
/*
* Определя какъв е адреса и го обхожда име по име.
* Ако намери директория която не съществува я създава.
* @return - търсената директория, ако поради някакви причини не се е създала връща root.
*/
Directory* CommandPrompt::createNestedDirectories(string path) {
	string currentName;
	Directory* result = NULL;
	Directory* tempDir = fs.getRoot();
	if (path.find('/') == string::npos) {
		if ((tempDir = tempDir->findDirByName(path)) == NULL) {
			result = new Directory(currentDir->getFSysPath(),
				fs.getNextNumber(),
				path,
				currentDir);
			currentDir->addChild(result);
			return result;
		}
			return tempDir;
	}
	else {
		for (int i = 0; i <= path.size(); i++) {
			if ((path[i] == '/' || i == path.size()) && !currentName.empty()) {	
				if (tempDir->findDirByName(currentName) == NULL) {
					result = new Directory(tempDir->getFSysPath(),
						fs.getNextNumber(),
						currentName,
						tempDir);
					tempDir->addChild(result);
				}
				tempDir = tempDir->findDirByName(currentName);
				currentName = "";
			}
			else if(path[i] != '/') {
				currentName += path[i];
			}
		}
	}
	if (result == NULL)
		return tempDir;
	return result;
}