#include "usbdetectdialog.h"
#include "ui_usbdetectdialog.h"
#include <windows.h>
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QColor>
#include "LoUIClass/x_uimsgboxok.h"
UsbDetectDialog::UsbDetectDialog(QWidget *parent) :
    LoQDialog(parent),
    ui(new Ui::UsbDetectDialog)
{
    ui->setupUi(this);
    ui->lineEditPassword->setEchoMode(QLineEdit::Password);
    ui->lineEditPassword->setPlaceholderText(tr("Input password"));
    QFileInfoList list =  QDir::drives();  //获取当前系统的盘符
    driver_number=list.count();
    m_pTimer = new QTimer(this);
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(detectDriver()));
    connect(ui->bnOk, SIGNAL(clicked()), this, SLOT(onAccepted()));
    m_pTimer->start(3000);
    detectDriver();
}

UsbDetectDialog::~UsbDetectDialog()
{
    delete ui;
}
void UsbDetectDialog::detectDriver()
{
    QFileInfoList list =  QDir::drives();  //获取当前系统的盘符
    int temp_number=0;
    QStringList strTempListUsb;
    std::vector<UINT> driver_types;
    for(int i=0;i<list.count();++i)
    {
        UINT driver_type = GetDriveType((WCHAR *) list[i].filePath().utf16());
        driver_types.push_back(driver_type);
        switch (driver_type) {
        case 0:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 驱动类型不能确定";
            break;
        case 1:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 根路径无效";
            break;
        case 2:
            temp_number++;
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 可移动驱动器：软盘驱动器，拇指驱动器或闪存卡读取器";
            strTempListUsb.append(list[i].filePath());
            break;
        case 3:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 固定驱动器：硬盘驱动器或闪存驱动器";
            break;
        case 4:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" 远程（网络）驱动器";
            break;
        case 5:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" CD-ROM驱动器";
            break;
        case 6:
            //qDebug()<<list[i].filePath()<<" "<<driver_type<<" RAM磁盘";
            break;
        default:
            break;
        }
    }
    if(temp_number>removable_number)
    {
        qDebug()<<"驱动器数量： "<<list.count();
        qDebug()<<"插入可移动驱动器！！！";
        for(int m=0;m<strTempListUsb.count();m++)
        {
            QString strA=strTempListUsb.at(m);
            bool bExist=false;
            for (int n=0;n<ui->listWidgetUsb->count();n++) {

                   QString strB=ui->listWidgetUsb->item(n)->text();
                   if(strA==strB)
                   {
                       bExist=true;
                       break;
                   }
            }
            if(!bExist)
                ui->listWidgetUsb->addItem(strA);
        }

    }
    else if(temp_number==removable_number)
    {
        //qDebug()<<"未插入/移除可移动驱动器！！！";
    }
    else if(temp_number<removable_number)
    {
        qDebug()<<"驱动器数量： "<<list.count();
        qDebug()<<"移除可移动驱动器！！！";

//        for(int m=0;m<ui->listWidgetUsb->count();m++)
//        {
//            QString strA=ui->listWidgetUsb->item(m)->text();
//            bool iExist=false;
//            for(int n=0;n<strTempListUsb.count();n++)
//            {
//                QString strB=strTempListUsb.at(n);
//                if(strB==strA)
//                {
//                    iExist=true;
//                    break;
//                }
//            }
//            if(!iExist)
//            {
//                ui->listWidgetUsb->removeItemWidget(ui->listWidgetUsb->item(m));
//                m--;
//            }
//        }
        ui->listWidgetUsb->clear();
        ui->listWidgetUsb->addItems(strTempListUsb);
    }
    removable_number=temp_number;

    driver_number=list.count();
    ui->listWidgetUsb->setProperty("ssType", "usbList");
    ui->listWidgetUsb->setProperty("ssName", "usbListName");
    if(removable_number==1)
    {
        QListWidgetItem *item = ui->listWidgetUsb->item(0);
        item->setSelected(true);
    }


}
void UsbDetectDialog::onAccepted()
{
    //    QList * aa=
    QList<QListWidgetItem *>  aa=ui->listWidgetUsb->selectedItems();
    int iSelectCount=aa.count();
    if(iSelectCount>0)
    {
        for (int i=0;i<aa.count();i++) {

            QString strUsbName=aa.at(i)->text();
            emit sigAcceptData(strUsbName,ui->lineEditPassword->text());

        }

    }
    else {
        if(ui->listWidgetUsb->count()<=0)
        {
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("No checked USB device"),this,1);
            pDlg->exec();
            return;
        }
        else {
            X_UIMsgBoxOk *pDlg=new X_UIMsgBoxOk(tr("Tip"),tr("please select usb device in list"),this,1);
            pDlg->exec();
            return;
        }

    }
    close();
}
