#include "pylonrecorder.h"

#include <QApplication>
#include <QClipboard>
#include <QDir>
#include <QFileDialog>
#include <QImageReader>
#include <QImageWriter>
#include <QLabel>
#include <QMenuBar>
#include <QMessageBox>
#include <QMimeData>
#include <QPainter>
#include <QScreen>
#include <QScrollArea>
#include <QScrollBar>
#include <QStandardPaths>
#include <QStatusBar>
#include <QToolBar>
#include <QErrorMessage>
#include <iostream>
#include <chrono>
#include <cmath>

#if defined(QT_PRINTSUPPORT_LIB)
#  include <QtPrintSupport/qtprintsupportglobal.h>

#  if QT_CONFIG(printdialog)
#include <QDateTime>
#    include <QPrintDialog>
#  endif
#endif

PylonRecorder::PylonRecorder(QWidget *parent)
  : QMainWindow(parent), imageLabel(new QLabel), scrollArea(new QScrollArea), grabber(nullptr), writer(nullptr), buffer(nullptr), pyloncam(nullptr), dryRun(false), cameraOpened(false)
{
  imageLabel->setBackgroundRole(QPalette::Base);
  imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
  imageLabel->setScaledContents(true);

  scrollArea->setBackgroundRole(QPalette::Dark);
  scrollArea->setWidget(imageLabel);
  scrollArea->setVisible(false);
  setCentralWidget(scrollArea);
  // Pylon::DeviceInfoList_t deviceList = detectCameras();
  // for (auto dev : deviceList) {
  //   std::cout << dev.GetFullName() << " " << dev.GetFriendlyName() << std::endl;
  // }
  // std::cout << "peng\n";
  // std::cout << deviceList.size() << std::endl;
  // std::cout << "peng\n";

  // if (deviceList.size() == 0) {
  //   QErrorMessage message(this);
  //   message.showMessage("No camera detected!");
  // } else {
  //   std::cout << "peng\n";
  //   buffer = new ImageBuffer(defaultBufferSize);
  //   grabber = new Grabber(pyloncam, buffer, defaultFrameRate);
  //   writer = new Writer(buffer);
  //   connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
  // } 
  frameTimer = new QTimer(this);
  connect(frameTimer, &QTimer::timeout, this, &PylonRecorder::displaySingleFrame);
  pressureTimer = new QTimer(this);
  connect(pressureTimer, &QTimer::timeout, this, &PylonRecorder::displayBufferPressure);
  labelTimer = new QTimer(this);
  connect(labelTimer, &QTimer::timeout, this, &PylonRecorder::displayActivity);
  pressureBar = new QProgressBar(this);
  pressureBar->setRange(0, 100);
  pressureBar->setTextVisible(true);
  pressureBar->setFixedSize(200, 25);
  QColor color = progressColor(0);
  QPalette progressPalette = pressureBar->palette();
  progressPalette.setBrush(QPalette::Highlight, QBrush(color));
  pressureBar->setPalette(progressPalette);
  QLabel *preassureLabel = new QLabel("Buffer preassure:", this);
  preassureLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;font-weight: Bold}");
  loadBar = new QProgressBar(this);
  loadBar->setRange(0, defaultBufferSize);
  loadBar->setFixedSize(200, 25);
  QLabel *loadLabel = new QLabel("Load:", this);
  loadLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;font-weight: Bold}");
  writingLabel = new QLabel("writing");
  writingLabel->setEnabled(false);
  writingLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
  grabbingLabel = new QLabel("grabbing");
  grabbingLabel->setEnabled(false);
  grabbingLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
  labelSwitch = false;
  cameraConnectedLabel = new QLabel("not connected");
  cameraConnectedLabel->setStyleSheet("QLabel { color : red; }");
  cameraConnectedLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
  fileLabel = new QLabel();
  fileLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");

  QLabel *camHeader = new QLabel("Camera:");
  camHeader->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial; font-weight: Bold}");
  QLabel *statusHeader = new QLabel("Status:");
  statusHeader->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial; font-weight: Bold}");
  QLabel *fileHeader = new QLabel("Output file:");
  fileHeader->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial; font-weight: Bold}");
  statusBar()->addWidget(camHeader);
  statusBar()->addWidget(cameraConnectedLabel);
  statusBar()->addWidget(preassureLabel);
  statusBar()->addWidget(pressureBar);
  statusBar()->addWidget(loadLabel);
  statusBar()->addWidget(loadBar);
  statusBar()->addWidget(statusHeader);
  statusBar()->addWidget(grabbingLabel);
  statusBar()->addWidget(writingLabel);
  statusBar()->addWidget(fileHeader);
  statusBar()->addWidget(fileLabel);
  resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
  detectCameras();
  createActions();
  updateActions();
  applySettings();
}

