#include "camerapreview.h"
#include <QMessageBox>
#include <math.h> 
#include "mylogger.h"

CameraPreview::CameraPreview(QWidget *parent):cameraname(""), camera(nullptr), QWidget(parent) {
    qDebug() << "Camera preview constructor";
    label = new QLabel(cameraname);
    this->setLayout(new QVBoxLayout(this));
    this->layout()->addWidget(label);
    imgLabel = new QLabel(this);
    imgLabel->setMinimumSize(QSize(800, 600));
    this->layout()->addWidget(imgLabel);
    QWidget *controls = new QWidget(this);

    width = new QSpinBox(controls);
    width->setMinimum(32);
    width->setMaximum(2048);
    width->setSingleStep(32);
    width->setValue(width->maximum());
    connect(width, SIGNAL(textChanged(QString)), SLOT(updateWidth(QString)));

    height = new QSpinBox(controls);
    height->setSingleStep(32);
    height->setMinimum(32);
    height->setMaximum(1536);
    height->setValue(height->maximum());
    connect(height, SIGNAL(textChanged(QString)), SLOT(updateHeight(QString)));

    xoffs = new QSpinBox(controls);
    xoffs->setSingleStep(8);
    xoffs->setMinimum(0);
    xoffs->setMaximum(2047);
    xoffs->setValue(0);
    connect(xoffs, SIGNAL(textChanged(QString)), SLOT(updateXoffs(QString)));

    yoffs = new QSpinBox(controls);
    yoffs->setSingleStep(8);
    yoffs->setMinimum(0);
    yoffs->setMaximum(1535);
    yoffs->setValue(0);
    connect(yoffs, SIGNAL(textChanged(QString)), SLOT(updateYoffs(QString)));

    QGridLayout *grid = new QGridLayout(controls);
    grid->addWidget(new QLabel("width", controls), 0, 0);
    grid->addWidget(width, 0, 1);

    grid->addWidget(new QLabel("height", controls), 1, 0);
    grid->addWidget(height, 1, 1);

    grid->addWidget(new QLabel("x-offset", controls), 0, 2);
    grid->addWidget(xoffs, 0, 3);

    grid->addWidget(new QLabel("y-offset", controls), 1, 2);
    grid->addWidget(yoffs, 1, 3);
    controls->setLayout(grid);
    this->layout()->addWidget(controls);
    //setPrimaryCamera(devicename);
    qDebug() << "Camera preview constructor";
}


void CameraPreview::setCamera(QString &device){
    qDebug() << "update camera! " << device;
    cameraname = device;
    if (camera != nullptr) {
        qDebug() << "camera is not nullptr! ";
        delete camera;
        camera = nullptr;
    }
    camera = new PylonWrapper(cameraname.toStdString());
    std::string message;
    bool success = camera->openCamera(message);
    if (!success) {
        qWarning() << "Could not open camera device!";
        qWarning() << QString::fromStdString(message);
        QMessageBox msgBox;
        QString msg = "<p><b>Could not open camera device!</b><p><p>" + QString::fromStdString(message) + "</p>";
        msgBox.setText(msg);
        msgBox.exec();
        return;
    }
    int64_t max_height = camera->sensorHeight();
    int64_t max_width = camera->sensorWidth();
    std::cerr << "set spin box values " << max_width << " " << max_height << std::endl;
    width->blockSignals(true);
    height->blockSignals(true);
    xoffs->blockSignals(true);
    yoffs->blockSignals(true);
    width->setMaximum(max_width);
    width->setValue(max_width);
    height->setMaximum(max_height);
    height->setValue(max_height);
    xoffs->setMaximum(max_width - 1);
    xoffs->setValue(0);
    yoffs->setMaximum(max_height - 1);
    yoffs->setValue(0);
    label->setText(device + " - " + camera->userName());
    width->blockSignals(false);
    height->blockSignals(false);
    xoffs->blockSignals(false);
    yoffs->blockSignals(false);
    takeStill();
}


