#pragma once

#include <vector>
#include <string>
#include <array>

#include "snake_geometry.h"

#define clipper_scale 10000.0

using namespace std;
using namespace snake_geometry;

namespace snake {

    enum AreaType {MeasurementArea, ServiceArea, Corridor};

    struct Area {
        GeoPoint2DList      geoPolygon;
        double              altitude;
        size_t              layers;
        AreaType            type;
    };

    //========================================================================================
    // Scenario
    //========================================================================================
    class Scenario{
    public:
        Scenario();

        bool addArea(Area &area);

        const Area &getMeasurementArea() const {return _measurementArea;}
        const Area &getServiceArea() const {return _serviceArea;}
        const Area &getCorridor() const {return _corridor;}

        const BoostPolygon &getMeasurementAreaENU() {return _measurementAreaENU;}
        const BoostPolygon &getServiceAreaENU()     {return _serviceAreaENU;}
        const BoostPolygon &getCorridorENU()        {return _corridorENU;}
        const BoostPolygon &getJoineAreaENU()       {return _joinedAreaENU;}
        const GeoPoint3D   &getOrigin()             {return _geoOrigin;}

        const vector<BoostPolygon> &getTilesENU()               {return _tilesENU;}
        const BoostPointList       &getTileCenterPointsENU()    {return _tileCenterPointsENU;}
        const min_bbox_rt          &getMeasurementAreaBBoxENU() {return _mAreaBoundingBox;}
        const BoostPoint           &getHomePositonENU()         {return _homePositionENU;}

        bool defined(double tileWidth, double tileHeight, double minTileArea);

        string errorString;

    private:
        bool _areas2enu();
        bool _setMeasurementArea(Area &area);
        bool _setServiceArea(Area &area);
        bool _setCorridor(Area &area);
        bool _calculateBoundingBox();
        bool _calculateTiles(double tileWidth, double tileHeight, double minTileArea);
        bool _calculateJoinedArea();

        Area _measurementArea;
        Area _serviceArea;
        Area _corridor;

        BoostPolygon _measurementAreaENU;
        BoostPolygon _serviceAreaENU;
        BoostPolygon _corridorENU;
        BoostPolygon _joinedAreaENU;

        min_bbox_rt _mAreaBoundingBox;

        vector<BoostPolygon>    _tilesENU;
        BoostPointList          _tileCenterPointsENU;

        GeoPoint3D _geoOrigin;
        BoostPoint _homePositionENU;
    };

    //========================================================================================
    // FlightPlan
    //========================================================================================
    class FlightPlan{
    public:
        FlightPlan();
        FlightPlan(const Scenario &scenario);

        void setScenario(const Scenario &scenario)          {_scenario = scenario;}
        void setProgress(const vector<int8_t> &progress)    {_progress = progress;}

        const Scenario          &getScenario(void)      {return _scenario;}
        const BoostPointList    &getWaypointsENU(void)  {return _waypointsENU;}
        const GeoPoint2DList    &getWaypoints(void)     {return _waypoints;}

        bool generate(double lineDistance, double minTransectLength);

        string errorString;

    private:
        bool _generateTransects(double lineDistance, double minTransectLength);
        bool _generateRoutingModel();
        Scenario                                _scenario;
        BoostPointList                          _waypointsENU;
        GeoPoint2DList                          _waypoints;
        vector<tuple<BoostPoint, BoostPoint>>   _transects;
        vector<int8_t>                          _progress;
        BoostPolygon                            _joinedAreaOffset;
    };

    namespace detail {
        double polygonOffset = 0.1; // meter, polygon offset to compenstate for numerical inaccurracies.
    }
}