// void PylonRecorder::setupCameras(){
//   std::cerr << "Setting up cameras!" << std::endl;
//   qDebug() << "setting up cameras";
//   for (auto dev : deviceList) {
//     std::cout << dev.GetFullName() << " " << dev.GetFriendlyName() << std::endl;
//   }
//   std::string s = "detected ";
//   s += deviceList.size();
//   s += " devices";
//   qDebug() << s.c_str();

//   if (deviceList.size() == 0) {
//     qWarning() << "device list is empty!";
//     QErrorMessage message(this);
//     message.showMessage("No camera detected!");
//   } else {
//     std::cout << "peng\n";
//     std::string cname = (std::string)deviceList[0].GetFullName(); 
//     // qDebug() <<
//     // pyloncam = new PylonWrapper(cname);
//     qDebug() << "Creating buffer, grabber, and writer";
//     buffer = new ImageBuffer(defaultBufferSize);
//     grabber = new Grabber(pyloncam, buffer, defaultFrameRate);
//     writer = new Writer(buffer);
//     connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
//   } 
// }

void PylonRecorder::detectCameras() {
  qDebug() << "Detecting devices!";
  Pylon::CTlFactory& TlFactory = Pylon::CTlFactory::GetInstance();
  TlFactory.EnumerateDevices(deviceList);
  qDebug() << "Found devices!" << deviceList.size();
}

PylonRecorder::~PylonRecorder(){
  qDebug() << "Destructing PylonRecorder";
  if (grabber != nullptr && grabber->isRunning()) { 
      grabber->requestStop();
      grabber->wait(1000);
  }
  if (writer != nullptr && writer->isRunning()) {
      writer->forceStop();
      writer->wait(1000);
  }
  storeSettings();
  if (pyloncam != nullptr) {
    qDebug() << "Deleting pyloncam";
    delete pyloncam;
    pyloncam = nullptr;
  }

  if (buffer != nullptr) {
    qDebug() << "Deleting buffer";
    delete buffer;
    buffer = nullptr;
  }
  qDebug() << "Deleting grabber";
  delete grabber;
  qDebug() << "Deleting writer";
  delete writer;
  qDebug() << "Deleting setting";
  delete settings;
}


void PylonRecorder::applySettings() {
  int bufferSize = settings->value("recorder/buffersize", defaultBufferSize).toInt();
  int exposureTime = settings->value("camera/exposure", defaultExposureTime).toInt();
  int frameRate = settings->value("camera/framerate", defaultFrameRate).toInt();
  int gain = settings->value("camera/gain", defaultGain).toInt();
  storageLocation = settings->value("recorder/storagelocation", "").toString();
  if (!storageLocation.isEmpty()) {
    selectStorageAction->setStatusTip(tr("Default storage location: ") + storageLocation);
  }
  buffersizeSpinner->setValue(bufferSize);
  exposureSpinner->setValue(exposureTime);
  gainSpinner->setValue(gain);
  framerateSpinner->setValue(frameRate);
}


void PylonRecorder::storeSettings() {
  settings->setValue("camera/exposure", exposureSpinner->value());
  settings->setValue("camera/framerate", framerateSpinner->value());
  settings->setValue("camera/gain", gainSpinner->value());
  settings->setValue("recorder/buffersize", buffersizeSpinner->value());
  settings->setValue("recorder/storagelocation", storageLocation);
}


bool PylonRecorder::loadFile(const QString &fileName) {
  QImageReader reader(fileName);
  reader.setAutoTransform(true);
  const QImage newImage = reader.read();
  if (newImage.isNull()) {
      QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
                               tr("Cannot load %1: %2")
                               .arg(QDir::toNativeSeparators(fileName), reader.errorString()));
      return false;
    }

  setImage(newImage);

  setWindowFilePath(fileName);

  const QString message = tr("Opened \"%1\", %2x%3, Depth: %4")
      .arg(QDir::toNativeSeparators(fileName)).arg(image.width()).arg(image.height()).arg(image.depth());
  statusBar()->showMessage(message);
  return true;
}


