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

#include "DataIf.h"
#include "../SceneFile.h"
#include "../DataTypes.h"

BEGIN_TEG_NAMESPACE();

SmartPointer<IData> DataIf::resolve(std::map<std::string, SmartPointer<IData>>& variables) {
	// Resolve the condition
	SmartPointer<IData> condResolved = cond->resolve(variables);
	if (condResolved == NULL) return NULL; // Check to be sure ..
	if (condResolved->type != DATA_BOOL) return NULL; // TODO: report in the log; the cond needed be a bool expression

	// Check the result condition 
	if (castToBool(condResolved)->value) {
		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;
			}
		}
	} else if (elseCond != NULL) {
		SmartPointer<IData> tmp = elseCond->resolve(variables);
		return tmp;
	}

	return NULL;
}

END_TEG_NAMESPACE();
