#ifndef EVALWIFIGROUND_H
#define EVALWIFIGROUND_H

#include "Indoor/sensors/radio/setup/WiFiOptimizer.h"
#include "Indoor/sensors/radio/setup/WiFiFingerprint.h"
#include "Indoor/sensors/radio/setup/WiFiFingerprints.h"
#include "Indoor/sensors/radio/setup/WiFiOptimizer.h"
#include "Indoor/sensors/radio/model/WiFiModels.h"

#include "Indoor/sensors/radio/VAPGrouper.h"
#include "Indoor/sensors/offline/FileReader.h"

#include "Indoor/floorplan/v2/Floorplan.h"
#include "Indoor/floorplan/v2/FloorplanReader.h"
#include "Indoor/floorplan/v2/FloorplanHelper.h"
#include "Indoor/floorplan/v2/FloorplanCeilings.h"

#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementPoints.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementColorPoints.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotPlot.h>
#include <KLib/misc/gnuplot/GnuplotPlotElementHistogram.h>

#include <KLib/math/statistics/Statistics.h>

#include "../Structs.h"
#include "../plots/Plotty.h"
#include "../plots/PlotErrTime.h"
#include "../plots/PlotErrFunc.h"
#include "../plots/PlotWiFiGroundProb.h"
#include <Indoor/debug/PlotWifiMeasurements.h>
//#include "../CSV.h"

#include <unordered_set>
#include <thread>

class EvalWiFiGround {

private:

	Floorplan::IndoorMap* map;
	WiFiModel* mdl;
	WiFiObserverFree* obs;

public:

	PlotWiFiGroundProb* groundProb;
	PlotWifiMeasurements plotWifi;

public:

	EvalWiFiGround(Floorplan::IndoorMap* map, const std::string calibModel, Plotty::Settings settings = Plotty::Settings()) : map(map) {

		mdl = WiFiModelFactory(map).loadXML(calibModel);
		obs = new WiFiObserverFree(8.0, *mdl);
		groundProb = new PlotWiFiGroundProb(map, settings);

		// must be 34
		std::cout << "Model APs: " << mdl->getAllAPs().size() << std::endl;
		int i = 0; (void) i;

	}

	void show(const std::string walkFile, const std::vector<int> gtIndices) {

		Offline::FileReader reader(walkFile);
		Offline::FileReader::GroundTruth gt = reader.getGroundTruth(map, gtIndices);
		VAPGrouper vap(VAPGrouper::Mode::LAST_MAC_DIGIT_TO_ZERO, VAPGrouper::Aggregation::AVERAGE);



		int cnt = 0;

		WiFiMeasurements mes;

		//std::vector<int> indices = {260,   270,271,272,   280};
		//std::vector<int> indices = {270,271,272};

		for (size_t i = 0; i < reader.getWiFiGroupedByTime().size(); ++i) {
		//for (int i : indices) {

			//mal nur zeigen welche regionen aus sicht von D8:84:66:4A:4B:C0 wahrscheinlich sind

			//beim VAP grouping mitzählen wieviele APs contributen.. wenns nur einer ist, dann vlt lieber löschen???

			const auto& entry = reader.getWiFiGroupedByTime()[i];

			const WiFiMeasurements& _newMes = entry.data;
			std::cout << _newMes.asString() << std::endl;

			WiFiMeasurements newMes = vap.group(_newMes);
			//newMes.remove(MACAddress("D8:84:66:4A:4B:C0"));

//			WiFiMeasurement leap = *(newMes.getForMac(MACAddress("D8:84:66:4A:4B:C0")));
//			newMes.entries.clear();
//			newMes.entries.push_back(leap);


			//std::cout << newMes.entries.front().getAP().getMAC().asString() << ":" << newMes.entries.front().getRSSI() << std::endl;
			//newMes.entries.erase(newMes.entries.begin());

			//			MACAddress mac("D8:84:66:4A:4B:C0");
			//			const WiFiMeasurement* xxx = newMes.getForMac(mac);
			//			if (xxx) {
			//				std::cout << xxx->getRSSI() << std::endl;
			//			} else {
			//				std::cout << "MISSING" << std::endl;
			//			}
			//			mes = WiFiMeasurements::mix(mes, newMes, 2);
			mes = newMes;

			plotWifi.add(newMes);
			plotWifi.plot();

			Point3 p3 = gt.get(mes.entries.front().getTimestamp());
			K::GnuplotPoint3 gp3(p3.x, p3.y, p3.z);
			groundProb->getPlot().cpoints.clear();
			groundProb->getPlot().points.add(gp3);

			const Timestamp ts = newMes.entries.front().getTimestamp();
			groundProb->show(*obs, mes, ts);
			groundProb->plotMe();


			//if (++cnt > 70) {break;}
			std::this_thread::sleep_for(std::chrono::milliseconds(10));

		}

		//groundProb->update();
		groundProb->plotMe();
		int i = 0; (void) i;

		sleep(1000);

	}

	void add(const std::string walkFile, const int idx, const float hue) {

		VAPGrouper vap(VAPGrouper::Mode::LAST_MAC_DIGIT_TO_ZERO, VAPGrouper::Aggregation::AVERAGE);

		Offline::FileReader reader(walkFile);

		const auto& entry = reader.getWiFiGroupedByTime()[idx];
		const WiFiMeasurements& _mes = entry.data;
		const WiFiMeasurements mes = vap.group(_mes);

		groundProb->showStrongest(*obs, mes, hue);
		//groundProb->plotMe();
		//std::this_thread::sleep_for(std::chrono::milliseconds(2));

		//groundProb->update();
		//groundProb->plotMe();
		int i = 0; (void) i;

	}

	void addGT(const std::string walkFile, const int idx, const float hue, const std::vector<int>& path) {

		Offline::FileReader reader(walkFile);
		const auto& entry = reader.getWiFiGroupedByTime()[idx];
		const WiFiMeasurements& mes = entry.data;

		// gt at the time of the wifi measurement
		const Timestamp ts = mes.entries.front().getTimestamp();
		const Point3 pt = reader.getGroundTruth(map, path).get(ts);

		Color c1 = Color::fromRGB(0,0,0);
		Color c2 = Color::fromHSV(hue, 255, 255);

		groundProb->getPlot().addFloorRect(pt+Point3(0,0,0.200), 1.5, c1);
		groundProb->getPlot().addFloorRect(pt+Point3(0,0,0.201), 1.0, c2);

	}

	void writeGP(const std::string& path, const std::string& name) {

		const std::string file = path + "/" + name;

		groundProb->getPlot().splot.getCustom() << "unset border \n";
		groundProb->getPlot().splot.getCustom() << "set view equal xy\n";

		groundProb->getPlot().splot.getAxisX().setTicsVisible(false);
		groundProb->getPlot().splot.getAxisY().setTicsVisible(false);
		groundProb->getPlot().splot.getAxisZ().setTicsVisible(false);
		groundProb->getPlot().splot.getAxisZ().setRange(-12.500000, 21.200000);
		groundProb->getPlot().splot.getView().setCamera(72, 28);
		groundProb->getPlot().splot.getView().setScaleAll(5.5);

		groundProb->getPlot().gp.setTerminal("epslatex", K::GnuplotSize(8.6, 4.0));
		groundProb->getPlot().gp.setOutput(file+".tex");
		groundProb->getPlot().gp.writePlotToFile(file+".gp");
		groundProb->plotMe();
		groundProb->getPlot().gp.writePlotToFile("");

	}



};

#endif // EVALWIFIGROUND_H
