/**
  * @RDE_HEADER@
  * ViewshedGeoProcessing
  * $Id$
  * $HeadURL$
  * $Author$
  */

#pragma once

// Own
#include "viewshed_base.h"

// ESRI
#include <arcgis/Map.h>
#include <arcgis/SpatialReference.h>


// Qt
#include <QObject>
#include <QColor>
#include <QPointF>

// forward:
namespace Esri
{
namespace ArcGISRuntime
{
class Map;
class Graphic;
class GraphicsOverlay;
class GeoprocessingTask;
class GeoprocessingJob;
class GeoprocessingResult;
class GeoprocessingFeatures;
class GeoprocessingRaster;
}
}


//----------------------------------------------------------------------------
namespace SituationMap
{
class MapImpl;


class ViewshedGeoProcessing : public ViewshedBase
{
  Q_OBJECT

public:
  explicit ViewshedGeoProcessing(const QString &url,
                                 Esri::ArcGISRuntime::GraphicsOverlay *graphicsLayer,
                                 const Esri::ArcGISRuntime::SpatialReference &spatialReference,
                                 QObject *parent = nullptr);

  virtual ~ViewshedGeoProcessing();


  bool init(SituationMap::MapImpl *mapImpl = nullptr);
  bool initialized() const  { return m_initialized; }


  bool execute(const QPointF &viewshedPoint_map, const double distance, double zOffset) override;

  bool isRunning() const  { return m_viewshedInProgress; }

  bool processResults(Esri::ArcGISRuntime::GeoprocessingResult *results);

  void clearGraphics();

  void createInputGraphic(const Esri::ArcGISRuntime::Point &origin);
  void clearInputGraphics();

  void clearResultGraphics();


protected slots:
  void onJobStatusChanged();
  void onErrorOccuredGeoprocessingTask(const Esri::ArcGISRuntime::Error &error);
  void onErrorOccuredGeoprocessingJob(const Esri::ArcGISRuntime::Error &error);


protected:
  bool processFeatureResults(Esri::ArcGISRuntime::GeoprocessingFeatures *viewshedResultFeatures);
  bool processRasterResults(Esri::ArcGISRuntime::GeoprocessingRaster *viewshedResultRaster);

private:

  QString                                 m_url;

  Esri::ArcGISRuntime::GraphicsOverlay   *m_resultsOverlay;

  SituationMap::MapImpl                  *m_mapImpl             = nullptr;
  Esri::ArcGISRuntime::SpatialReference   m_srMap;

  Esri::ArcGISRuntime::GeoprocessingTask *m_viewshedTask        = nullptr;
  QString                                 m_jobStatus;

  Esri::ArcGISRuntime::GeoprocessingJob  *m_viewshedJob         = nullptr;

  bool                                    m_initialized         = false;

  bool                                    m_viewshedInProgress  = false;
  QList<Esri::ArcGISRuntime::Graphic *>   m_resultGraphics;

  Esri::ArcGISRuntime::Graphic           *m_originGraphic = nullptr;

  LOGGER_T                                m_logger;

}; // class ViewshedGeoProcessing



} // namespace SituationMap

