#include <sstream>
#include "CarSource.h"
#include "Car.h"
#include "../Logger.h"
#include "../Simulator.h"

CarSource::CarSource(Sector* sector, CarType carType, const Vector3r &pos, const dReal dir, unsigned limit, unsigned wait):
	SimulationObject(),
	_sector(sector),
	_carType(carType),
	_pos(pos),
	_dir(dir),
	_limit(limit),
	_wait(wait),
	_timer(0)
{
}

CarSource::~CarSource()
{
}

void CarSource::initialize()
{
	_DEBUG("CarSource initialized");
}

void CarSource::computeStep()
{
	if (_sector->getNumOfCars() == 0 && _timer > 0) _timer--;
}

void CarSource::performStep()
{
	if (_sector->getNumOfCars() == 0 && _limit > 0 && _timer == 0) {
		Car* car = new Car(_sector, _carType._bodySize, _pos, _dir, _carType._weight, _carType._wheelRad, _carType._wheelThick, _carType._wheelWeight, _carType._maxSpeed);
		car->_maxAcceleration = _carType._acceleration;
		car->_maxDecceleration = _carType._decceleration;
		Simulator::getInstance()->scheduleForAddition(car);
		_timer = _wait;
		_limit--;
	}
}

std::string CarSource::toString() const
{
	std::ostringstream oss;
	oss << "CarSource[" << getID() << "], _limit=" << _limit << ", _wait=" << _wait;
	return oss.str();
}

std::ostream& operator<<(std::ostream& os, const CarSource& source)
{
	os << source.toString();
	return os;
}
