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

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center (DKFZ)
All rights reserved.

Use of this source code is governed by a 3-clause BSD license that can be
found in the LICENSE file.

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

#ifndef QmitkMultiWidgetLayoutSelectionWidget_h
#define QmitkMultiWidgetLayoutSelectionWidget_h

#include <MitkQtWidgetsExports.h>

#include <QmitkAutomatedLayoutWidget.h>

#include <nlohmann/json.hpp>

// qt
#include <QWidget>
#include <memory>

namespace Ui
{
  class QmitkMultiWidgetLayoutSelectionWidget;
}

/**
* \brief Widget that allows the user to select and apply a layout for a multi widget.
*
* Provides a table to define custom row/column layouts, buttons for data-based
* layout selection, and functionality to save and load layouts to/from JSON files.
*/
class MITKQTWIDGETS_EXPORT QmitkMultiWidgetLayoutSelectionWidget : public QWidget
{
  Q_OBJECT

public:

  QmitkMultiWidgetLayoutSelectionWidget(QWidget* parent = nullptr);
  ~QmitkMultiWidgetLayoutSelectionWidget() override;

  void SetDataStorage(mitk::DataStorage::Pointer dataStorage);

Q_SIGNALS:

  void LayoutSet(int row, int column);
  void SetDataBasedLayout(const QList<mitk::DataNode::Pointer>& nodes);

  // needs to be connected via Qt::DirectConnection (usually default), to ensure the stream pointers validity
  void SaveLayout(std::ostream* outStream);

  void LoadLayout(const nlohmann::json* jsonData);

private Q_SLOTS:

  void OnTableItemSelectionChanged();
  void OnSetLayoutButtonClicked();
  void OnDataBasedLayoutButtonClicked();
  void OnSaveLayoutButtonClicked();
  void OnLoadLayoutButtonClicked();
  void OnLayoutPresetSelected(int index);

private:

  void Init();


  std::unique_ptr<Ui::QmitkMultiWidgetLayoutSelectionWidget> ui;
  std::map<int, nlohmann::json> m_PresetMap;
  QmitkAutomatedLayoutWidget* m_AutomatedDataLayoutWidget;

};

#endif
