#pragma once

//#include "circular.h"
#include "BarometerObservation.h"
#include "../reader/SensorReader.h"
#include <sstream>

#include <Indoor/math/MovingAVG.h>
#include <Indoor/math/MovingMedian.h>

//circular_buffer<double> measurementHistory(1000);


class BarometerSensorReader{

private:

	// NOTE: median or avg?
	MovingMedian<float> avg;
	MovingMedian<float> avgStart;

	// avg: lower size, median: bigger still fine
	static constexpr int avgSize = 10;
	static constexpr int startAvgSize = 10;

	// skip the first 1-2 seconds and let the sensor settle itself
	uint64_t skipTS = 0;

public:

	BarometerSensorReader(): avg(avgSize), avgStart(startAvgSize) {
		;
    }

    BarometerObservation* readBarometer(const SensorEntry& se) {

		// skip the first few 1.5 seconds
		if (skipTS == 0) {skipTS = se.ts;}
		if (se.ts - skipTS < 3000) {return nullptr;}
		
		std::string tmp = se.data;
		BarometerObservation* obs = new BarometerObservation();
		const float cur = std::stof(tmp);

		// get the next hPa reading and average it
		avg.add(cur);

		// average the first few readings as reference
		if (avgStart.getNumUsed() < startAvgSize) {
			avgStart.add(cur);
		}

		// current average relative to the start-average
		obs->hpa = avg.get() - avgStart.get();

		// done
		return obs;

    }

	float getHPA(const SensorEntry& se) {
		return std::stof(se.data);
	}

//    //TODO
//    void readVerticalAcceleration(const SensorEntry& se){

//        //Problem: Koordinatensystem LinearAcceleraton ist relativ zum Telefon und nicht zum
//        //Weltkoordinatensystem. Brauchen die Beschleunigung nach Oben in Weltkoordinaten.

//    }


};
