/*=========================================================================

  Library:   CTK

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

#ifndef __ctkDICOMQuery_h
#define __ctkDICOMQuery_h

// Qt includes
#include <QObject>
#include <QMap>
#include <QString>

// ctkCore includes
#include <ctkPimpl.h>

// ctkDICOMCore includes
#include "ctkDICOMCoreExport.h"
#include "ctkDICOMDatabase.h"
class ctkDICOMQueryPrivate;
class ctkDICOMJobResponseSet;

/// \ingroup DICOM_Core
class CTK_DICOM_CORE_EXPORT ctkDICOMQuery : public QObject
{
  Q_OBJECT
  Q_PROPERTY(QString connectionName READ connectionName WRITE setConnectionName);
  Q_PROPERTY(QString callingAETitle READ callingAETitle WRITE setCallingAETitle);
  Q_PROPERTY(QString calledAETitle READ calledAETitle WRITE setCalledAETitle);
  Q_PROPERTY(QString host READ host WRITE setHost);
  Q_PROPERTY(int port READ port WRITE setPort);
  Q_PROPERTY(int connectionTimeout READ connectionTimeout WRITE setConnectionTimeout);
  Q_PROPERTY(int maximumPatientsQuery READ maximumPatientsQuery WRITE setMaximumPatientsQuery);
  Q_PROPERTY(QList<QPair<QString,QString>> studyAndSeriesInstanceUIDQueried READ studyAndSeriesInstanceUIDQueried);
  Q_PROPERTY(QString jobUID READ jobUID WRITE setJobUID);

public:
  explicit ctkDICOMQuery(QObject* parent = 0);
  virtual ~ctkDICOMQuery();

  ///@{
  /// Name identifying the server
  void setConnectionName(const QString& connectionName);
  QString connectionName() const;
  ///@}

  ///@{
  /// Methods for connectivity.
  /// Empty by default
  void setCallingAETitle(const QString& callingAETitle);
  QString callingAETitle()const;

  void setCalledAETitle(const QString& calledAETitle);
  QString calledAETitle() const;
  ///@}

  ///@{
  /// Peer hostname being connected to
  /// Empty by default
  void setHost(const QString& host);
  QString host() const;
  ///@}

  ///@{
  /// Specify a port for the packet headers.
  /// \a port ranges from 0 to 65535.
  /// 0 by default.
  void setPort(const int& port);
  int port() const;
  ///@}

  ///@{
  /// connection timeout, default 10 sec.
  void setConnectionTimeout(const int& timeout);
  int connectionTimeout() const;
  ///@}

  ///@{
  /// maximum number of responses allowed in one query
  /// when query is at Patient level. Default is 25.
  void setMaximumPatientsQuery(const int& maximumPatientsQuery);
  int maximumPatientsQuery() const;
  ///@}

  ///@{
  /// Filters are keyword/value pairs as generated by
  /// the ctkDICOMWidgets in a human readable (and editable)
  /// format.  The Query is responsible for converting these
  /// into the appropriate dicom syntax for the C-Find
  /// Currently supports the keys: Name, Study, Series, ID, Modalities,
  /// StartDate and EndDate
  /// Key         DICOM Tag                Type        Example
  /// -----------------------------------------------------------
  /// Name        DCM_PatientName          QString     JOHNDOE
  /// Study       DCM_StudyDescription     QString
  /// Series      DCM_SeriesDescription    QString
  /// ID          DCM_PatientID            QString
  /// Modalities  DCM_ModalitiesInStudy    QStringList CT, MR, MN
  /// StartDate   DCM_StudyDate            QString     20090101
  /// EndDate     DCM_StudyDate            QString     20091231
  /// No filter (empty) by default.
  Q_INVOKABLE void setFilters(const QMap<QString,QVariant>&);
  Q_INVOKABLE QMap<QString,QVariant> filters()const;
  ///@}

  /// Return true if the operation was canceled.
  Q_INVOKABLE bool wasCanceled();

  /// Query a remote DICOM Image Store SCP.
  /// You must at least set the host and port before calling query()
  Q_INVOKABLE bool query(ctkDICOMDatabase& database);

  /// Access the list of study and series instance UIDs from the last query method.
  QList<QPair<QString, QString>>  studyAndSeriesInstanceUIDQueried()const;

  /// Utility method to query a remote DICOM Image Store SCP only at patient level.
  /// You must at least set the host and port before calling query().
  /// Results will be stored in the ctkDICOMJobResponseSet list (jobResponseSets).
  Q_INVOKABLE bool queryPatients();

  /// Utility method to query a remote DICOM Image Store SCP only at study level.
  /// You must at least set the host and port before calling query().
  /// Results will be stored in the ctkDICOMJobResponseSet list (jobResponseSets).
  Q_INVOKABLE bool queryStudies(const QString& patientID);

  /// Utility method to query a remote DICOM Image Store SCP only at series level
  /// given a studyInstanceUID.
  /// You must at least set the host and port before calling query().
  /// Results will be stored in the ctkDICOMJobResponseSet list (jobResponseSets).
  Q_INVOKABLE bool querySeries(const QString& patientID,
                               const QString& studyInstanceUID);

  /// Utility method to query a remote DICOM Image Store SCP only at instance level
  /// given a studyInstanceUID and seriesInstanceUID.
  /// You must at least set the host and port before calling query().
  /// Results will be stored in the ctkDICOMJobResponseSet list (jobResponseSets).
  Q_INVOKABLE bool queryInstances(const QString& patientID,
                                  const QString& studyInstanceUID,
                                  const QString& seriesInstanceUID);


  /// Access the list of datasets from the last queryPatients, queryStudies,
  /// querySeries and queryInstances methods.
  Q_INVOKABLE QList<ctkDICOMJobResponseSet*> jobResponseSets() const;
  QList<QSharedPointer<ctkDICOMJobResponseSet>> jobResponseSetsShared() const;
  ///@}

  ///@{
  /// Reference job uid.
  void setJobUID(const QString& jobUID);
  QString jobUID() const;
  ///@}

Q_SIGNALS:
  /// Signal is emitted inside the query() function. It ranges from 0 to 100.
  /// In case of an error, you are assured that the progress value 100 is fired
  void progress(int progress);
  /// Signal is emitted inside the query() function. It sends the different step
  /// the function is at.
  void progress(const QString& message);
  /// Signal is emitted inside the query() function. It sends
  /// detailed feedback for debugging
  void debug(const QString& message);
  /// Signal is emitted inside the query() function. It send any warning messages
  void warn(const QString& message);
  /// Signal is emitted inside the query() function. It send any error messages
  void error(const QString& message);
  /// Signal is emitted inside the query() function when finished with value
  /// true for success or false for error
  void done(const bool& error);

public Q_SLOTS:
  /// Cancel the current operation
  Q_INVOKABLE void cancel();
  Q_INVOKABLE void releaseAssociation();

protected:
  QString applyFilters(QMap<QString,QVariant>);
  bool initializeSCU();

  QScopedPointer<ctkDICOMQueryPrivate> d_ptr;

private:
  Q_DECLARE_PRIVATE(ctkDICOMQuery);
  Q_DISABLE_COPY(ctkDICOMQuery);

  friend class ctkDICOMQuerySCUPrivate;  // for access to queryResponseHandled
};

#endif
