#ifndef FLOOR2D_H
#define FLOOR2D_H

#include <Indoor/floorplan/v2/Floorplan.h>
#include "Renderable2D.h"

/**
 * draw the floor itself (outline, obstacles)
 */
class Floor2D : public Renderable2D {

private:

	Floorplan::Floor* floor;

public:

	/** ctor */
	Floor2D(Floorplan::Floor* floor) : floor(floor) {

	}

protected:

	void doRender(QPainter& qp, const Scaler2D& s, const RenderParams2D& r) override {

		if (floor->atHeight < r.clip.belowHeight_m) {return;}
		if (floor->atHeight > r.clip.aboveHeight_m) {return;}

		qp.setPen(Qt::black);
		for (const Floorplan::FloorObstacle* obs : floor->obstacles) {
			const Floorplan::FloorObstacleLine* line = dynamic_cast<const Floorplan::FloorObstacleLine*>(obs);
			if (line) {drawLine(qp, s, line);}
		}

		qp.setPen(Qt::gray);
		for (const Floorplan::FloorOutlinePolygon* poly : floor->outline) {
			drawOutline(qp, s, poly);
		}

	}

private:

	void drawLine(QPainter& qp, const Scaler2D& s, const Floorplan::FloorObstacleLine* line) {
		const Point2 pt1 = s.mapToScreen(line->from);
		const Point2 pt2 = s.mapToScreen(line->to);
		qp.drawLine(pt1.x, pt1.y, pt2.x, pt2.y);
	}

	void drawOutline(QPainter& qp, const Scaler2D& s, const Floorplan::FloorOutlinePolygon* poly) {
		const int num = poly->poly.points.size();
		for (int i = 0; i < num; ++i) {
			const Point2 pt1 = s.mapToScreen(poly->poly.points[(i+0)]);
			const Point2 pt2 = s.mapToScreen(poly->poly.points[(i+1)%num]);
			qp.drawLine(pt1.x, pt1.y, pt2.x, pt2.y);
		}
	}

};

#endif // FLOOR2D_H
