#pragma once

#include <KLib/misc/gnuplot/Gnuplot.h>
#include <KLib/misc/gnuplot/GnuplotSplot.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementLines.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementPoints.h>
#include <KLib/misc/gnuplot/GnuplotSplotElementColorPoints.h>
#include <KLib/misc/gnuplot/objects/GnuplotObjectPolygon.h>

#include <Indoor/math/Distributions.h>
#include <Indoor/navMesh/NavMesh.h>
#include <Indoor/floorplan/v2/Floorplan.h>

class NavMeshTriangleDijkstra;

/**
 * debug plot NavMeshes
 */
class MeshPlotter {

public:

    K::Gnuplot gp;
    K::GnuplotSplot plot;
    K::GnuplotSplotElementLines pFloor;
    K::GnuplotSplotElementLines pOutline;
    K::GnuplotSplotElementLines lines;
    K::GnuplotSplotElementPoints border;
    K::GnuplotSplotElementColorPoints particles;
    K::GnuplotSplotElementColorPoints distances;
    K::GnuplotSplotElementLines pathEstimated;
    K::GnuplotSplotElementLines shortestPath;
    K::GnuplotSplotElementLines groundtruthPath;

private:

    K::GnuplotFill gFill[6] = {
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#0000ff"), 1),	// unknown
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#999999"), 1),	// indoor
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#44ffee"), 1),	// outdoor
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#666699"), 1),	// door
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#444444"), 1),	// stairs_level
        K::GnuplotFill(K::GnuplotFillStyle::SOLID, K::GnuplotColor::fromHexStr("#666666"), 1)	// stairs_skewed
    };

public:

    MeshPlotter() {
        gp << "set view equal xy\n";
        plot.add(&lines); lines.setShowPoints(true);
        plot.add(&border);
        plot.add(&particles); particles.setPointType(7); particles.setPointSize(0.2);
        plot.add(&pathEstimated); pathEstimated.getStroke().setWidth(2); pathEstimated.setShowPoints(false); pathEstimated.getStroke().getColor().setHexStr("#00ff00");
        plot.add(&groundtruthPath); groundtruthPath.getStroke().setWidth(2); groundtruthPath.getStroke().getColor().setHexStr("#000000");
        plot.add(&distances); distances.setPointSize(0.75); distances.setPointType(7);
        plot.add(&shortestPath); shortestPath.getStroke().setWidth(3);
        plot.add(&pFloor);
        plot.add(&pOutline); pOutline.getStroke().getColor().setHexStr("#999999");
    }

    void draw() {
        gp.draw(plot);
        gp.flush();
    }

    template <typename T> void showParticles(const std::vector<T>& particles) {
        this->particles.clear();
        double min = +999;
        double max = -999;
        for (const T& p : particles) {
            const K::GnuplotPoint3 p3(p.state.pos.pos.x, p.state.pos.pos.y, p.state.pos.pos.z);
            const double prob = std::pow(p.weight, 0.25);
            this->particles.add(p3, prob);
            if (prob > max) {max = prob;}
            if (prob < min) {min = prob;}
        }
        plot.getAxisCB().setRange(min, max + 0.000001);
    }


    template <typename Tria> void addMesh(NM::NavMesh<Tria>& nm) {

        K::GnuplotStroke gStroke = K::GnuplotStroke(K::GnuplotDashtype::SOLID, 1, K::GnuplotColor::fromHexStr("#666600"));

        const BBox3 bbox = nm.getBBox();

        border.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z));
        border.add(K::GnuplotPoint3(bbox.getMax().x,bbox.getMax().y,bbox.getMax().z));
