#include "deviceitem.h"
#include "devicepanel.h"
#include "main.h"
#include "gutil/qwaitingdlg.h"
#include "gutil/qgui.h"
#include "gutil/qnetwork.h"
#include <QMessageBox>
#include <QPainter>
#include <QInputDialog>

DeviceItem::DeviceItem(LoQTreeWidget *parent) : TreeWidgetItem(parent) {
    setFlags(flags() & ~Qt::ItemIsUserCheckable);
    setCheckState("check", Qt::Unchecked);
    auto ft = treeWidget()->font();
    ft.setPixelSize(14);
    for(int i=2; i<treeWidget()->columnCount()-2; i++) setFont(i, ft);

    auto btnGetCapture = new QPushButton;
    btnGetCapture->setCursor(QCursor(Qt::PointingHandCursor));
    btnGetCapture->setIcon(QIcon(":/res/deviceReadbackPic.png"));
    btnGetCapture->setIconSize({28, 28});
    QObject::connect(btnGetCapture, &QPushButton::clicked, btnGetCapture, [=] {
        JObj json;
        json.insert("_id", "GetScreenshotFull");
        json.insert("_type", "GetScreenshotFull");
        auto waitingDlg = new WaitingDlg(btnGetCapture, DevicePanel::tr("Getting ")+DevicePanel::tr("Screenshot")+" ...");
        waitingDlg->show();
        auto reply = NetReq("http://"+mCard.ip+":2016/settings").timeout(120000).post(json);
        ConnReply(reply, waitingDlg) [=] {
            waitingDlg->close();
            QJsonDocument json;
            QString err = checkReplyForJson(reply, &json);
            if(! err.isEmpty()) {
                QMessageBox::critical(treeWidget(), DevicePanel::tr("Error"), err);
                return;
            }
            ImgDlg dlg(QByteArray::fromBase64(json["data"].toString().toLatin1()), treeWidget());
            dlg.exec();
        });
    });
    setCellWidget("screenshot", btnGetCapture);

    fdOnline = new QLabel;
    fdOnline->setPixmap({":/res/online.png"});
    fdOnline->setScaledContents(true);
    fdOnline->setFixedSize(24, 24);
    auto wgt = new QWidget;
    auto hhh = new HBox(wgt);
    hhh->setContentsMargins(0,0,0,0);
    hhh->addWidget(fdOnline);
    setCellWidget("online", wgt);

    btnUnlock = new QPushButton;
    btnUnlock->setMaximumHeight(40);
    QObject::connect(btnUnlock, &QPushButton::clicked, btnUnlock, [this] {
        if(! mCard.isLocked) return;
        bool ok;
        auto pwd = QInputDialog::getText(treeWidget(), DevicePanel::tr("Input password"), DevicePanel::tr("Input password"), QLineEdit::Password, QString(), &ok);
        if(! ok) return;
        JObj json;
        json.insert("_id", "VerifyPassword");
        json.insert("_type", "VerifyPassword");
        json.insert("pwd", pwd);
        auto waitingDlg = new WaitingDlg(btnUnlock, DevicePanel::tr("VerifyPassword")+" ...");
        waitingDlg->show();
        auto reply = NetReq("http://"+mCard.ip+":2016/settings").timeout(60000).post(json);
        ConnReply(reply, waitingDlg) [=] {
            QJsonDocument json;
            QString err = checkReplyForJson(reply, &json);
            if(! err.isEmpty()) {
                waitingDlg->close();
                QMessageBox::critical(treeWidget(), DevicePanel::tr("Error"), err);
                return;
            }
            if(! json["result"].toBool()) {
                waitingDlg->close();
                QMessageBox::critical(treeWidget(), DevicePanel::tr("Tip Info"), DevicePanel::tr("password is wrong"));
                return;
            }
            waitingDlg->success();
            mCard.isLocked = false;
            btnUnlock->setIcon(QIcon(":/res/UnLock.png"));
        });
    });
    wgt = new QWidget;
    hhh = new HBox(wgt);
    hhh->setContentsMargins(0,0,0,0);
    hhh->addWidget(btnUnlock);
    setCellWidget("password", wgt);
}

void DeviceItem::init() {
    setText("id", mCard.id);
    setText("ip", mCard.ip);
    setText("screenSize", QString("%1 x %2").arg(mCard.mWidth).arg(mCard.mHeight));
    if(mCard.hasPassword) btnUnlock->setIcon(QIcon(mCard.isLocked ? ":/res/Lock.png" : ":/res/UnLock.png"));
    else btnUnlock->hide();
}