void PylonRecorder::setImage(const QImage &newImage) {
  image = newImage;
  // (image.colorSpace().isValid())
  //  image.convertToColorSpace(QColorSpace::SRgb);
  imageLabel->setPixmap(QPixmap::fromImage(image));
  // scaleFactor = 1.0;

  scrollArea->setVisible(true);
  printAct->setEnabled(true);
  fitToWindowAct->setEnabled(true);
  //updateActions();

  if (!fitToWindowAct->isChecked()) {
    imageLabel->adjustSize();
    applyScaling();
  }
  this->update();
}


bool PylonRecorder::saveFile(const QString &fileName) {
  QImageWriter writer(fileName);

  if (!writer.write(image)) {
      QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
                               tr("Cannot write %1: %2")
                               .arg(QDir::toNativeSeparators(fileName)), writer.errorString());
      return false;
    }
  const QString message = tr("Wrote \"%1\"").arg(QDir::toNativeSeparators(fileName));
  statusBar()->showMessage(message);
  return true;
}


static void initializeImageFileDialog(QFileDialog &dialog, QFileDialog::AcceptMode acceptMode) {
  static bool firstDialog = true;

  if (firstDialog) {
      firstDialog = false;
      const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
      dialog.setDirectory(picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
    }

  QStringList mimeTypeFilters;
  const QByteArrayList supportedMimeTypes = acceptMode == QFileDialog::AcceptOpen
      ? QImageReader::supportedMimeTypes() : QImageWriter::supportedMimeTypes();
  for (const QByteArray &mimeTypeName : supportedMimeTypes)
    mimeTypeFilters.append(mimeTypeName);
  mimeTypeFilters.sort();
  dialog.setMimeTypeFilters(mimeTypeFilters);
  dialog.selectMimeTypeFilter("image/jpeg");
  if (acceptMode == QFileDialog::AcceptSave)
    dialog.setDefaultSuffix("jpg");
}


void PylonRecorder::open() {
  QFileDialog dialog(this, tr("Open File"));
  initializeImageFileDialog(dialog, QFileDialog::AcceptOpen);

  while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
}


void PylonRecorder::saveAs() {
  QFileDialog dialog(this, tr("Save File As"));
  initializeImageFileDialog(dialog, QFileDialog::AcceptSave);

  while (dialog.exec() == QDialog::Accepted && !saveFile(dialog.selectedFiles().first())) {}
}


void PylonRecorder::print() {
  QPixmap map = imageLabel->pixmap(Qt::ReturnByValue);
  Q_ASSERT(!map.isNull());
  #if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)

  QPrintDialog dialog(&printer, this);

  if (dialog.exec()) {
      QPainter painter(&printer);
      QRect rect = painter.viewport();
      QSize size = map.size();
      size.scale(rect.size(), Qt::KeepAspectRatio);
      painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
      painter.setWindow(map.rect());
      painter.drawPixmap(0, 0, map);
    }
#endif
}


void PylonRecorder::copy() {
#ifndef QT_NO_CLIPBOARD
  QGuiApplication::clipboard()->setImage(image);
#endif // !QT_NO_CLIPBOARD
}


#ifndef QT_NO_CLIPBOARD
static QImage clipboardImage() {
  if (const QMimeData *mimeData = QGuiApplication::clipboard()->mimeData()) {
      if (mimeData->hasImage()) {
          const QImage image = qvariant_cast<QImage>(mimeData->imageData());
          if (!image.isNull())
            return image;
        }
    }
  return QImage();
}
#endif // !QT_NO_CLIPBOARD


void PylonRecorder::paste() {
#ifndef QT_NO_CLIPBOARD
  const QImage newImage = clipboardImage();
  if (newImage.isNull()) {
      statusBar()->showMessage(tr("No image in clipboard"));
    } else {
      setImage(newImage);
      setWindowFilePath(QString());
      const QString message = tr("Obtained image from clipboard, %1x%2, Depth: %3")
          .arg(newImage.width()).arg(newImage.height()).arg(newImage.depth());
      statusBar()->showMessage(message);
    }
#endif // !QT_NO_CLIPBOARD
}


void PylonRecorder::zoomIn() {
  scaleImage(1.25);
}


void PylonRecorder::zoomOut()
{
  scaleImage(0.8);
}


void PylonRecorder::normalSize() {
  imageLabel->adjustSize();
  scaleFactor = 1.0;
}


void PylonRecorder::fitToWindow() {
  bool fitToWindow = fitToWindowAct->isChecked();
  scrollArea->setWidgetResizable(fitToWindow);
  if (!fitToWindow)
    normalSize();
  updateActions();
}


void PylonRecorder::about() {
  QMessageBox::about(this, tr("About Pylon Recorder"),
                     tr("<p><b>Pylon Recorder</b><br> Simple recorder for video grabbing from pylon USB3 monochrome camera."
                        "Usage: In order to grab image, a camera device needs to be opened, press the connect button to do so. Push the record button to record continuously.</p>"
                        "<p> videos are stored in the same folder as the tool is called from. Videos are compressed to mp4."
                        " As the buffer preassure reaches 100% frames will be lost! You may want to increase the buffer size"
                        " or reduce the framerate.</p><p>In order to run you need to install the pylon libraries"
                        " and the mp4 video codec avalable for download at <a href='https://www.baslerweb.com/de/'>Basler website</a></p>"
                        "<p>by Jan Grewe, <a href='http://www.neuroetho.uni-tuebingen.de'>Neuroethology Lab</a>, University of Tuebingen.</p>"));
}


void PylonRecorder::createActions() {
  const QIcon connect_icon(":/images/connect.png");
  const QIcon disconnect_icon(":/images/disconnect.png");
  const QIcon snapshot_icon(":/images/snapshot.png");
  const QIcon grab_icon(":/images/record.png");
  const QIcon stop_icon(":/images/stop.png");
  const QIcon exit_icon(":/images/exit.png");

  QMenu *fileMenu = menuBar()->addMenu(tr("&File"));

  QAction *openAct = fileMenu->addAction(tr("&Open..."), this, &PylonRecorder::open);
  openAct->setShortcut(QKeySequence::Open);

  saveAsAct = fileMenu->addAction(tr("&Save As..."), this, &PylonRecorder::saveAs);
  saveAsAct->setEnabled(false);

  printAct = fileMenu->addAction(tr("&Print..."), this, &PylonRecorder::print);
  printAct->setShortcut(QKeySequence::Print);
  printAct->setEnabled(false);

  fileMenu->addSeparator();

  QAction *exitAct = fileMenu->addAction(exit_icon, tr("E&xit"), this, &PylonRecorder::quitApplication);
  exitAct->setShortcut(tr("Ctrl+Q"));

  QMenu *editMenu = menuBar()->addMenu(tr("&Edit"));

  copyAct = editMenu->addAction(tr("&Copy"), this, &PylonRecorder::copy);
  copyAct->setShortcut(QKeySequence::Copy);
  copyAct->setEnabled(false);

  QAction *pasteAct = editMenu->addAction(tr("&Paste"), this, &PylonRecorder::paste);
  pasteAct->setShortcut(QKeySequence::Paste);

  QMenu *viewMenu = menuBar()->addMenu(tr("&View"));

  zoomInAct = viewMenu->addAction(tr("Zoom &In (25%)"), this, &PylonRecorder::zoomIn);
  zoomInAct->setShortcut(QKeySequence::ZoomIn);
  zoomInAct->setEnabled(false);

  zoomOutAct = viewMenu->addAction(tr("Zoom &Out (25%)"), this, &PylonRecorder::zoomOut);
  zoomOutAct->setShortcut(QKeySequence::ZoomOut);
  zoomOutAct->setEnabled(false);

  normalSizeAct = viewMenu->addAction(tr("&Normal Size"), this, &PylonRecorder::normalSize);
  normalSizeAct->setShortcut(tr("Ctrl+S"));
  normalSizeAct->setEnabled(false);

  viewMenu->addSeparator();

  fitToWindowAct = viewMenu->addAction(tr("&Fit to Window"), this, &PylonRecorder::fitToWindow);
  fitToWindowAct->setEnabled(false);
  fitToWindowAct->setCheckable(true);
  fitToWindowAct->setShortcut(tr("Ctrl+F"));

  QMenu *camera_menu = menuBar()->addMenu(tr("&Camera"));
  connect_camera_action = camera_menu->addAction(connect_icon, tr("&connect"), this, &PylonRecorder::connectCamera);
  connect_camera_action->setStatusTip(tr("Connect to to camera and open device"));
  disconnect_camera_action = camera_menu->addAction(disconnect_icon, tr("&disconnect"), this, &PylonRecorder::disconnectCamera);
  disconnect_camera_action->setStatusTip(tr("Disconnect from the camera device"));
  camera_menu->addSeparator();
  grab_still_action = camera_menu->addAction(snapshot_icon, tr("&grab still"), this, &PylonRecorder::grabStillFromPylon);
  grab_still_action->setStatusTip(tr("Grab single image from Pylon camera"));
  grab_still_action->setShortcut(tr("Ctrl+ "));
  grab_continuous_action = camera_menu->addAction(grab_icon, tr("&grab continuous"), this, &PylonRecorder::startRecording);
  grab_continuous_action->setShortcut(tr("Ctrl+Enter"));
  grab_stop_action = camera_menu->addAction(stop_icon, tr("&stop grabbing"), this, &PylonRecorder::stopRecording);
  grab_stop_action->setShortcut(tr("Ctrl+Shift+Enter"));

  selectStorageAction = new QAction(tr("storage location"), this);
  selectStorageAction->setStatusTip(tr("Select a storage location for the recorded videos"));
  selectStorageAction->setToolTip(tr("Select a storage location for the recorded videos"));
  connect(selectStorageAction, &QAction::triggered, this, &PylonRecorder::selectStorageLocation);
  
  storeSettingsAction = new QAction(tr("store settings"), this);
  storeSettingsAction->setStatusTip(tr("store current settings as defaults"));
  connect(storeSettingsAction, &QAction::triggered, this, &PylonRecorder::storeSettings);

  projectSettingsAction = new QAction(tr("project metadata"), this);
  projectSettingsAction->setStatusTip(tr("Edit project metadata"));
  connect(projectSettingsAction, &QAction::triggered, this, &PylonRecorder::editProjectMetadata);

  QMenu *settingsMenu = menuBar()->addMenu(tr("&Settings"));
  settingsMenu->addAction(selectStorageAction);
  settingsMenu->addAction(projectSettingsAction);
  settingsMenu->addSeparator();
  settingsMenu->setToolTipsVisible(true);
  settingsMenu->addAction(storeSettingsAction);
  
  QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  helpMenu->addAction(tr("&About"), this, &PylonRecorder::about);
  helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);

  framerateSpinner = new QSpinBox();
  framerateSpinner->setRange(1, 120);
  framerateSpinner->setSuffix("Hz");
  framerateSpinner->setFixedSize(80, 25);
  framerateSpinner->setValue(defaultFrameRate);
  framerateSpinner->setStyleSheet("QSpinBox{font-size: 10pt;font-family: Arial;color: rgb(0, 0, 0);background-color: rgb(255,255,255);}");
  //framerateSpinner->setStyleSheet("QSpinBox{font-size: 10px;font-family: Arial;color: rgb(255, 255, 255);background-color: rgb(38,56,76);}");

  buffersizeSpinner = new QSpinBox();
  buffersizeSpinner->setRange(100, 5000);
  buffersizeSpinner->setSingleStep(25);
  buffersizeSpinner->setValue(defaultBufferSize);
  buffersizeSpinner->setFixedSize(100, 25);
  buffersizeSpinner->setStyleSheet("QSpinBox{font-size: 10pt;font-family: Arial;color: rgb(0, 0, 0);background-color: rgb(255,255,255);}");

  exposureSpinner = new QSpinBox();
  exposureSpinner->setRange(10, 50000);
  exposureSpinner->setSingleStep(50);
  exposureSpinner->setValue(defaultExposureTime);
  exposureSpinner->setFixedSize(120, 25);
  exposureSpinner->setStyleSheet("QSpinBox{font-size: 10pt;font-family: Arial;color: rgb(0, 0, 0);background-color: rgb(255,255,255);}");

  gainSpinner = new QSpinBox();
  gainSpinner->setRange(0, 24);
  gainSpinner->setSingleStep(1);
  gainSpinner->setValue(defaultGain);
  gainSpinner->setFixedSize(80, 25);
  gainSpinner->setStyleSheet("QSpinBox{font-size: 10pt;font-family: Arial;color: rgb(0, 0, 0);background-color: rgb(255,255,255);}");

  dryRunCheckBox = new QCheckBox("Dry run, no writing");
  dryRunCheckBox->setChecked(false);
  dryRunCheckBox->setFixedHeight(25);
  dryRunCheckBox->setStyleSheet("QCheckBox{font-size: 12pt;font-family: Arial;color: rgb(0, 0, 0);}");

  QToolBar *toolbar = addToolBar("main toolbar");
  toolbar->addAction(exitAct);
  toolbar->addSeparator();
  toolbar->addAction(connect_camera_action);
  toolbar->addAction(disconnect_camera_action);
  toolbar->addSeparator();
  toolbar->addAction(grab_still_action);
  toolbar->addAction(grab_continuous_action);
  toolbar->addAction(grab_stop_action);
  toolbar->addSeparator();
  toolbar->addWidget(new QLabel("frame rate (Hz):"));
  toolbar->addWidget(framerateSpinner);
  toolbar->addSeparator();
  toolbar->addWidget(new QLabel("gain (dB):"));
  toolbar->addWidget(gainSpinner);
  toolbar->addSeparator();
  toolbar->addWidget(new QLabel("exposure time (us):"));
  toolbar->addWidget(exposureSpinner);
  toolbar->addSeparator();
  toolbar->addWidget(new QLabel("buffer size:"));
  toolbar->addWidget(buffersizeSpinner);
  toolbar->addSeparator();
  toolbar->addWidget(dryRunCheckBox);
}