//		lines.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(bbox.getMax().x, 0, 0));
//		lines.add(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(0,bbox.getMax().y,0));
//		lines.addSegment(K::GnuplotPoint3(bbox.getMin().x,bbox.getMin().y,bbox.getMin().z), K::GnuplotPoint3(0,0,bbox.getMax().z));

        //stairs in eigene group? vlt gehen dann auch die dellen weg?

        for (const Tria* tria : nm) {
            const uint8_t type = tria->getType();
            if (type < 0 || type > 5) {
                throw std::runtime_error("out of type-bounds");
            }
            K::GnuplotObjectPolygon* pol = new K::GnuplotObjectPolygon(gFill[type], gStroke);
            pol->add(K::GnuplotCoordinate3(tria->getP1().x, tria->getP1().y, tria->getP1().z, K::GnuplotCoordinateSystem::FIRST));
            pol->add(K::GnuplotCoordinate3(tria->getP2().x, tria->getP2().y, tria->getP2().z, K::GnuplotCoordinateSystem::FIRST));
            pol->add(K::GnuplotCoordinate3(tria->getP3().x, tria->getP3().y, tria->getP3().z, K::GnuplotCoordinateSystem::FIRST));
            pol->close();
            pol->setZIndex(tria->getP3().z);
            plot.getObjects().add(pol);

            //for (int i = 0; i < nm.getNumNeighbors(tria); ++i) {
            //	const Tria* o = nm.getNeighbor(tria, i);
            //	const Point3 p1 = tria->getCenter();
            //	const Point3 p2 = o.getCenter();
            //	//lines.addSegment(K::GnuplotPoint3(p1.x,p1.y,p1.z+0.1), K::GnuplotPoint3(p2.x,p2.y,p2.z+0.1));
            //}

            for (const NM::NavMeshTriangle* o : *tria) {
                const Point3 p1 = tria->getCenter();
                const Point3 p2 = o->getCenter();
            //	lines.addSegment(K::GnuplotPoint3(p1.x,p1.y,p1.z+0.1), K::GnuplotPoint3(p2.x,p2.y,p2.z+0.1));
            }

        }

        plot.getObjects().reOrderByZIndex();

    }

    template <typename Tria> void addDijkstra(NM::NavMesh<Tria>& mesh) {

        distances.clear();

        // ensure Tria extends NavMeshTriangleDijkstra
        StaticAssert::AinheritsB<Tria, NavMeshTriangleDijkstra>();

        NM::NavMeshRandom<Tria> rnd = mesh.getRandom();

        for (int i = 0; i < 5000; ++i) {
            NM::NavMeshLocation<Tria> loc = rnd.draw();
            float v = loc.tria->interpolate(loc.pos, loc.tria->spFromP1.distance, loc.tria->spFromP2.distance, loc.tria->spFromP3.distance);
            distances.add(K::GnuplotPoint3(loc.pos.x, loc.pos.y, loc.pos.z), v);
        }


//			Distribution::Uniform<float> dist (-0.5, +0.5);
//			for (const Tria* t : mesh) {
//				const Point3 posC = t->getCenter();
//				distances.add(K::GnuplotPoint3(posC.x+dist.draw(), posC.y+dist.draw(), posC.z), t->distAtCenter);
//				const Point3 pos1 = t->getP1();
//				distances.add(K::GnuplotPoint3(pos1.x+dist.draw(), pos1.y+dist.draw(), pos1.z), t->distAtP1);
//				const Point3 pos2 = t->getP2();
//				distances.add(K::GnuplotPoint3(pos2.x+dist.draw(), pos2.y+dist.draw(), pos2.z), t->distAtP2);
//				const Point3 pos3 = t->getP3();
//				distances.add(K::GnuplotPoint3(pos3.x+dist.draw(), pos3.y+dist.draw(), pos3.z), t->distAtP3);
//			}

    }

    template <typename Tria> void addDijkstra(std::vector<NM::NavMeshLocation<Tria>>& path) {
        shortestPath.clear();
        for (auto& e : path) {
            K::GnuplotPoint3 gp(e.pos.x, e.pos.y, e.pos.z);
            shortestPath.add(gp);
        }
    }

    void addGroundTruthNode(const Point3 pos) {
        K::GnuplotPoint3 gp(pos.x, pos.y, std::round(pos.z * 10) / 10);
        groundtruthPath.add(gp);
    }

    void addEstimationNode(const Point3 pos){
        K::GnuplotPoint3 est(pos.x, pos.y, std::round(pos.z * 10) / 10);
        pathEstimated.add(est);
    }


    void setTimeInMinute(const int minutes, const int seconds) {
        gp << "set label 1002 at screen 0.02, 0.94 'Time: " << minutes << ":" << seconds << "'\n";
    }

    void setGT(const Point3 pt) {
        gp << "set arrow 31337 from " << pt.x << "," << pt.y << "," << (pt.z+1.4) << " to " << pt.x << "," << pt.y << "," << pt.z << " front \n";
    }

    void setCurPos(const Point3 pt) {
        gp << "set arrow 31338 from " << pt.x << "," << pt.y << "," << (pt.z+0.9) << " to " << pt.x << "," << pt.y << "," << pt.z << " lw 2 lc 'green' front \n";
    }

    void saveToFile(std::ofstream& stream){
        gp.draw(plot);
        stream << "set terminal x11 size 2000,1500\n";
        stream << gp.getBuffer();
        stream << "pause -1\n";
        gp.flush();
    }

    void printOverview(const std::string& path) {
        gp << "set terminal png size 1280,720\n";
        gp << "set output '" << path << "_overview" << ".png'\n";
        gp << "set view 75,60\n";
        gp << "set autoscale xy\n";
        gp << "set autoscale z\n";
        draw();
    }


    //meshless drawing
    void addFloors(Floorplan::IndoorMap* map) {

        for (Floorplan::Floor* f : map->floors) {
            for (Floorplan::FloorObstacle* obs : f->obstacles) {
                Floorplan::FloorObstacleLine* line = dynamic_cast<Floorplan::FloorObstacleLine*>(obs);
                if (line) {
                    K::GnuplotPoint3 p1(line->from.x, line->from.y, f->atHeight);
                    K::GnuplotPoint3 p2(line->to.x, line->to.y, f->atHeight);
                    pFloor.addSegment(p1, p2);
                }
            }
        }

    }

    void addOutline(Floorplan::IndoorMap* map) {

        for (Floorplan::Floor* f : map->floors) {
            for (Floorplan::FloorOutlinePolygon* poly : f->outline) {
                const int cnt = poly->poly.points.size();
                for (int i = 0; i < cnt; ++i) {
                    Point2 p1 = poly->poly.points[(i+0)];
                    Point2 p2 = poly->poly.points[(i+1)%cnt];
                    K::GnuplotPoint3 gp1(p1.x, p1.y, f->atHeight);
                    K::GnuplotPoint3 gp2(p2.x, p2.y, f->atHeight);
                    pOutline.addSegment(gp1, gp2);
                }
            }
        }

    }

};

