#pragma once

#include <QGeoCoordinate>
#include <QSharedPointer>
#include <QThread>

#include "snake.h"
#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>

struct RoutingData {
  snake::BoostLineString route;
  snake::Transects transects;
  std::vector<snake::TransectInfo> transectsInfo;
};

//!
//! \brief The CSWorker class
//! \note Don't call QThread::start, QThread::quit, etc. onyl use Worker
//! members!
class RoutingWorker : public QThread {
  Q_OBJECT
  using Lock = std::unique_lock<std::mutex>;

public:
  using PtrRoutingData = QSharedPointer<RoutingData>;
  using Generator = std::function<bool(snake::Transects &)>;

  RoutingWorker(QObject *parent = nullptr);
  ~RoutingWorker() override;

  bool calculating();

public slots:
  void route(const snake::BoostPolygon &safeArea, const Generator &generator);

signals:
  void result(PtrRoutingData pTransects);
  void calculatingChanged();

protected:
  void run() override;

private:
  mutable std::mutex _mutex;
  mutable std::condition_variable _cv;
  // Internal data
  snake::BoostPolygon _safeArea;
  Generator _generator; // transect generator
  // State
  std::atomic_bool _calculating;
  std::atomic_bool _stop;
  std::atomic_bool _restart;
};
