void GetFilesInDirectoryByExtensionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	// TODO: DO EXTENSION STUFF
	vector<string>& parameters = node.getContentArrayNonConstant();
	auto var = vm.getVariable(parameters[1]);

	string extension = ".cpp";
	vector<string> out;
	DIR* dir;
	struct dirent* de;

	string directory = var->getValue();
	directory = directory.substr(1, directory.size() - 2);

	dir = opendir(directory.c_str()); /*your directory*/

	if (dir == nullptr) 
	{
		//dir is null dir not found
		throwCustomError("Directory not found! Cannot get files by extension..", vm, supergeheimeToken);

		return;
	}

	while (dir)
	{
		de = readdir(dir);

		if (!de) 
		{
			break;
		}

		if (getExtension(de->d_name) == extension) 
		{
			out.push_back(de->d_name);
		}
	}
	closedir(dir);

	string buffer;
	CompileSingleStatement varGetter = CompileSingleStatement();
	string localVariable;
	string arrayDictionary = varGetter.getNextLocalVariableName(buffer);
	string arrayIdentifier = varGetter.getNextLocalVariableName(buffer);
	vm.setVariable(arrayDictionary, "", supergeheimeToken, IToken::TYPE_TEXT_ARRAY);
	auto arrayVar = vm.getVariable(arrayDictionary);
	vm.setFunctionParameter(arrayDictionary, arrayIdentifier);
	int size = out.size();
	vm.addArrayToDictionary(arrayDictionary, vector<int>({size}));
	vm.addIdentifer(arrayIdentifier);

	for (size_t i = 0; i < out.size(); i++)
	{
		localVariable = varGetter.getNextLocalVariableName(buffer);
		vm.setVariable(localVariable, out.at(i), supergeheimeToken, IToken::TYPE_TEXT);
		cout << out.at(i) << endl;
		vm.addItemToVariableArrayAt(arrayDictionary, vector<string>({ to_string(i) }), vm.getVariable(localVariable));
	}
	vm.setReturnValue(arrayIdentifier);
	vm.setReturnToken(IToken::TYPE_TEXT_ARRAY);
}
void GetAllFilesInDirectoryCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();
	auto var = vm.getVariable(parameters[1]);
	string extension = "*.*";
	vector<string> out;
	DIR* dir = nullptr;
	struct dirent* dirent = nullptr;

	string directory = var->getValue();
	directory = directory.substr(1, directory.size() - 2);

	dir = opendir(directory.c_str()); // Target directory

	if (dir == nullptr) 
	{
		//throwTypeError(*var, *var, vm);
		//dir is null dir not found
		throwCustomError("Directory not found! Cannot get all files in directory..", vm, supergeheimeToken);
		return;
	}

	while (dir)
	{
		dirent = readdir(dir);

		if (!dirent)
		{
			break;
		}
		string direct = dirent->d_name;
		direct = directory + "\\" + direct;
		DIR* temp = opendir(direct.c_str());

		if (!temp) 
		{
			string fileName = dirent->d_name;
			out.push_back("\"" + fileName + "\"");
		}
		closedir(temp);
	}
	closedir(dir);
	
	string buffer;
	CompileSingleStatement varGetter = CompileSingleStatement();
	string localVariable;
	string arrayDictionary = varGetter.getNextLocalVariableName(buffer);
	string arrayIdentifier = varGetter.getNextLocalVariableName(buffer);

	vm.setVariable(arrayDictionary, "", supergeheimeToken, IToken::TYPE_TEXT_ARRAY);
	shared_ptr<Variable> arrayVar = vm.getVariable(arrayDictionary);

	vm.setFunctionParameter(arrayDictionary, arrayIdentifier);
	int Size = out.size();
	vm.addArrayToDictionary(arrayDictionary, vector<int>({ Size }));
	vm.addIdentifer(arrayIdentifier);

	for (size_t i = 0; i < out.size(); i++)
	{
		localVariable = varGetter.getNextLocalVariableName(buffer);
		vm.setVariable(localVariable, out.at(i), supergeheimeToken, IToken::TYPE_TEXT);
		vm.addItemToVariableArrayAt(arrayDictionary, vector<string>({ to_string(i) }), vm.getVariable(localVariable));
	}
	vm.setReturnValue(arrayIdentifier);
	vm.setReturnToken(IToken::TYPE_TEXT_ARRAY);
}
void AddItemToArrayAtCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();
	string arrayIdentifier = vm.getFunctionParameterValueByKey(parameters[1]);
	shared_ptr<Variable> valueParam = vm.getVariable(parameters.at(parameters.size() - 1));
	vector<shared_ptr<Variable>> arrayIndexesVariables;
	vector<string> arrayIndexes;
	shared_ptr<Variable> param2;

	for (size_t i = 2; i < parameters.size()-1; i++)
	{
		arrayIndexesVariables.push_back(vm.getVariable(parameters[i]));
		arrayIndexes.push_back(vm.getVariable(parameters[i])->getValue());
	}
	list<string> functionParams = vm.getFunctionParametersByKey(parameters[1]);
	string value = valueParam->getValue();
	string arrayKey = functionParams.back();

	for (auto it : arrayIndexesVariables) 
	{
		if (it->getType() != VariableType::number) 
		{
			shared_ptr<Error> error = make_shared<Error>("you want to set an item from an array, but the index isn't a number", ".md", supergeheimeToken->getLineNumber(), supergeheimeToken->getPosition(), ErrorType::ERROR);
			ErrorHandler::getInstance()->addError(error);
			vm.triggerRunFailure();
		}

		if (it->getValue() == "") 
		{
			shared_ptr<Error> error = make_shared<Error>("you want to add an item to an array, but the key is empty", ".md", supergeheimeToken->getLineNumber(), supergeheimeToken->getPosition(), ErrorType::ERROR);
			ErrorHandler::getInstance()->addError(error);
			vm.triggerRunFailure();
		}
	}

	if (value != "" && arrayKey != "")
	{
		VariableType arrayType = valueParam->getType();
		pair<string, string> arrayTypes = vm.getVariableTypeSameAsArrayType(arrayIdentifier, valueParam->getTokenType());

		if (arrayTypes.first != arrayTypes.second)
		{
            Variable var1 = Variable(arrayTypes.second);
            Variable var2 = Variable(arrayTypes.first);
			throwCustomError("cannot add " + var2.getValue() + " to array", vm, supergeheimeToken);
		}
		else
		{
			for (string& item : functionParams)
			{
				vm.addItemToVariableArrayAt(item, arrayIndexes, valueParam);
			}
		}
	}
	else
	{
		if (value == "") 
		{
			shared_ptr<Error> error = make_shared<Error>("you want to add an item to an array, but the value is empty", ".md", supergeheimeToken->getLineNumber(), supergeheimeToken->getPosition(), ErrorType::ERROR);
			ErrorHandler::getInstance()->addError(error);
			vm.triggerRunFailure(); 
		}
		else if (arrayKey == "") 
		{
			shared_ptr<Error> error = make_shared<Error>("you want to add an item to an array, but the array is undefined", ".md", supergeheimeToken->getLineNumber(), supergeheimeToken->getPosition(), ErrorType::ERROR);
			ErrorHandler::getInstance()->addError(error);
			vm.triggerRunFailure();
		}
	}
}