#include <QString>
#include <QStringList>
#include <QDataStream>

#include "launch_printer.h"
#include "device_monitor.h"
#include "common.h"
#include "device_map.h"
#include "global_variable.h"

LaunchPrinter::LaunchPrinter(/* args */)
{

}

LaunchPrinter::~LaunchPrinter()
{

}

void LaunchPrinter::add(const QString &path)
{
    qDebug() << APP_PATH<<"add" << path;
    DeviceMap::getInstance()->checkPathAdd(devicePathCheck(path));
    // runCommand(QStringList{APP_PATH, "add", path});
}

void LaunchPrinter::remove(const QString &path)
{
    qDebug() <<  APP_PATH<<"remove" << path;
    DeviceMap::getInstance()->checkPathRemove(devicePathCheck(path));
    // runCommand(QStringList{APP_PATH, "remove", path});
}

void LaunchPrinter::add_uri(const QString &path)
{
    qDebug() <<  APP_PATH<<"add_uri" << path;
    // runCommand(QStringList{APP_PATH, "add_uri", path});
}

bool checkUriExist(const DeviceInformation &test,
                   const QList<DeviceInformation> &res)
{
    for (int i=0 ;i<res.count();i++)
    {
        if(test.uri == res.at(i).uri)
        {
            qDebug()<<"这个打印机已经安装过了!"<<test.uri;
            //isInstallPop();  //每次开机会弹窗提示有打印机已经安装过
            return true;
        }
    }
    return false;
}
void LaunchPrinter::findPrePrinter()
{
    QList<DeviceInformation> lis = DeviceMonitor::getAllPrinterConnected();
    QList<DeviceInformation> res = DeviceMonitor::getAllPrinterWithPDD(true);
    // DeviceInformation device = DeviceInformation("test");
    // qDebug () << device;

    // 如果之前安装过了， 就忽略
    for (int i = 0; i < lis.size(); i++) {
        if (checkUriExist(lis.at(i), res) == false)
            add_uri(lis.at(i).uri);
    }
}