#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRegularExpression>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    process(new QProcess(this))
{
    ui->setupUi(this);

    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::btnOBRClicked);
    connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::btnOCRClicked);

    connect(process, &QProcess::readyReadStandardOutput, this, [=]() {
        QByteArray output = process->readAllStandardOutput();
        QString outputText = QString::fromUtf8(output);

        QStringList lines = outputText.split("\n");
        for (QString line : lines) {
            if (line.contains("[DISPLAY]")) {
                line.remove("[DISPLAY]");
                ui->textBrowser->append(line);
            }else if(line.contains("ERROR")){
                ui->textBrowser->append(line);
            }
        }
    });


    connect(process, &QProcess::readyReadStandardError, this, [=]() {
        QByteArray errorOutput = process->readAllStandardError();
        ui->textBrowser->append("Error: " + errorOutput);
    });

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::btnOBRClicked()
{
    QString program = "./sdksample";
    QStringList arguments;

    QString deviceName = ui->lineEdit->text();
    QString startPage = ui->lineEdit_2->text();
    if (startPage.isEmpty()) {
        startPage = "0";
    }

    QString endPage = ui->lineEdit_3->text();
    if (endPage.isEmpty()) {
        endPage = "0";
    }

    QString left = ui->lineEdit_4->text();
    if (left.isEmpty()) {
        left = "0";
    }

    QString top = ui->lineEdit_5->text();
    if (top.isEmpty()) {
        top = "0";
    }

    QString right = ui->lineEdit_6->text();
    if (right.isEmpty()) {
        right = "0";
    }

    QString bottom = ui->lineEdit_7->text();
    if (bottom.isEmpty()) {
        bottom = "0";
    }

    int unit = ui->comboBox->currentIndex();

    QRegularExpression regex("^[0-9.]+$");
    QRegExp re("\\d+");

    if (regex.match(deviceName).hasMatch()) {
        deviceName.prepend("IP:");
    }

     ui->textBrowser->clear();

    if(re.exactMatch(startPage) &&
        re.exactMatch(endPage)  &&
        re.exactMatch(left)     &&
        re.exactMatch(top)      &&
        re.exactMatch(right)    &&
        re.exactMatch(bottom)
    ){
        arguments << "--scan" << deviceName << "--mode" << "0" << "--obr"
                  << "-s" << startPage << "-e" << endPage << "-u" << QString::number(unit)
                  << "-l" << left << "-t" << top << "-r" << right << "-b" << bottom;
        qDebug() << arguments;
    }else{
        ui->textBrowser->setText("Recognize Page or Area invalid parameter.");
        return;
    }

    process->start(program, arguments);

    if (!process->waitForStarted()) {
        ui->textBrowser->setText("Failed to start process.");
    }

    if (process->waitForFinished()){
        if(ui->textBrowser->toPlainText().isEmpty()){
            ui->textBrowser->setText("OBR Result 0");
        }
    }else{
        ui->textBrowser->setText("Failed to finish process.");
    }


}


void MainWindow::btnOCRClicked()
{
    QString program = "./sdksample";
    QStringList arguments;

    QString deviceName = ui->lineEdit->text();
    QString startPage = ui->lineEdit_2->text();
    if (startPage.isEmpty()) {
        startPage = "0";
    }

    QString endPage = ui->lineEdit_3->text();
    if (endPage.isEmpty()) {
        endPage = "0";
    }

    QString left = ui->lineEdit_4->text();
    if (left.isEmpty()) {
        left = "0";
    }

    QString top = ui->lineEdit_5->text();
    if (top.isEmpty()) {
        top = "0";
    }

    QString right = ui->lineEdit_6->text();
    if (right.isEmpty()) {
        right = "0";
    }

    QString bottom = ui->lineEdit_7->text();
    if (bottom.isEmpty()) {
        bottom = "0";
    }

    int unit = ui->comboBox->currentIndex();


    QRegularExpression regex("^[0-9.]+$");
    QRegExp re("\\d+");

    if (regex.match(deviceName).hasMatch()) {
        deviceName.prepend("IP:");
    }

     ui->textBrowser->clear();

    if(re.exactMatch(startPage) &&
        re.exactMatch(endPage)  &&
        re.exactMatch(left)     &&
        re.exactMatch(top)      &&
        re.exactMatch(right)    &&
        re.exactMatch(bottom)
    ){
        arguments << "--scan" << deviceName << "--mode" << "0" << "--ocr"
                  << "-s" << startPage << "-e" << endPage << "-u" << QString::number(unit)
                  << "-l" << left << "-t" << top << "-r" << right << "-b" << bottom;
        qDebug() << arguments;
    }else{
        ui->textBrowser->setText("Recognize Page or Area invalid parameter.");
        return;
    }

    process->start(program, arguments);

    if (!process->waitForStarted()) {
        ui->textBrowser->setText("Failed to start process.");
    }
}
