#include "stdafx.h"
#include "ResultDTOIteratorFactory.h"

DTOIterator *ResultDTOIteratorFactory::createGeneralIterator(AbstractSimulationResultDTO *abstractResultDTO)
{
	list<AbstractSimulationResultDTO*>* services = new list<AbstractSimulationResultDTO*>();
	services = recursiveListContructor(abstractResultDTO,services);
	DTOIterator *iterator = new DTOIterator(services);
	return iterator;
}

DTOIterator *ResultDTOIteratorFactory::createServicesIterator(AbstractSimulationResultDTO *abstractResultDTO)
{
	list<AbstractSimulationResultDTO*>* services = new list<AbstractSimulationResultDTO*>();
	services = servicesRecursiveListContructor(abstractResultDTO,services);
	DTOIterator *iterator = new DTOIterator(services);
	return iterator;
}

DTOIterator *ResultDTOIteratorFactory::createSingleSimulationIterator(AbstractSimulationResultDTO *abstractResultDTO)
{
	list<AbstractSimulationResultDTO*>* services = new list<AbstractSimulationResultDTO*>();
	services = singleSimulationRecursiveListContructor(abstractResultDTO,services);
	DTOIterator *iterator = new DTOIterator(services);
	return iterator;
}

list<AbstractSimulationResultDTO*>* ResultDTOIteratorFactory::recursiveListContructor(AbstractSimulationResultDTO *abstractResultDTO,list<AbstractSimulationResultDTO*>* services){
	if (abstractResultDTO->isLeaf()){
		services->push_back(abstractResultDTO);
		return services;
	} else {
		list<AbstractSimulationResultDTO*> children = *abstractResultDTO->getChildren();
		while (children.size() > 0){
			AbstractSimulationResultDTO* child = children.front();
			if (child->isLeaf()){
				services->push_back(child);
				children.pop_front();
			} else {
				recursiveListContructor(child,services);
				children.pop_front();
			}
		}
		services->push_back(abstractResultDTO);
		return services;
	}
}

list<AbstractSimulationResultDTO*>* ResultDTOIteratorFactory::servicesRecursiveListContructor(AbstractSimulationResultDTO *abstractResultDTO,list<AbstractSimulationResultDTO*>* services){
	int temp = abstractResultDTO->getType();
	if (abstractResultDTO->isLeaf()){
		if (temp > 3)
			services->push_back(abstractResultDTO);
		return services;
	} else {
		list<AbstractSimulationResultDTO*> children = *abstractResultDTO->getChildren();
		while (children.size() > 0){
			AbstractSimulationResultDTO* child = children.front();
			if (child->isLeaf()){
				if (temp > 3)
					services->push_back(abstractResultDTO);
				services->push_back(child);
				children.pop_front();
			} else {
				recursiveListContructor(child,services);
			}
		}
		return services;
	}
}

list<AbstractSimulationResultDTO*>* ResultDTOIteratorFactory::singleSimulationRecursiveListContructor(AbstractSimulationResultDTO *abstractResultDTO,list<AbstractSimulationResultDTO*>* services){
	int temp = abstractResultDTO->getType();
	if (abstractResultDTO->isLeaf()){
		if (temp == 2)
			services->push_back(abstractResultDTO);
		return services;
	} else {
		list<AbstractSimulationResultDTO*> children = *abstractResultDTO->getChildren();
		while (children.size() > 0){
			AbstractSimulationResultDTO* child = children.front();
			if (child->isLeaf()){
				if (temp == 2)
					services->push_back(abstractResultDTO);
				services->push_back(child);
				children.pop_front();
			} else {
				recursiveListContructor(child,services);
			}
		}
		return services;
	}
}



