#include "stdafx.h"
#include "SimulatorSingleton.h"
#include "Arrival.h"
#include "SuitFactory.h"
#include "Event.h"
#include "EndSimulation.h"

SimulatorSingleton* SimulatorSingleton::_instance =0;


SimulatorSingleton* SimulatorSingleton::instance() {
	if(!_instance)
	{
		_instance=new SimulatorSingleton(size);
	}
	return _instance;
}

void SimulatorSingleton::start(){
		clock = 0;
		simulationComplete = false;
		Arrival* arrival = new Arrival(0,arrivalsRate);
		SuitFactory *suitFactory = SuitFactory::instance();
		Suit suit = suitFactory->createNewSuit();
		suit.setInitialTime(0);
		arrival->suit = suit;
		add_event(arrival);
		EndSimulation* endSimulation = new EndSimulation(500);
		add_event(endSimulation);
}

void SimulatorSingleton::reset(){
		clock = 0;
		simulationComplete = false;
		Arrival* arrival = new Arrival(0,arrivalsRate);
		SuitFactory *suitFactory = SuitFactory::instance();
		Suit suit = suitFactory->createNewSuit();
		suit.setInitialTime(0);
		arrival->suit = suit;
		add_event(arrival);
		EndSimulation* endSimulation = new EndSimulation(500);
		add_event(endSimulation);
}

void SimulatorSingleton::step(){
	if(events.size() > 0) {
		Event* event = events.heapExtractMax();
		clock = event->getTime();
		event->action();
		currentEvent = event;
		if (event->getType() == -1)
			simulationComplete = true;
		notifyStep();
	}
}

bool SimulatorSingleton::isSimulationComplete(){
	if (simulationComplete){
		notifySimulation();
	}
	return simulationComplete;
}

Heap<Ptr<Event> > SimulatorSingleton::getEventQueue(){
	return events;
}

Event* SimulatorSingleton::getCurrentEvent(){
		return currentEvent;
}

Event* SimulatorSingleton::getIthEvent(int i){
		return events.getEvent(i);
}

int SimulatorSingleton::getEventQueueSize(){
		return events.size();
}

AbstractSimulationResultDTO* SimulatorSingleton::getSimulationResult(){
	return simulationResult;
}

void SimulatorSingleton::setSimulationResult(AbstractSimulationResultDTO* result){
	simulationResult = result;
}