void PylonRecorder::updateActions() {
  qInfo() << "Update Actions";
  saveAsAct->setEnabled(!image.isNull());
  copyAct->setEnabled(!image.isNull());
  zoomInAct->setEnabled(!fitToWindowAct->isChecked());
  zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
  normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
  disconnect_camera_action->setEnabled(deviceList.size() > 0);
  connect_camera_action->setEnabled(true);
  grab_still_action->setEnabled(deviceList.size() > 0);
  grab_continuous_action->setEnabled(cameraOpened && !grabbing);
  grab_stop_action->setEnabled(grabbing);
}


void PylonRecorder::scaleImage(double factor) {
  QPixmap map = imageLabel->pixmap(Qt::ReturnByValue);
  Q_ASSERT(!map.isNull());
  scaleFactor *= factor;
  imageLabel->resize(scaleFactor * map.size());

  adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
  adjustScrollBar(scrollArea->verticalScrollBar(), factor);

  zoomInAct->setEnabled(scaleFactor < 3.0);
  zoomOutAct->setEnabled(scaleFactor > 0.333);
}


void PylonRecorder::applyScaling(){
  imageLabel->resize(scaleFactor * imageLabel->pixmap(Qt::ReturnByValue).size());
}


void PylonRecorder::quitApplication() {
  qDebug() << "QuitApplication: Quit Application!";
  if (grabbing) {
    std::cerr << "QuitApplication: Stop grabbing\n";
    qDebug() << "QuitApplication: Stop grabbing";
    stopRecording();
  }
  qDebug() << "QuitApplication done!";
  this->close();
  std::cerr << "QuitApplication: done,\n";
}


