/* This source file is part of the Tomviz project, https://tomviz.org/.
   It is released under the 3-Clause BSD License, see "LICENSE". */

#include "Behaviors.h"

#include "ActiveObjects.h"
#include "AddRenderViewContextMenuBehavior.h"
#include "ShiftRotationCenterWidget.h"
#include "ManualManipulationWidget.h"
#include "MoveActiveObject.h"
#include "OperatorPython.h"
#include "RotateAlignWidget.h"
#include "TimeSeriesLabel.h"
#include "ViewFrameActions.h"

#include <pqAlwaysConnectedBehavior.h>
#include <pqApplicationCore.h>
#include <pqDefaultViewBehavior.h>
#include <pqInterfaceTracker.h>
#include <pqPersistentMainWindowStateBehavior.h>
#include <pqStandardPropertyWidgetInterface.h>
#include <pqViewStreamingBehavior.h>
#include <vtkSMReaderFactory.h>
#include <vtkSMSessionProxyManager.h>
#include <vtkSMSettings.h>

#include <QMainWindow>
#include <QTextCharFormat>
#include <QTextCursor>

#include <sstream>

// Include the XML configuration data. Should be generated by CMake.
#include "pvextensions/TomvizExtensionsXml.h"

const char* const settings = "{"
                             "   \"settings\" : {"
                             "      \"RenderViewSettings\" : {"
                             "         \"LODThreshold\" : 102400.0,"
                             "         \"UseFXAA\": 1,"
                             //"         \"ShowAnnotation\" : 1,"
                             "         \"UseDisplayLists\" : 1"
                             "      }"
                             "  },"
                             "  \"misc\" : {"
                             "      \"SaveAnimation\" : {"
                             "         \"FrameRate\" : 30"
                             "      }"
                             "   }"
                             "}";

Q_DECLARE_METATYPE(QTextCharFormat)
Q_DECLARE_METATYPE(QTextCursor)

namespace tomviz {

Behaviors::Behaviors(QMainWindow* mainWindow) : QObject(mainWindow)
{
  Q_ASSERT(mainWindow);

  qRegisterMetaType<QTextCharFormat>();
  qRegisterMetaType<QTextCursor>();

  vtkSMReaderFactory::AddReaderToWhitelist("sources", "JPEGSeriesReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "PNGSeriesReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "TIFFSeriesReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "TVRawImageReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "MRCSeriesReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "XMLImageDataReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "XdmfReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "CSVReader");
  vtkSMReaderFactory::AddReaderToWhitelist("sources", "MetaImageReader");

  vtkSMSettings::GetInstance()->AddCollectionFromString(settings, 0.0);

  // Register ParaView interfaces.
  auto pgm = pqApplicationCore::instance()->interfaceTracker();

  // * add support for ParaView properties panel widgets.
  pgm->addInterface(new pqStandardPropertyWidgetInterface(pgm));

  pgm->addInterface(new ViewFrameActions(pgm));

  new pqDefaultViewBehavior(this);
  new pqAlwaysConnectedBehavior(this);
  new pqViewStreamingBehavior(this);
  new pqPersistentMainWindowStateBehavior(mainWindow);

  new tomviz::AddRenderViewContextMenuBehavior(this);

  m_moveActiveBehavior.reset(new tomviz::MoveActiveObject(this));
  m_timeSeriesLabel.reset(new tomviz::TimeSeriesLabel(this));

  // This will trigger the logic to setup reader/writer factories, etc.
  pqApplicationCore::instance()->loadConfigurationXML("<xml/>");

  registerCustomOperatorUIs();

  auto pxm = ActiveObjects::instance().proxyManager();
  pxm->LoadConfigurationXML(tomvizExtensionsXml);
}

void Behaviors::registerCustomOperatorUIs()
{
  OperatorPython::registerCustomWidget("ShiftRotationCenterWidget", true,
                                       ShiftRotationCenterWidget::New);
  OperatorPython::registerCustomWidget("RotationAlignWidget", true,
                                       RotateAlignWidget::New);
  OperatorPython::registerCustomWidget("ManualManipulationWidget", true,
                                       ManualManipulationWidget::New);
}

} // end of namespace tomviz
