#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include "base_notify_dialog.h"
#include "deb_install_window.h"
#include "ui_deb_install_window.h"
#include "find_and_match_ppds.h"
#include "ukui_apt.h"
#include "size_para.h"

DebInstallWindow::DebInstallWindow(bool hasPPDSelectWin, QWidget*parentWin, QDialog *parent)
    : QDialog(parent)
    , m_ui(new Ui::DebInstallWindow)
    , m_parentWidget(parentWin)
    , m_hasPPDSelectWinFlag(hasPPDSelectWin)
{
    setParent(parentWin);
    setWindowModality(Qt::ApplicationModal);
    setWindowFlags(Qt::Tool);
    UKUI_WIN_CONTROL_PROTOOL(this->winId());
    m_ui->setupUi(this);
    m_ui->titleBar->setTitleName(tr("Modify Printer Driver"));
    m_ui->closeBtn->setShowBtnsMode(QuadBtnsShowMode::ONLY_CLOSE_BTN,this);
    m_ui->debNameLine->setReadOnly(true);
    m_ui->debNameLine->setPlaceholderText(tr("Choose A Deb, Or Forward Directly!"));
    m_ui->debNameLine->setAcceptDrops(true);
    connect(m_ui->chooseBtn,SIGNAL(released()),this,SLOT(onChooseBtnClicked()));
    connect(m_ui->cancelBtn,SIGNAL(released()),this,SLOT(onCancelBtnClicked()));
    connect(m_ui->forwardBtn,SIGNAL(released()),this,SLOT(onForwardBtnClicked()));
//    QPoint temp = QPoint(parentWindow->mapToGlobal(QPoint(0,0)).x() + parentWindow->width()/2,
//                         parentWindow->mapToGlobal(QPoint(0,0)).y() + parentWindow->height()/2);
//    this->move(temp - this->rect().center());
}

DebInstallWindow::~DebInstallWindow()
{
    delete m_ui;
}

void DebInstallWindow::onChooseBtnClicked()
{
    qDebug() << "onChooseBtnClicked!";
    // QString debName =
    //         QFileDialog::getOpenFileName(this, tr("Please select a deb package."), QDir::currentPath(), "Deb(*.deb)");
    QString debName = "";
    QFileDialog dialog(this,tr("Please select a deb package."));
    dialog.setFileMode(QFileDialog::AnyFile);
    dialog.setNameFilter(tr("Deb File(*.deb)"));
    dialog.setLabelText(QFileDialog::Accept, tr("Choose"));
    dialog.setLabelText(QFileDialog::Reject, tr("Cancel"));
    if(QDialog::Accepted == dialog.exec()){
      //  QStringList fileName = dialog.selectedFiles();
        QStringList names = dialog.selectedFiles();
        qDebug() << names;
        if(names.size()!=0) {
            debName = names.at(0);
        }
    }

    if(debName.compare("") != 0)
    {
        m_ui->debNameLine->setText(debName);
    }
}

void DebInstallWindow::onCancelBtnClicked()
{
    m_ui->debNameLine->clear();
    this->close();
}

void DebInstallWindow::onForwardBtnClicked()
{
    if (m_ui->debNameLine->text().compare("") != 0)
    {
        //安装这个包
        m_installOk = -1;

        QPoint windowCenter = QPoint(this->mapToGlobal(QPoint(0,0)).x() + this->width()/2,this->mapToGlobal(QPoint(0,0)).y() + this->height()/2);

        BaseNotifyDialog dialog(this);
        dialog.move(windowCenter - dialog.rect().center());

        dialog.dialogSetType(BaseNotifyDialogType::WAITTING);
        dialog.labelSetText(tr("Searching driver..."));
        connect(g_findAndMatchWorkThread, &FindAndMatchPPDs::updateFinished, &dialog, &QDialog::close);
        connect(g_findAndMatchWorkThread, &FindAndMatchPPDs::updateFailed, &dialog, &QDialog::close);
        connect(this, SIGNAL(installEnd()), &dialog, SLOT(close()));

        DebUtilHelper* debHelper = new DebUtilHelper(m_ui->debNameLine->text());
        connect(debHelper, &DebUtilHelper::succeed, [=]() {
            qDebug() << "Install succeed";
            m_installOk = 0;
            g_findAndMatchWorkThread->updatePPDsMap();
        });
        connect(debHelper, &DebUtilHelper::failed, [=]() {
            qDebug() << "Install failed";
            m_installOk = 1;
            emit installEnd();
        });
        debHelper->startWorker();
        dialog.exec();
        if (m_installOk == 1) {
            //弹窗
            QMessageBox Msg(QMessageBox::Warning, tr("Warning"), tr("Install package failed，please retry."), QMessageBox::NoButton);
            Msg.setParent(this);
            Msg.addButton(tr("Confirm"), QMessageBox::YesRole);
            Msg.exec();
            return;
        }
    }
    else
    {
        //弹窗
        QMessageBox Msg(QMessageBox::Warning, tr("Warning"), tr("No driver package selected,Continue?"), QMessageBox::NoButton);
        Msg.setParent(this);
        QPushButton *btnYes = Msg.addButton(tr("Confirm"), QMessageBox::YesRole);
        QPushButton *btnNo  = Msg.addButton(tr("Cancel"), QMessageBox::NoRole);
        Msg.exec();
        if ((QPushButton*)Msg.clickedButton() == btnNo)
        {
            return;
        }
    }
    if(m_hasPPDSelectWinFlag)
    {
        m_pSelectPpdDialog = new SelectPpdDialog(this);
        connect(m_pSelectPpdDialog,SIGNAL(chosenPPDName(QString)),this,SLOT(applySelectedPPD(QString)));
        connect(m_pSelectPpdDialog, SIGNAL(userCanceled()), this,SLOT(cancelSelectedPPD()));

        m_pSelectPpdDialog->onShow();
        m_pSelectPpdDialog->setFontSizeF(m_font14);
        m_pSelectPpdDialog->exec();
    }
    else
    {
        this->close();
    }
}

void DebInstallWindow::applySelectedPPD(QString ppdName)
{
    m_ui->debNameLine->clear();
    this->close();
    emit modifyPPDName(ppdName);
}

void DebInstallWindow::cancelSelectedPPD()
{
    m_ui->debNameLine->clear();
    this->close();
}

void DebInstallWindow::onShow()
{
    QPoint temp = QPoint(m_parentWidget->mapToGlobal(QPoint(0,0)).x() + m_parentWidget->width()/2,
                         m_parentWidget->mapToGlobal(QPoint(0,0)).y() + m_parentWidget->height()/2);
    this->move(temp - this->rect().center());
//    this->setBackgroundRole(QPalette::Base);
    if(!isActiveWindow())
    {
        hide();
        show();
    }
    show();
    raise();
    activateWindow();
}
void DebInstallWindow::setQSSFontSize(QFont curFont)
{
    m_font14 = curFont;
    m_font14.setPointSizeF(curFont.pointSizeF());
    if(m_pSelectPpdDialog != nullptr)
    {
        m_pSelectPpdDialog->setFontSizeF(m_font14);
    }
}
