#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 <iostream>
#include <chrono>
#include <cmath>

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

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

PylonRecorder::PylonRecorder(QWidget *parent)
  : QMainWindow(parent), imageLabel(new QLabel), scrollArea(new QScrollArea)
{
  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 = new PylonWrapper();
  buffer = new ImageBuffer(1000);
  grabber = new Grabber(pylon, buffer);
  writer = new Writer(buffer);
  createActions();
  updateActions();

  frameTimer = new QTimer(this);
  connect(frameTimer, &QTimer::timeout, this, &PylonRecorder::displaySingleFrame);
  preassureTimer = new QTimer(this);
  connect(preassureTimer, &QTimer::timeout, this, &PylonRecorder::displayBufferPreassure);

  preassureBar = new QProgressBar(this);
  preassureBar->setRange(0, 100);
  preassureBar->setTextVisible(true);
  QColor color = progressColor(0);
  QPalette progressPalette = preassureBar->palette();
  progressPalette.setBrush(QPalette::Highlight, QBrush(color));
  preassureBar->setPalette(progressPalette);
  QLabel *preassureLabel = new QLabel("Buffer preassure:", this);

  statusBar()->addWidget(preassureLabel);
  statusBar()->addWidget(preassureBar);
  resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
}

PylonRecorder::~PylonRecorder(){
  delete pylon;
  delete buffer;
  delete grabber;
  delete writer;
}

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();
}

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() {
  Q_ASSERT(imageLabel->pixmap());
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printdialog)

  QPrintDialog dialog(&printer, this);

  if (dialog.exec()) {
      QPainter painter(&printer);
      QRect rect = painter.viewport();
      QSize size = imageLabel->pixmap()->size();
      size.scale(rect.size(), Qt::KeepAspectRatio);
      painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
      painter.setWindow(imageLabel->pixmap()->rect());
      painter.drawPixmap(0, 0, *imageLabel->pixmap());
    }
#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 Image Viewer"),
                     tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
                        "and QScrollArea to display an image. QLabel is typically used "
                        "for displaying a text, but it can also display an image. "
                        "QScrollArea provides a scrolling view around another widget. "
                        "If the child widget exceeds the size of the frame, QScrollArea "
                        "automatically provides scroll bars. </p><p>The example "
                        "demonstrates how QLabel's ability to scale its contents "
                        "(QLabel::scaledContents), and QScrollArea's ability to "
                        "automatically resize its contents "
                        "(QScrollArea::widgetResizable), can be used to implement "
                        "zooming and scaling features. </p><p>In addition the example "
                        "shows how to use QPainter to print an image.</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);

  QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
  helpMenu->addAction(tr("&About"), this, &PylonRecorder::about);
  helpMenu->addAction(tr("About &Qt"), &QApplication::aboutQt);

  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);
}

void PylonRecorder::updateActions() {
  saveAsAct->setEnabled(!image.isNull());
  copyAct->setEnabled(!image.isNull());
  zoomInAct->setEnabled(!fitToWindowAct->isChecked());
  zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
  normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
  disconnect_camera_action->setEnabled(pylon->isOpen());
  connect_camera_action->setEnabled(!pylon->isOpen());
  grab_still_action->setEnabled(pylon->isOpen());
  grab_continuous_action->setEnabled(pylon->isOpen() && !grabbing);
  grab_stop_action->setEnabled(grabbing);
}

void PylonRecorder::scaleImage(double factor) {
  Q_ASSERT(imageLabel->pixmap());
  scaleFactor *= factor;
  imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

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

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

void PylonRecorder::quitApplication() {
  if (pylon->isOpen()) {
      if (grabbing) {
          stopRecording();
      }
    pylon->closeCamera();
  }
  this->close();
}

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

void PylonRecorder::connectCamera() {
  std::string message;
  pylon->openCamera(message);
  statusBar()->showMessage(QString::fromStdString(message));
  updateActions();
}

void PylonRecorder::disconnectCamera() {
  pylon->closeCamera();
  statusBar()->showMessage(tr("Camera closed!"));
  updateActions();
}

void PylonRecorder::startRecording() {
  VideoSpecs specs;
  specs.fps = 25;
  specs.filename = "Test.mp4";
  ImageSettings settings = pylon->getImageSettings();
  specs.width = static_cast<uint32_t>(settings.width);
  specs.height= static_cast<uint32_t>(settings.height);
  specs.pixelType = settings.pixelType;
  specs.orientation = settings.orientation;
  specs.quality = 95;
  writer->setVideoSpecs(specs);
  buffer->clear();
  grabber->start();
  writer->start();
  grabbing = true;

  preassureTimer->start(50);
  frameTimer->start(50);
  updateActions();

}

void PylonRecorder::stopRecording() {
  grabber->requestStop();
  writer->requestStop();
  grabber->wait(10000);
  writer->wait(10000);
  grabbing = false;
  frameTimer->stop();
  preassureTimer->stop();
  preassureBar->reset();
  updateActions();
}

void PylonRecorder::displaySingleFrame() {
     MyImage img;
     bool valid = buffer->readLast(img);
     if (valid) {
      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);
}

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

void PylonRecorder::grabStillFromPylon() {
  if (pylon->isOpen()) {
      MyImage img;
      bool valid = pylon->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!"));
    }
}