#include "usbdetectdialog.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QLabel>
#include <QDir>
#include <QMessageBox>
#include <QTimerEvent>

UsbDetectDialog::UsbDetectDialog(QWidget *parent) : BaseDlg(parent) {
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowTitle(tr("Usb upgrade program"));
    resize(240, 220);
    auto vBox = new QVBoxLayout(this);

    auto hBox = new QHBoxLayout();
    hBox->addStretch();
    auto bnClose = new QPushButton("X");
    bnClose->setProperty("ssType","progManageTool");
    bnClose->setFixedWidth(32);
    connect(bnClose, &QPushButton::clicked, this, &BaseDlg::close);
    hBox->addWidget(bnClose);

    vBox->addLayout(hBox);

    hBox = new QHBoxLayout();
    hBox->addWidget(new QLabel(tr("Password")));

    fdPassword = new QLineEdit();
    fdPassword->setEchoMode(QLineEdit::Password);
    fdPassword->setPlaceholderText(tr("Input password"));
    hBox->addWidget(fdPassword);

    vBox->addLayout(hBox);

    fdDrives = new QListWidget();
    fdDrives->setSelectionRectVisible(true);
    fdDrives->setProperty("ssType", "usbList");
    fdDrives->setProperty("ssName", "usbListName");
    vBox->addWidget(fdDrives);

    hBox = new QHBoxLayout();
    hBox->addStretch();
    auto bnOk = new QPushButton(tr("OK"));
    bnOk->setProperty("ssType","progManageTool");
    connect(bnOk, &QPushButton::clicked, this, [this] {
        auto selects = fdDrives->selectedItems();
        if(selects.count() > 0) {
            for(auto select : selects) emit acceptData(select->text(), fdPassword->text());
            accept();
            return;
        }
        if(fdDrives->count() <= 0) QMessageBox::warning(this, tr("Tip"),tr("No checked USB device"));
        else QMessageBox::warning(this, tr("Tip"),tr("please select usb device in list"));
    });
    hBox->addWidget(bnOk);
    hBox->addStretch();
    auto bnCancel = new QPushButton(tr("Cancel"));
    bnCancel->setProperty("ssType","progManageTool");
    connect(bnCancel, &QPushButton::clicked, this, &BaseDlg::reject);
    hBox->addWidget(bnCancel);
    hBox->addStretch();
    vBox->addLayout(hBox);

    detectDriver();
}

void UsbDetectDialog::detectDriver() {
    auto drives = QDir::drives();  //获取当前系统的盘符
    fdDrives->clear();
    foreach(auto drive, drives) fdDrives->addItem(drive.filePath());
}
