#include "threadcontroller.h"
#include "worker.h"

ThreadController::ThreadController(QObject *parent) : QObject(parent)
{
    Worker *worker = new Worker;
    worker->moveToThread(&this->workerThread);
    this->workerThread.setObjectName("workerThread");
    connect(&this->workerThread,SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(this, SIGNAL(finished()), &this->workerThread, SLOT(quit()));
    connect(this, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(this, SIGNAL(operate(const QString&)), worker, SLOT(doParallelAlgorithm(const QString&)));
    connect(worker, SIGNAL(resultReady(const QString&)), this, SLOT(handleResults(const QString&)));
    connect(&this->workerThread, SIGNAL(finished()), this, SLOT(closeApplication()));
    this->workerThread.start();
    emit operate("doParallel Multitrheading");
}


ThreadController::~ThreadController()
{

}

void ThreadController::handleResults(const QString& parameter)
{
    qDebug() << "Handling results";
    qDebug() << parameter;
    emit finished();
}

void ThreadController::closeApplication()
{
  QCoreApplication::quit();
}
