void MinusMinusCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();

	Variable variable = *vm.getVariable(parameters[1]);

	if (variable.getType() == VariableType::nulltype)
	{
		variable = *vm.getVariable(parameters[2]);
		parameters[1] = parameters[2];
	}

	if (variable.getType() != VariableType::nulltype && variable.getTokenType() == IToken::TYPE_NUMBER)
	{
		double number1 = atof(variable.getValue().c_str()) - 1;

		for (string & item : vm.getFunctionParametersByKey(parameters.at(1))) 
		{
			vm.setVariable(item, to_string(number1), supergeheimeToken, variable.getTokenType());
		}
		vm.setReturnValue(to_string(number1));
		vm.setReturnToken(variable.getTokenType());
	}
	else 
	{
		throwCustomError("cannot decrease an undefined variable.", vm, supergeheimeToken);
	}
}
void AddLengthToArrayCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	vector<string>& parameters = node.getContentArrayNonConstant();

	if (parameters.size() > 3)
	{
		string retVal = "";

		for (size_t i = 1; i < parameters.size()-1; i++)
		{
			if (i != parameters.size() - 2) 
			{
				retVal += vm.getVariable(parameters.at(i))->getValue() + ";";
			}
			else
			{
				retVal += vm.getVariable(parameters.at(i))->getValue();
			}
		}
		vm.setReturnValue(retVal);
	}
	else
	{
		vm.setReturnValue(vm.getVariable(parameters[1])->getValue());
	}
}
void TimesCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();

	Variable variable1 = *vm.getVariable(parameters.at(1));
	Variable variable2 = *vm.getVariable(parameters.at(2));

	if (isUndefined(variable1, variable2, vm))
	{
		return;
	}

	if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) 
	{
		double number1 = atof(variable1.getValue().c_str());
		double number2 = atof(variable2.getValue().c_str());

		vm.setReturnValue(to_string(number1 * number2));
		vm.setReturnToken(variable1.getTokenType());
	}
	else 
	{
		Variable variable1 = *vm.getVariable(parameters.at(1));
		Variable variable2 = *vm.getVariable(parameters.at(2));
		// Exception division requires 2 numbers
		throwCustomError("cannot multiply " + variable1.getValue() + " by " + variable2.getValue(), vm, supergeheimeToken);

		return;
	}
}
void CountCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();
	shared_ptr<Variable> var = vm.getVariable(parameters[1]);

	if (var->getTokenType() != IToken::TYPE_FACT_ARRAY && var->getTokenType() != IToken::TYPE_NUMBER_ARRAY && var->getTokenType() != IToken::TYPE_TEXT_ARRAY) 
	{
		throwCustomError("cannot count array " + var->getValue(), vm, supergeheimeToken);

		return;
	}
	shared_ptr<Array> array =  vm.getVariableArray(parameters[1]);

	if (array != nullptr) 
	{
		if (parameters.size() > 2) 
		{
			auto var = vm.getVariable(parameters[2]);

			if (var->getTokenType() == IToken::TYPE_NUMBER && var->getType() == VariableType::number) 
			{
				int index = atof(var->getValue().c_str());

				if (index < 0) 
				{
					throwCustomError("index is below zero.", vm, supergeheimeToken);
				}
				else if (index > (int)array->arraySizes.size() - 1) 
				{
					throwCustomError("index out of bounds range.", vm, supergeheimeToken);
				}
				else 
				{
					vm.setReturnValue(to_string(array->arraySizes.at(index)));
					vm.setReturnToken(IToken::TYPE_NUMBER);
				}
			}
			else 
			{
				throwCustomError("input is not a number.", vm, supergeheimeToken);
			}
		}
		else
		{
			vm.setReturnValue(to_string(array->variableArrayDictionary.size()));
			vm.setReturnToken(IToken::TYPE_NUMBER);
		}
	}
	else 
	{
		throwCustomError("array is not found.", vm, supergeheimeToken);
	}
}
void ModuloCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();

	Variable variable1 = *vm.getVariable(parameters.at(1));
	Variable variable2 = *vm.getVariable(parameters.at(2));

	if (isUndefined(variable1, variable2, vm))
	{
		return;
	}

	if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER)
	{
		int number1 = atoi(variable1.getValue().c_str());
		int number2 = atoi(variable2.getValue().c_str());

		vm.setReturnValue(to_string(number1 % number2));
		vm.setReturnToken(variable1.getTokenType());
	}
	else 
	{
		throwCustomError("cannot get remainder (modulo) " + variable1.getValue() + " from " + variable2.getValue(), vm,supergeheimeToken);

		return;
	}
}
void PlusCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	vector<string>& parameters = node.getContentArrayNonConstant();

	Variable variable1 = *vm.getVariable(parameters.at(1));
	Variable variable2 = *vm.getVariable(parameters.at(2));

	if (isUndefined(variable1, variable2, vm))
	{
		return;
	}

	if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER) 
	{

		double number1 = atof(variable1.getValue().c_str());
		double number2 = atof(variable2.getValue().c_str());

		vm.setReturnValue(to_string(number1 + number2));
		vm.setReturnToken(variable1.getTokenType());
	}
	else if (variable1.getTokenType() == IToken::TYPE_FACT && variable2.getTokenType() == IToken::TYPE_FACT)
	{
		bool bool1 = (variable1.getValue() == "true") ? true : false;
		bool bool2 = (variable2.getValue() == "true") ? true : false;

		bool outcome = bool1 + bool2;

		if (outcome)
		{
			vm.setReturnValue("true");
		}
		else
		{
			vm.setReturnValue("false");
		}
		vm.setReturnToken(variable1.getTokenType());
	}
	else 
	{
		string var1 = variable1.getValue();
		string var2 = variable2.getValue();

		if (variable1.getTokenType() == IToken::TYPE_NUMBER) 
		{
			var1 = removeUnnecessaryDotsAndZeros(var1);
		}

		if (variable1.getTokenType() == IToken::TYPE_NUMBER) 
		{
			var2 = removeUnnecessaryDotsAndZeros(var2);
		}
		vm.setReturnValue(var1 + var2);
		vm.setReturnToken(IToken::TYPE_TEXT);
	}
}
void ShowFunctionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	string val = "";
	vector<string>& parameters = node.getContentArrayNonConstant();

	if (parameters.size() >= 2) 
	{
		Variable variable2 = *vm.getVariable(parameters.at(1));

		if (variable2.getType() != VariableType::nulltype)
		{
			val += variable2.getValue();

			if (variable2.getTokenType() == IToken::TYPE_NUMBER)
			{
				val = removeUnnecessaryDotsAndZeros(val);
			}
		}
		val.erase(remove(val.begin(), val.end(), '\"'), val.end());
	}

	if (parameters.size() >= 3) 
	{
		Variable variable3 = *vm.getVariable(parameters.at(2));

		if (variable3.getType() != VariableType::nulltype)
		{
			val += " - "+variable3.getValue();

			if (variable3.getTokenType() == IToken::TYPE_NUMBER) 
			{
				val = removeUnnecessaryDotsAndZeros(val);
			}
		}
		val.erase(remove(val.begin(), val.end(), '\"'), val.end());
	}
	cout << val << endl;
}
void ShowUpFunctionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	vector<string>& parameters = node.getContentArrayNonConstant();

	Variable variable2 = *vm.getVariable(parameters.at(1));
	string val = "";

	if (variable2.getType() != VariableType::nulltype)
	{
		val += variable2.getValue();
	}
	val.erase(remove(val.begin(), val.end(), '\"'), val.end());
	cout << "UP: " + val << endl;
}
void RemoveDirectoryCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	vector<string>& parameters = node.getContentArrayNonConstant();
	string file = vm.getVariable(parameters[1])->getValue();
	file.erase(remove(file.begin(), file.end(), '\"'), file.end());

	if (getExtension(file) != "") 
	{
        shared_ptr<Token> tempToken = node.getToken();
		throwCustomError("Cannot remove files, only directories (use removeFile to delete a file)", vm, tempToken);

		return;
	}
	int result = -1;

	#ifdef _WIN32
		if (fs::exists(file.c_str()))
		{
			fs::remove_all(file.c_str());
			result = 0;
		}
	#else
		result = UnixRemoveDirectoryRecursive(file.c_str());
	#endif

	if (result == 0) 
	{
		cout << "Directory " << file << " and its contents were removed." << endl;
	}
	else 
	{
		char buff[256];
        
        #ifdef _WIN32
            strerror_s(buff, 100, errno);
        #else
            strerror_r(100, buff, errno);
        #endif
                
        shared_ptr<Token> tempToken = node.getToken();
		throwCustomError("Error: " + file + ": " + buff, vm, tempToken);
	}
}
void GetFileExtensionCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
	vector<string>& parameters = node.getContentArrayNonConstant();
	shared_ptr<Variable> variable = vm.getVariable(parameters.at(1));

	if (variable->getTokenType() == IToken::TYPE_TEXT) 
	{
		string fileName = variable->getValue();
		string::size_type idx;
		string fileExtension = "";
		idx = fileName.rfind('.');

		if (idx != string::npos)
		{
			fileExtension = "\"" + fileName.substr(idx + 1) + "\"";
		}
		vm.setReturnValue(fileExtension);
		vm.setReturnToken(variable->getTokenType());
	}
}
void GetFromValueCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
	vector<string>& parameters = node.getContentArrayNonConstant();
	string rValue = vm.getReturnValue();
	IToken rToken = vm.getReturnToken();

	if (&rValue != nullptr)
	{
		vm.setReturnValue("");
		vm.setReturnToken(IToken::ANY);

		if (vm.isAnIdentifier(rValue))
		{
			if (!vm.hasValueInFunctionParameters(parameters[1]))
			{
				vector<string> value = vm.getFunctionParametersByValue(rValue);

				if (value.size() > 0)
				{
					vm.setVariable(parameters[1], vm.getVariable(value.back())->getValue(), supergeheimeToken, rToken);
				}
				else
				{
					// Exception var undefined
					vm.setVariable(parameters[1], "", supergeheimeToken, rToken);
				}
				vm.setFunctionParameter(parameters[1], rValue);
			}
		}
		else
		{
			vm.setVariable(parameters[1], rValue, supergeheimeToken, rToken);
		}
	}
}
void SmallerEqualsToCommand::execute(VirtualMachine& vm, AbstractFunctionCall& node)
{
    auto supergeheimeToken = node.getToken();
    vector<string>& parameters = node.getContentArrayNonConstant();

    Variable variable1 = *vm.getVariable(parameters.at(1));
    Variable variable2 = *vm.getVariable(parameters.at(2));

    if (isUndefined(variable1, variable2, vm))
    {
        return;
    }

    if (variable1.getTokenType() == IToken::TYPE_NUMBER && variable2.getTokenType() == IToken::TYPE_NUMBER)
    {
        double number1 = atof(variable1.getValue().c_str());
        double number2 = atof(variable2.getValue().c_str());

        if (number1 <= number2)
        {
            vm.setReturnValue("true");
        }
        else
        {
            vm.setReturnValue("false");
        }
        vm.setReturnToken(IToken::TYPE_FACT);
    }
    else
    {
        // Exception "cannot compare different types than numbers"
        throwCustomError("cannot compare " + variable1.getValue() + " with " + variable2.getValue(), vm, supergeheimeToken);

        return;
    }
}
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 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 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();
		}
	}
}