#include "orderprintwork.h"

#include "preDefine.h"

#include "event/fmapplication.h"
#include "event/posevent.h"

#include "model/orderObject.h"
#include "model/posorderpool.h"

#include "base/Print/printlib.h"

#include "QsLog.h"

#include <QFile>
#include <QByteArray>

#include <QSettings>

orderprintwork::orderprintwork(QObject *parent) : QObject(parent)
{
    //订阅订单本地化事件
    FMApplication::subscibeEvent(this, PosEvent::s_order_location);
}

bool orderprintwork::event(QEvent *e)
{
    if(e->type() == PosEvent::s_order_location)
    {
        QVariantMap map;

        GETEVENTINFO(map, e, QVariantMap);

        if(map.contains(EVENT_KEY_ORDERID) && map.contains(EVENT_KEY_MSG) &&
                map[EVENT_KEY_MSG].toInt() == DEFAULT_LOCATION_PRINT)
        {

            QVariantMap smap;
            smap.insert(EVENT_KEY_ORDERSTATUS, 0);
            smap.insert(EVENT_KEY_MSG, QString::fromLocal8Bit("打印中..."));
            POSTEVENTTYPE(PosEvent::s_opt_status,smap,QVariantMap);

            OrderObject order;

            PosOrderPool::GetOrderObject(map[EVENT_KEY_ORDERID].toString(), order);

            QLOG_INFO() << "begin print ...";

            QString error;
            QString content;

            //QFile file(qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET);
            QFile file;

            QString prtinifile = qApp->applicationDirPath() + "/" + PREINTER_CONSS;

            int prtwi = QSettings(prtinifile, QSettings::IniFormat).value("Printer/PaperWidth", 0).toInt();

            if(order.order_type == 1 || order.order_type == 2)
            {
                if(prtwi == 56)
                    file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINTTAKE_TEMPLET);
                else
                    file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINTTAKE80_TEMPLET);
            }
            else
            {
                if(prtwi == 56)
                    file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET);
                else
                    file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINT80_TEMPLET);
            }

            qDebug() << "+++++++++++++++++++++++++++" << file.fileName();

            if(!file.open(QFile::ReadOnly))
            {
                QVariantMap tmpmap;

                tmpmap.insert(EVENT_KEY_STATUS, false);
                tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit(" 打印失败")));

                POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);

                QLOG_ERROR() << "open " << qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET << " failed";
                return true;
            }

            content = QString::fromUtf8(file.readAll());
            file.close();

            qDebug() << content;
            {
                PrintLib print;

                if(print.DoPrint(content, &order, error))
                {
                    QLOG_INFO() << "print succcess";

                    QVariantMap tmpmap;

                    tmpmap.insert(EVENT_KEY_STATUS, true);
                    tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit("打印成功")));

                    POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);

                    QVariantMap map;

                    map.insert(EVENT_KEY_ORDERID, order.order_id);
                    map.insert(EVENT_KEY_MSG, DEFAULT_LOCATION_PRINT);
                    map.insert(EVENT_KEY_STATUS, 1);

                    POSTEVENTTYPE(PosEvent::s_location_orderpool,map,QVariantMap);
                }
                else
                {
                    QLOG_INFO() << "print failed : " << error;

                    QVariantMap tmpmap;

                    tmpmap.insert(EVENT_KEY_STATUS, false);
                    tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit(" 打印失败")));

                    POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);

                }
            }
            QString otherfile = qApp->applicationDirPath() + "/" + CONFIG_NAME;

            int needptr = QSettings(otherfile, QSettings::IniFormat).value(INI_NEEDPTR, 0).toInt();


            QEventLoop loop;
            QTimer timer;

            connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);

            timer.start(2*1000);

            loop.exec();

            if(needptr == 1)
            {
                PrintLib otherprint;

                QVariantMap smap;
                smap.insert(EVENT_KEY_ORDERSTATUS, 0);
                smap.insert(EVENT_KEY_MSG, QString::fromLocal8Bit("打印中..."));
                POSTEVENTTYPE(PosEvent::s_opt_status,smap,QVariantMap);

                QString error;
                QString contents;

                //QFile file(qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET);
                QFile file;

                int othprtw = QSettings(prtinifile, QSettings::IniFormat).value("OtherPrinter/PaperWidth", 0).toInt();

                if(order.order_type == 1 || order.order_type == 2)
                {
                    if(othprtw == 56)
                        file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINTTAKE_TEMPLET);
                    else
                        file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINTTAKE80_TEMPLET);
                }
                else
                {
                    if(othprtw == 56)
                        file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET);
                    else
                        file.setFileName(qApp->applicationDirPath() + "/" + FILE_PRINT80_TEMPLET);
                }

                qDebug() << "+++++++++++++++++++++++++++" << file.fileName();

                if(!file.open(QFile::ReadOnly))
                {
                    QVariantMap tmpmap;

                    tmpmap.insert(EVENT_KEY_STATUS, false);
                    tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit(" 打印失败")));

                    POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);

                    QLOG_ERROR() << "open " << qApp->applicationDirPath() + "/" + FILE_PRINT_TEMPLET << " failed";
                    return true;
                }

                contents = QString::fromUtf8(file.readAll());
                file.close();

                if(otherprint.OnlyDoPrint(contents, &order, error))
                {
                    QLOG_INFO() << "print succcess";

                    QVariantMap tmpmap;

                    tmpmap.insert(EVENT_KEY_STATUS, true);
                    tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit("打印成功")));

                    POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);
                }
                else
                {
                    QLOG_INFO() << "print failed : " << error;

                    QVariantMap tmpmap;

                    tmpmap.insert(EVENT_KEY_STATUS, false);
                    tmpmap.insert(EVENT_KEY_MSG, QString(order.order_id).append(QString::fromLocal8Bit(" 打印失败")));

                    POSTEVENTTYPE(PosEvent::s_opt_status,tmpmap,QVariantMap);
                }
            }
        }
        return true;
    }

    return QObject::event(e);
}