void PylonRecorder::adjustScrollBar(QScrollBar *scrollBar, double factor) {
  scrollBar->setValue(int(factor * scrollBar->value()
                          + ((factor - 1) * scrollBar->pageStep()/2)));
}

void PylonRecorder::cameraConfiguration(){
    d = new CamConfigurator(deviceList, this);
    connect(d, SIGNAL(accepted()), SLOT(camerasetup()));
    // QObject::connect(&d, SIGNAL(column_visibility_changed(QString, QString,bool)), this, SLOT(visible_columns_update(QString, QString,bool)));
    d->exec();
}

void PylonRecorder::camerasetup() {
  qDebug() << "Cameras setting " << ((d->result()) ? "Accepted" : "Discarded");
  this->layout = d->layout();
  delete d;
}

void PylonRecorder::connectCamera() {
  this->layout = CameraLayout();
  std::cerr << this->layout.rois.size() << " " << this->layout.devices.size() << std::endl;
  qDebug() << "connecting camera(s)";
  if (deviceList.size() == 0) {
    detectCameras();
    QMessageBox msgBox;
    QString msg = "<p><b>No camera device found!</b></p><br><p>Connect camera and try again!</p>";
    msgBox.setText(msg);
    msgBox.exec();
  } else {
    cameraConfiguration();
    if (layout.mode == CameraMode::single && layout.devices.size() == 1) {
      qDebug() << "single camera mode";
      std::string cname = layout.devices[0];
      std::string message;
      qDebug() << "connecting to camera " << cname.c_str();
      pyloncam = new PylonWrapper(layout);
      bool success = pyloncam->openCamera(message);
      if (success) {
        cameraConnectedLabel->setText("connected");
        cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: green;}");
        cameraOpened = true;
      } else {
        QMessageBox msgBox;
        QString msg = "<p><b>Could not open camera device!</b><p><p>" + QString::fromStdString(message) + "</p>";
        msgBox.setText(msg);
        msgBox.exec();
        cameraOpened = false;
      }
      statusBar()->showMessage(QString::fromStdString(message));
      updateActions();
    }
  }
  qDebug() << "connecting cam(s) done!";
}


