/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <QtConcurrent>
#include <QMutex>
#include <QDebug>
#include <QFile>
#include "ocr.h"
#include "kylinocr.h"

#define LOG_FLAG "OCR_MODULE#"

OCR::OCR() {}

OCR::~OCR()
{
    if (m_ocr) {
        delete m_ocr;
        m_ocr = nullptr;
    }
}

void OCR::setEngineType(EngineType type)
{
    if (m_ocr) {
        delete m_ocr;
        m_ocr = nullptr;
    }
    // TODO:目前因为子类中无私有属性，目前不会造成内存泄漏，所以先作为待办项
    switch (type) {
    case EngineType::KySDK:
        m_ocr = dynamic_cast<OCR *>(new KylinOCR());
        break;
    default:
        qWarning() << LOG_FLAG << "Invaild OCR engine type";
    }
}

void OCR::getText(QString imgPath)
{
    if (!QFile::exists(imgPath) || !m_ocr) {
        qWarning() << LOG_FLAG << "getText Failed!Check image path or set engine type first?";
        return;
    }

    connect(m_ocr, &OCR::recResult, this, &OCR::recResult);

    m_ocr->getText(imgPath);
}

// void OCR::getRect(QString imgPath)
//{
//    // 接口暂未启用
//    Q_UNUSED(imgPath);
//    return;
//}
