// __author__ = Juan Carlos De Abreu (jcabreur@gmail.com)

#include "DataWhile.h"
#include "../SceneFile.h"
#include "../DataTypes/DataBool.h"

BEGIN_TEG_NAMESPACE();

SmartPointer<IData> DataWhile::resolve(std::map<std::string, SmartPointer<IData>>& variables) {
	while(true) {
		// Check the condition
		SmartPointer<DataBool> condResolved = castToBool(cond->resolve(variables));
		if (condResolved == NULL) return NULL;	// TODO: report un the log the function cannot be evaluate in the while ... print...
		if (!condResolved->value) return newData(true);	// break the while

		// Make the instructions
		const int nInst = static_cast<int> (inst.size());
		for (int i = 0; i < nInst; ++i) {
			SmartPointer<IData> tmp = inst[i]->resolve(variables);
			if (SceneFileManagement::GetInstance().isReturning) {
				return tmp;
			}
		}
	}
}

END_TEG_NAMESPACE();