void PylonRecorder::disconnectCamera() {
  qDebug() << "disconnecting camera";
  if (pyloncam != nullptr && pyloncam->isOpen()) {
    pyloncam->closeCamera();
    statusBar()->showMessage(tr("Camera closed!"));
    cameraConnectedLabel->setText("not connected");
    cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: red;}");
    updateActions();
  }
  qDebug() << "disconnecting cameras done";
}

VideoSpecs PylonRecorder::getVideoSpecs(const ImageSettings &settings) {
  VideoSpecs s = VideoSpecs();
  if (this->layout.mode == CameraMode::single && this->layout.devices.size() > 0) {
    s.fps = framerateSpinner->value();
    s.exposureTime = static_cast<double>(exposureSpinner->value());
    s.detectorGain = static_cast<double>(gainSpinner->value());
    s.width = static_cast<uint32_t>(this->layout.rois[0].width);
    s.height = static_cast<uint32_t>(this->layout.rois[0].height);
    s.xoffset = static_cast<uint32_t>(this->layout.rois[0].x);
    s.yoffset = static_cast<uint32_t>(this->layout.rois[0].y);

    s.pixelType = settings.pixelType;
    s.orientation = settings.orientation;
    s.quality = 95;
  } else {
    qWarning() << "Dual camera mode not supported yet!";
  }
  return s;
}