void CameraPreview::takeStill() {
    qDebug() << "Take Still image!";
    if (camera != nullptr && camera->isOpen()) {
      ImageSettings s = camera->getImageSettings();
      MyImage mimg(s.width, s.height);
      bool valid = camera->grabFrame(mimg);
      if (!valid) {
          qWarning() << "Grabbing from camera failed!";
          return;
      }
      qDebug() << "Grabbed image from camera succeeded!";
      QImage img(static_cast<uchar *>(mimg.data()), mimg.width(), mimg.height(),
                 QImage::Format::Format_Grayscale8);
      QPixmap mpm = QPixmap::fromImage(img);
      this->pm = mpm;
      mpm = mpm.scaledToWidth(800);
      setImage(mpm);
      updateROI();
    } else {
      qDebug() << "Camera is not open! Connect to camera first!";
    }
}

void CameraPreview::setImage(const QPixmap &img) {
    imgLabel->setPixmap(img);
}

void CameraPreview::updateWidth(QString s) {
    // if (xoffs->value() + width->value() > 2048) {
    //     xoffs->setValue(2048 - width->value());
    // }
    validate(width, xoffs, camera->sensorWidth());
    updateROI();
}

void CameraPreview::updateXoffs(QString s) {
    // if (xoffs->value() + width->value() > 2048) {
    //     width->setValue(2048 - xoffs->value());
    // }
    validate(xoffs, width, camera->sensorHeight());
    updateROI();
}

void CameraPreview::updateHeight(QString s) {
    // if (height->value() + yoffs->value() > 1536) {
    //     yoffs->setValue(1536 - height->value());
    // }
    validate(height, yoffs, camera->sensorHeight());
    updateROI();
}

void CameraPreview::updateYoffs(QString s) {
    // if (height->value() + yoffs->value() > 1536) {
    //     height->setValue(1536 - yoffs->value());
    // }
    validate(yoffs, height, camera->sensorWidth());
    updateROI();
}

void CameraPreview::validate(QSpinBox *origin, QSpinBox *dest, int limit){
    qDebug() << "validate";
    int val = ensureDivbyfour(origin->value());
    origin->setValue(val);
    if (origin->value() + dest->value() > limit) {
        dest->setValue(limit - origin->value());
    }
    qDebug() << "validate done";
}

int CameraPreview::ensureDivbyfour(int val) {
    // if (val % 4 != 0) {
    //     int divisor = floor(val / 4);
    //     val = divisor * 4;
    // }
    return val;
}

void CameraPreview::updateROI(bool emitSignal) {
    qDebug() << "Update roi with signal: " << emitSignal;
    QImage img = pm.toImage();
    double scaling = 800.0 / img.width();
    img = img.scaledToWidth(800);
    QPainter qPainter(&img);
    qPainter.setBrush(Qt::NoBrush);
    qPainter.setPen(Qt::red);
    QPen p = qPainter.pen();
    p.setWidthF(1.5);
    qPainter.setPen(p);
    int x = xoffs->value();
    int rxoffs = round(xoffs->value() * scaling);
    int ryoffs = round(yoffs->value() * scaling);
    int rwidth = round(width->value() * scaling);
    int rheight = round(height->value() * scaling);
    qPainter.drawRect(rxoffs, ryoffs, rwidth, rheight);
    bool bEnd = qPainter.end();
    QPixmap npm = QPixmap::fromImage(img);
    setImage(npm);
    if (emitSignal) {
        emit roiUpdated(xoffs->value(), yoffs->value(), width->value(), height->value());
    }
    qDebug() << "update ROI done";
}

void CameraPreview::setSize(int w, int h) {
    qDebug() << "set size " << w << " " << h;
    width->setValue(w);
    height->setValue(h);
    if (camera != nullptr && camera->isOpen()) {
        validate(width, xoffs, camera->sensorWidth());
        validate(height, yoffs, camera->sensorHeight());
    }
    updateROI(false);
    qDebug() << "set size done";
}

QString CameraPreview::device(){
    return cameraname;
}

ROI CameraPreview::getRoi() {
    ROI r;
    uint32_t max_height = camera->sensorHeight();
    uint32_t max_width = camera->sensorWidth();
    r.x = xoffs->value();
    r.y = yoffs->value();
    r.width = width->value();
    r.height = height->value();
    r.height = r.height > max_height ? max_height - r.y : r.height;
    r.width = r.width > max_width ? max_width - r.x : r.width;
    return r;
}

CameraPreview::~CameraPreview(){
    qDebug() << "cameraPreview destructor";
    if (camera != nullptr){ 
        qDebug() << "close camera";
        // camera->closeCamera();
        delete camera;
        camera = nullptr;
    }
    qDebug() << "deleted camera";

}