#include "ocrscene.h"
#include "ui_ocrscene.h"

#include <QFileDialog>
#include <QMessageBox>
#include <outputdialog.h>

#include <image/checkimage.h>

#include <check/check.h>

OCRScene::OCRScene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::OCRScene) {
    ui->setupUi(this);

    auto modules = parser.get_modules_names();

    for (auto &module : modules) {
        ui->store_combo_box->addItem(QString::fromStdString(module));
    }
}

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

void OCRScene::on_parse_button_clicked() {
    std::wstring checkContent = ui->check_text_edit->toPlainText().toStdWString();

    parser.set_module(parser.search_modules()[ui->store_combo_box->currentIndex()]);

    std::vector<Goods> goods = parser.parse(checkContent);
    if (goods.size() == 0) {
        QMessageBox infoDialog;
        infoDialog.setText(tr("An error has occured. Check was matched incorrectly. Vector sizes are different. Please, contact the developer."));
        infoDialog.setIcon(QMessageBox::Critical);
        infoDialog.setWindowTitle(tr("Error in parsing"));
        infoDialog.exec();
        return;
    }

    Check check;
    check.add_goods(goods);

    OutputDialog d(this, check);
    d.show();
    d.exec();
}

void OCRScene::on_choose_image_button_clicked() {
    QString filename = QFileDialog::getOpenFileName();

    if (filename == "") {
        QMessageBox infoDialog;
        infoDialog.setText(tr("Please, select a picture to scan"));
        infoDialog.setIcon(QMessageBox::Critical);
        infoDialog.setWindowTitle(tr("Picture was not selected"));
        infoDialog.exec();
        return;
    }

    std::string new_text = "Selected: " + filename.toStdString();
    ui->path_to_image_label->setText(tr("Path to image: ")+ filename);

    CheckImage i(filename.toStdString());
    std::string parsed = i.parse_text();

    ui->check_text_edit->setPlainText(QString::fromStdString(parsed));


}