void PylonRecorder::startRecording() {
  qDebug() << "start recording!";
  std::string filename = createFilename(".mp4");
  fileLabel->setText(QString::fromStdString(filename));
  qDebug() << "storing to file " << filename.c_str();

  ImageSettings settings = pyloncam->getImageSettings();
  qDebug() << "got image settings";

  VideoSpecs specs = getVideoSpecs(settings);
  specs.filename = filename;
  specs.format = VideoFormat::mp4;
  qDebug() << "got video specifications";

  if (buffer != nullptr) {
    buffer->clear();
    delete buffer;
    buffer = nullptr;
  }
  qDebug() << "setting image buffer to size " << buffersizeSpinner->value();
  buffer = new ImageBuffer(defaultBufferSize);
  if (buffersizeSpinner->value() != static_cast<int>(buffer->capacity())) {
    buffer->resize(static_cast<size_t>(buffersizeSpinner->value()));
    loadBar->setRange(0, buffersizeSpinner->value());
  }

  qDebug() << "setting up grabber";
  if (grabber != nullptr) {
    delete grabber;
    grabber = nullptr;
  }
  grabber = new Grabber(pyloncam, buffer, defaultFrameRate);
  
  if (framerateSpinner->value() != grabber->currentFramerate())
    grabber->setFrameRate(framerateSpinner->value());
  if (exposureSpinner->value() != int(grabber->currentExposureTime()))
      grabber->setExposureTime(static_cast<double>(exposureSpinner->value()));
  if (gainSpinner->value() != int(grabber->currentGain()))
      grabber->setGain(static_cast<double>(gainSpinner->value()));

  qDebug() << "setup writer";
  if (writer != nullptr) {
    delete writer;
    writer = nullptr;
  }
  writer = new Writer(buffer);
  connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
  writer->setVideoSpecs(specs);

  QSettings s;
  this->mdata.read(s);
  writer->setProjectMetadata(mdata);

  buffer->clear();
  if (!dryRunCheckBox->isChecked()) {
    writer->start();
    writing = true;
  }
  dryRun = dryRunCheckBox->isChecked();
  grabber->start();
  grabbing = true;
  stopRequest = false;

  pressureTimer->start(50);
  frameTimer->start(50);
  labelTimer->start(650);
  updateActions();
}


