#include "wprogrampublishitem.h"
#include "gutil/qgui.h"
#include "base/waitingdlg.h"
#include "gutil/qnetwork.h"
#include "deviceitem.h"
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QProgressBar>
#include "globaldefine.h"
#include <QInputDialog>
#include <QMetaEnum>

wProgramPublishItem::wProgramPublishItem(LedCard pLedCard, LoQTreeWidget *parent, QString strProgramName, QString strProgramPath) : QObject(parent), QTreeWidgetItem(UserType), m_parent(parent) {
    m_strProgramName = strProgramName;
    m_strProgramPath = strProgramPath;
    mLedCard = pLedCard;

    setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    setCheckState(0, Qt::Unchecked);
    m_parent->addTopLevelItem(this);
    m_ImageOnline = new QLabel();
    m_ImageOnline->setAlignment(Qt::AlignCenter);
    m_parent->setItemWidget(this, ENUM_DEVICE_PUBLISH_HEADE_ONLINE, m_ImageOnline);
    for(int i=1; i<ENUM_DEVICE_PUBLISH_HEADE_REMARKS; i++) setTextAlignment(i, Qt::AlignCenter);
    fdProgress = new QProgressBar();
    fdProgress->setStyleSheet("margin-top: 8px; margin-bottom: 8px;");
    m_parent->setItemWidget(this, ENUM_DEVICE_PUBLISH_HEADE_PROGRESS, fdProgress);
    fdProgress->setAlignment(Qt::AlignCenter);

    btnUnlock = new QPushButton;
    btnUnlock->setMaximumHeight(40);
    auto wgt = new QWidget;
    auto vBox = new VBox(wgt);
    vBox->setContentsMargins(0,0,0,0);
    vBox->addWidget(btnUnlock);
    m_parent->setItemWidget(this, ENUM_DEVICE_PUBLISH_HEADE_ENCRYPT, wgt);
    connect(btnUnlock, &QPushButton::clicked, this, [this] {
        if(! mLedCard.isLocked) return;
        bool ok;
        auto pwd = QInputDialog::getText(treeWidget(), tr("Input password"), tr("Input password"), QLineEdit::Password, QString(), &ok);
        if(! ok) return;
        QJsonObject json;
        json.insert("_type", "VerifyPassword");
        json.insert("_type", "VerifyPassword");
        json.insert("pwd", pwd);
        auto waitingDlg = new WaitingDlg(treeWidget(), tr("VerifyPassword")+" ...");
        waitingDlg->show();
        auto reply = NetReq("http://"+mLedCard.ip+":2016/settings").timeout(60000).post(json);
        waitingDlg->connAbort(reply);
        connect(reply, &QNetworkReply::finished, this, [=] {
            QJsonDocument json;
            QString err = checkReplyForJson(reply, &json);
            if(! err.isEmpty()) {
                waitingDlg->close();
                QMessageBox::critical(m_parent, QObject::tr("Error"), err);
                return;
            }
            if(! json["result"].toBool()) {
                waitingDlg->close();
                QMessageBox::warning(treeWidget(), tr("Tip Info"), tr("password is wrong"));
                return;
            }
            waitingDlg->success();
            mLedCard.isLocked = false;
            btnUnlock->setIcon(QIcon(":/res/device/UnLock.png"));
            auto item = findItem(mLedCard.id);
            if(item) {
                item->mCard.isLocked = false;
                item->btnUnlock->setIcon(QIcon(":/res/device/UnLock.png"));
            }
        });
    });

    SetItemParam(mLedCard);
}

void wProgramPublishItem::SetItemParam(LedCard card) {
    setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_ID, 0, card.id);
    setData(ENUM_DEVICE_PUBLISH_HEADE_REMARK_NAME, 0, card.alias);
    setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_IP, 0, card.ip);
    setData(ENUM_DEVICE_PUBLISH_HEADE_SCREEN_SIZE, 0, QString("%1 x %2").arg(card.mWidth).arg(card.mHeight));
    m_ImageOnline->setPixmap(QPixmap(mLedCard.isOnline ? ":/res/device/O_Online.png" : ":/res/device/O_Offline.png"));
    if(! card.hasPassword) btnUnlock->hide();
    else {
        if(! btnUnlock->isVisible()) btnUnlock->show();
        btnUnlock->setIcon(QIcon(card.isLocked ? ":/res/device/Lock.png" : ":/res/device/UnLock.png")); //如果已经验证通过密码显示绿色图标 没有验证显示蓝色锁图标
    }
}
