// +-------------------------------- DISCLAIMER ---------------------------------+
// |                                                                             |
// | This program is provided to you free of charge "as such".                   |
// | You should not assume that this program is error-free or suitable for any   |
// | purpose whatsoever.                                                         |
// |                                                                             |
// | EURESYS does not give any representation, warranty or undertaking that this |
// | program is free of any defect or error or suitable for any purpose. EURESYS |
// | shall not be liable, in contract, in torts or otherwise, for any damages,   |
// | loss, costs, expenses or other claims for compensation, including those     |
// | asserted by third parties, arising out of or in connection with the use of  |
// | this program.                                                               |
// +-----------------------------------------------------------------------------+

#include "CameraWidget.h"
#include <iostream>
#include <QPainter>
#include <QMetaObject>
#include <chrono>


CameraWidget::CameraWidget(const std::string& deepLearningModel, const std::string& cameraParams, QObject* /*parent*/): cameraHandler_(cameraParams), processingHandler_(deepLearningModel, &cameraHandler_), resized_(false)
{
  connect(&processingHandler_, &ProcessingHandler::renderedImage, this, &CameraWidget::updatePixmap);
  processingHandler_.start();
  cameraHandler_.launchCamera();
  if (cameraHandler_.abort_ || processingHandler_.abort_)
  {
    std::cerr << "some error occured, closing application" << std::endl;
    QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);  // call close once we have left the constructor
  }
  else
  {
    std::this_thread::sleep_for(std::chrono::milliseconds(100));  // waits for 100ms, assuming a problem will have happened by then
    if (cameraHandler_.abort_ || processingHandler_.abort_)
    {
      std::cerr << "some error occured, closing application" << std::endl;
      QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
    }
  }
}

CameraWidget::~CameraWidget()
{
}

void CameraWidget::paintEvent(QPaintEvent*)
{
  QPainter painter(this);
  painter.fillRect(rect(), Qt::black);
  if (pixmap_.isNull())
  {
    painter.setPen(Qt::white);
    painter.drawText(rect(), Qt::AlignCenter, tr("No image..."));
    return;
  }

  painter.drawPixmap(rect(), pixmap_, QRect(0, 0, pixmap_.width(), pixmap_.height()));

  if (label_.isEmpty())
    return;

  QRect boundingRect = painter.boundingRect(10, 10, 200, 50, 0, label_);
  painter.fillRect(boundingRect, QWidget::palette().color(QWidget::backgroundRole()));
  painter.drawText(boundingRect, 0, label_);
}

void CameraWidget::updatePixmap(const QImage& image, const QString& label)
{
  pixmap_ = QPixmap::fromImage(image);
  label_ = label;

  if (!resized_ && image.size() != size())  // resize the window to the size of the camera
  {
    resized_ = true;
    resize(image.size());
  }

  update();
}
