#ifndef RANDOMSENSOR_H
#define RANDOMSENSOR_H

#include <thread>
#include <Indoor/data/Timestamp.h>
#include <Indoor/Assertions.h>

template <typename Element, typename BaseClass> class RandomSensor : public BaseClass {

private:

	std::thread thread;
	bool running = false;
	Timestamp interval;

public:

	RandomSensor(const Timestamp interval) : interval(interval) {
		;
	}

	void start() override {
		Assert::isFalse(running, "sensor allready running!");
		running = true;
		thread = std::thread(&RandomSensor::run, this);
	}

	void stop() override {
		Assert::isTrue(running, "sensor not yet running!");
		running = false;
		thread.join();
	}

	bool isRunning() const override {
		return running;
	}


protected:

	/** subclasses must provide a random entry here */
	virtual Element getRandomEntry() = 0;

private:

	void run() {

		while(running) {

			const Element rnd = getRandomEntry();
			Sensor<Element>::informListeners(rnd);

			std::this_thread::sleep_for(std::chrono::milliseconds(interval.ms()));

		}

	}

};

#endif // RANDOMSENSOR_H
