#ifndef OLDGROUNDTRUTH_H
#define OLDGROUNDTRUTH_H

#include <KLib/geo/Point.h>
#include <unordered_map>
#include <KLib/gfx/svg/SVGLoader.h>
#include <Indoor/geo/Point2.h>

/**
 * TODO: REMOVE
 */
class OldGroundTruth {

	/**
	 * helper class for SVG floorplans.
	 *
	 * converts between the SVG's scale and real-world scale
	 */
	class SVGScaler {

	private:

		/** the scaling factor to apply to the svg data */
		double scalingFactor;

	public:

		/** ctor */
		SVGScaler(const double scalingFactor) : scalingFactor(scalingFactor) {
			;
		}

		/** scale (x, y) into (_x, _y) */
		void scale(const double x, const double y, float& _x, float& _y) const {
			_x = x * scalingFactor;
			_y = y * scalingFactor;
		}

		/** scale the given point into a new output point */
		Point2 scale(const K::Point p) const {
			Point2 ret;
			scale (p.x, p.y, ret.x, ret.y);
			return ret;
		}

		/** scale the given line into a new output line */
		Line2 scale(const K::Line l) const {
			Line2 ret;
			scale (l.p1.x, l.p1.y, ret.p1.x, ret.p1.y);
			scale (l.p2.x, l.p2.y, ret.p2.x, ret.p2.y);
			return ret;
		}

	};

private:

	/** helper to scale the SVG into real-world-scale */
	SVGScaler scaler;

	/** all ground-truth waypoints within the floorplan */
	std::unordered_map<int, Point2> points;

public:

	OldGroundTruth() :scaler(0) {;}

	/**
	 * ctor
	 * @param file the svg's filename
	 * @param layerName the name of the layer (within the SVG) to load
	 * @param scalingFactor the scaling to apply to convert between SVG and real-world scale
	 */
	OldGroundTruth(const std::string& file, const std::string& layerName, const double scalingFactor) : scaler(scalingFactor) {

		K::SVGFile svg;
		K::SVGLoader::load(K::File(file), &svg);
		K::SVGComposite* sc = svg.getLayers();

		K::SVGLayer* layer = sc->getContainedLayerNamed(layerName);
		if (!layer) {throw "svg has no layer named '" + layerName + "'";}
		load(layer);

	}

	/** get all waypoints */
	const std::unordered_map<int, Point2>& getWaypoints() const {
		return points;
	}


private:



	/** recursive loading/parsing of nested SVG elements */
	void load(K::SVGElement* el) {

		switch (el->getType()) {

			case SVGElementType::COMPOSITE: {
				for (K::SVGElement* sub : ((K::SVGComposite*)el)->getChilds()) {
					load(sub);
				}
				break;
			}

			case SVGElementType::LAYER: {
				K::SVGLayer* layer = (K::SVGLayer*) el;
				for (K::SVGElement* sub : layer->getChilds()) {
					load(sub);
				}
				break;
			}

			case SVGElementType::TEXT: {
				const K::SVGText* text = (K::SVGText*) el;
				if (text->getText().empty()) {break;}
				const int id = std::stoi(text->getText());
				points[id] = scaler.scale(K::Point(text->getPosition().x, text->getPosition().y));
				break;
			}

			case SVGElementType::PATH: {
				break;
			}

			default:
				throw "should not happen!";

		}

	}


};

#endif // OLDGROUNDTRUTH_H
