#include "printerthreadhandle.h"
#include "globalinfo.h"
#include "handle/printer/boxtemlateone.h"

PrinterThreadHandle::PrinterThreadHandle(QObject *parent) : QObject(parent),manger(nullptr),thread(nullptr)
{
}

void PrinterThreadHandle::updateConfig()
{
    if(manger == nullptr) return;
    auto config = GlobalInfo::this_()->config();
    manger->setPrinterName(config->printerName);
}

void PrinterThreadHandle::start()
{
    if(thread == nullptr){
        thread = new QThread(this);
        connect(thread,&QThread::started,this,&PrinterThreadHandle::updateConfig);
        manger = new PrinterManger(this);
        connect(this,&PrinterThreadHandle::printerBox,manger,&PrinterManger::printerBox,Qt::QueuedConnection);
        connect(this,&PrinterThreadHandle::printerBox1,manger,&PrinterManger::printerBox1,Qt::QueuedConnection);
        connect(this,&PrinterThreadHandle::printerBoxModel,manger,&PrinterManger::printerBoxModel,Qt::QueuedConnection);
        connect(this,&PrinterThreadHandle::printerPack,manger,&PrinterManger::printerPack,Qt::QueuedConnection);
        connect(this,&PrinterThreadHandle::printerPackModel,manger,&PrinterManger::printerPackModel,Qt::QueuedConnection);
        auto config = GlobalInfo::this_()->config();
        connect(config,&ConfigInfo::configUpdate,this,&PrinterThreadHandle::updateConfig,Qt::QueuedConnection);
        manger->setPrinter(QSharedPointer<BaseBoxPrinter>(new BoxTemlateOne()),QSharedPointer<BasePackinfoPrinter>(new PackTemlateOne()));
        this->moveToThread(thread);
        thread->start();
    }
}

