#include "controlpowerwidget.h"
#include "gutil/qgui.h"
#include "globaldefine.h"
#include "base/waitingdlg.h"
#include "tools.h"
#include <QMessageBox>

ControlPowerWidget::ControlPowerWidget(QWidget *parent, QList<LedCard *> *list) : QWidget(parent) {
    auto vBox = new VBox(this);

    lbScreenCfg = new QLabel;
    lbScreenCfg->setAlignment(Qt::AlignCenter);
    vBox->addWidget(lbScreenCfg);

    auto hBox = new HBox(vBox);
    hBox->addStretch();

    fdManual = new QRadioButton;
    hBox->addWidget(fdManual);
    hBox->addSpacing(40);

    fdSchedule = new QRadioButton;
    hBox->addWidget(fdSchedule);
    hBox->addStretch();

    auto stack = new QStackedLayout(vBox);
    {
        auto vBox = new VBox(stack);
        vBox->addSpacing(20);

        auto hBox = new HBox(vBox);
        hBox->addStretch();

        lbScreen = new QLabel;
        hBox->addWidget(lbScreen);

        fdScreen = new SwitchControl;
        fdScreen->setMinimumSize(QSize(80, 33));
        fdScreen->setSliderColor(QColor(0, 0, 0), QColor(0, 160, 230));
        fdScreen->setBgColor(QColor(200,200,200), QColor(0x00cc00));
        fdScreen->setTextColor(QColor(100,100,100), QColor(0, 160, 230));
        connect(fdScreen, &SwitchControl::checkedChanged, this, [this](bool checked) {
            if(gSelCards->isEmpty()) {
                QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
                return;
            }
            QJsonObject json;
            json.insert("_id", "SetScreenOn");
            json.insert("_type", "SetScreenOn");
            json.insert("on", checked);
            if(gSelCards->count() == 1) {
                auto waitingDlg = new WaitingDlg(this, (checked ? tr("SetScreenOn") : tr("SetScreenOff"))+" ...");
                Def_CtrlReqPre
                connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                    Def_CtrlSetReqAfter
                });
            } else {
                if(checked) foreach(auto card, *gSelCards) {
                    Def_CtrlSetMulti(tr("SetScreenOn"))
                }
                else foreach(auto card, *gSelCards) {
                    Def_CtrlSetMulti(tr("SetScreenOff"))
                }
            }
        });
        hBox->addWidget(fdScreen);
        hBox->addStretch();

        btnScreenGet = new QPushButton;
        btnScreenGet->setMinimumSize(QSize(60, 30));
        btnScreenGet->setProperty("ssType", "progManageTool");
        connect(btnScreenGet, &QPushButton::clicked, this, [this] {
            if(gSelCards->isEmpty()) {
                QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
                return;
            }
            QJsonObject json;
            json.insert("_id", "IsScreenOn");
            json.insert("_type", "IsScreenOn");
            if(gSelCards->count() == 1) {
                auto waitingDlg = new WaitingDlg(this, tr("IsScreenOn")+" ...");
                Def_CtrlReqPre
                connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                    Def_CtrlSingleGetReply
                    waitingDlg->success();
                    fdScreen->setCheckedStatus(json["on"].toBool());
                });
            } else {
                foreach(auto card, *gSelCards) {
                    auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                    auto cardId = card->m_strCardId;
                    connect(reply, &QNetworkReply::finished, this, [reply, cardId] {
                        QJsonDocument json;
                        QString err = checkReplyForJson(reply, &json);
                        if(err.isEmpty()) err = json["on"].toBool() ? tr("On") : tr("Off");
                        gFdResInfo->append(cardId+" "+tr("IsScreenOn")+" "+err);
                    });
                }
            }
        });
        vBox->addWidget(btnScreenGet, 0, Qt::AlignCenter);
        vBox->addStretch();
    }
    m_pSchedule = new ControlPowerSchedule(this, list);
    connect(m_pSchedule, &ControlPowerSchedule::sigHaveSchedule, this, [=](bool b) {
        if(b) fdSchedule->setChecked(true);
        else fdManual->setChecked(true);
    });
    stack->addWidget(m_pSchedule);

    connect(fdSchedule, &QRadioButton::toggled, stack, &QStackedLayout::setCurrentIndex);
    fdManual->setChecked(true);

    connect(gDevicePanel, &DevicePanel::sigSelectedDeviceList, this, [this] {
        if(isVisible()) init();
    });
    transUi();
}

void ControlPowerWidget::showEvent(QShowEvent *event) {
    QWidget::showEvent(event);
    init();
}
void ControlPowerWidget::init() {
    bool isSingle = gSelCards->count()==1;
    if(! isSingle) return;
    auto card = gSelCards->at(0);

    QJsonObject json;
    json.insert("_id", "IsScreenOn");
    json.insert("_type", "IsScreenOn");
    auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
    connect(reply, &QNetworkReply::finished, this, [this, reply] {
        QJsonDocument json;
        QString err = checkReplyForJson(reply, &json);
        if(! err.isEmpty()) return;
        fdScreen->setCheckedStatus(json["on"].toBool());
    });
}

void ControlPowerWidget::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void ControlPowerWidget::transUi() {
    lbScreenCfg->setText(tr("Power Configuration"));
    fdManual->setText(tr("Manual"));
    fdSchedule->setText(tr("Schedule"));

    lbScreen->setText(tr("Power"));
    fdScreen->setText(tr("Off"), tr("On"));
    btnScreenGet->setText(tr("Readback"));

    m_pSchedule->refreshLable();
}