void PylonRecorder::stopRecording() {
  qDebug() << "StopRecording!";
  if (!stopRequest) {
      qDebug() << "StopRecording: stop frame timer!";
      frameTimer->stop();
      qDebug() << "StopRecording: stop grabber!";
      if (grabber !=nullptr)
        grabber->requestStop();
      qDebug() << "StopRecording: stop writer!";
      if (writer != nullptr)
        writer->requestStop();
      grabbing = false;
      stopRequest = true;
      grab_stop_action->setEnabled(false);
      qDebug() << "StopRecording: clear buffer!";
      if(buffer != nullptr) {
          buffer->clear();
          writerDone();
      }
  }
  qDebug() << "StopRecording done!";
}


void PylonRecorder::writerDone() {
  std::cerr << "writer is Done!!!" << std::endl;
  pressureTimer->stop();
  pressureBar->reset();
  loadBar->reset();
  labelTimer->stop();
  writingLabel->setStyleSheet(inactiveLabelStyle);
  grabbingLabel->setStyleSheet(inactiveLabelStyle);
  grabber->wait(10000);
  writer->wait(10000);
  writing = false;
  updateActions();
  qInfo() << "writer is Done!";
}


void PylonRecorder::displayActivity() {
  grabbingLabel->setStyleSheet((labelSwitch && grabbing) ? activeLabelStyleHigh : activeLabelStyleLow);
  writingLabel->setStyleSheet((labelSwitch && writing) ? activeLabelStyleHigh : activeLabelStyleLow);
  labelSwitch = !labelSwitch;
}


void PylonRecorder::displaySingleFrame() {
  MyImage *img;
  // if (dryRunCheckBox->isChecked())
  //   img = buffer->pop();
  // else {
  img = buffer->readLast();
  // }
  if (img != nullptr){
    QImage qimg(static_cast<uchar *>(img->data()), img->width(), img->height(), QImage::Format::Format_Grayscale8);
    setImage(qimg);
  }else {
      std::cerr << "Error reading last image" << std::endl;
  }
}


QColor PylonRecorder::progressColor(int value) {
  int c, m, k = 0, y = 255;
  c = 255 - 255 * value/100;
  if (value < 50) {
      m = 127 - 127 * value/50;
  } else {
      m = 255 * (value-50)/50;
  }
  return QColor::fromCmyk(c, m, y, k);
}


std::string PylonRecorder::createFilename(const std::string &extension) {
  QDateTime dt(QDateTime::currentDateTimeUtc());
  QDate date = dt.date();
  std::string base = (date.toString("yyyy.MM.dd") + "_").toStdString();
  QString idx = QString::number(movieCount);
  std::string fname = base + idx.toStdString() + extension;
  while (QFile::exists(QString::fromStdString(fname))) {
      movieCount++;
      fname = base + QString::number(movieCount).toStdString() + extension;
  }
  return fname;
}


void PylonRecorder::displayBufferPressure() {
  int value = static_cast<int>(round(buffer->bufferPressure()));
  pressureBar->setValue(value);
  QColor color = progressColor(value);
  progressPalette.setBrush(QPalette::Highlight, QBrush(color));
  pressureBar->setPalette(progressPalette);

  int load = static_cast<int>(buffer->bufferLoad());
  loadBar->setValue(load);
}


void PylonRecorder::grabStillFromPylon() {
  qDebug() << "Grab still image form camera!";
  if (pyloncam != nullptr && pyloncam->isOpen()) {
      MyImage img;
      bool valid = pyloncam->grabFrame(img);
      if (valid) {
          QImage qimg(static_cast<uchar *>(img.data()), img.width(), img.height(),
                     QImage::Format::Format_Grayscale8);
          setImage(qimg);
        }
    } else {
      statusBar()->showMessage(tr("Camera is not open! Connect to camera first!"));
    }
  qDebug() << "grabbing still image done!";
}


void PylonRecorder::selectStorageLocation() {
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                   "/home",
                                                   QFileDialog::ShowDirsOnly
                                                  | QFileDialog::DontResolveSymlinks);
                                                
    if (!dir.isEmpty()) {
      this->storageLocation = dir;
      selectStorageAction->setStatusTip(tr("Storage location: ") + storageLocation);
    }    
}


void PylonRecorder::editProjectMetadata(){
  ProjectSettings *dlg = new ProjectSettings(this);

  dlg->setModal(true);
  int res = dlg->exec();
  if (res == QDialog::Accepted) {
    this->mdata = dlg->getMetadata();
  }
  
}