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

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

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

    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();

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

        fdVolume = new QSlider(Qt::Horizontal);
        fdVolume->setTickPosition(QSlider::TicksAbove);
        fdVolume->setRange(0, 15);
        connect(fdVolume, &QSlider::sliderReleased, this, [=] {
            if(gSelCards->isEmpty()) {
                QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
                return;
            }
            QJsonObject json;
            json.insert("_id", "SetVolume");
            json.insert("_type", "SetVolume");
            json.insert("volume", fdVolume->value());
            if(gSelCards->count() == 1) {
                auto waitingDlg = new WaitingDlg(this, tr("SetVolume")+" ...");
                Def_CtrlReqPre
                connect(reply, &QNetworkReply::finished, this, [reply, waitingDlg] {
                    Def_CtrlSetReqAfter
                });
            } else {
                foreach(auto card, *gSelCards) {
                    Def_CtrlSetMulti(tr("SetVolume"))
                }
            }
        });
        hBox->addWidget(fdVolume);

        auto lbCurVol = new QLabel;
        lbCurVol->setMinimumWidth(30);
        connect(fdVolume, &QSlider::valueChanged, lbCurVol, (void(QLabel::*)(int))&QLabel::setNum);
        hBox->addWidget(lbCurVol);
        hBox->addStretch();

        fdVolumeGet = new QPushButton;
        fdVolumeGet->setMinimumSize(QSize(60, 30));
        fdVolumeGet->setProperty("ssType", "progManageTool");
        connect(fdVolumeGet, &QPushButton::clicked, this, [=] {
            if(gSelCards->isEmpty()) {
                QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
                return;
            }
            QJsonObject json;
            json.insert("_id", "GetVolume");
            json.insert("_type", "GetVolume");
            if(gSelCards->count() == 1) {
                auto waitingDlg = new WaitingDlg(this, tr("GetVolume")+" ...");
                Def_CtrlReqPre
                connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                    Def_CtrlSingleGetReply
                    waitingDlg->success();
                    fdVolume->setValue(json["volume"].toInt());
                });
            } 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 = QString::number(json["volume"].toInt());
                        gFdResInfo->append(cardId+" "+tr("GetVolume")+" "+err);
                    });
                }
            }
        });
        vBox->addWidget(fdVolumeGet, 0, Qt::AlignCenter);
        vBox->addStretch();
    }
    m_pSchedule = new ControlVolumeSchedule(this, list);
    connect(m_pSchedule, &ControlVolumeSchedule::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 ControlVolumeWidget::showEvent(QShowEvent *event) {
    QWidget::showEvent(event);
    init();
}
void ControlVolumeWidget::init() {
    bool isSingle = gSelCards->count()==1;
    if(! isSingle) return;
    auto card = gSelCards->at(0);

    QJsonObject json;
    json.insert("_id", "GetVolume");
    json.insert("_type", "GetVolume");
    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;
        fdVolume->setValue(json["volume"].toInt());
    });
}

void ControlVolumeWidget::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void ControlVolumeWidget::transUi() {
    lbVolumeControl->setText(tr("Volume Control"));
    fdManual->setText(tr("Manual"));
    fdSchedule->setText(tr("Schedule"));

    lbVolume->setText(tr("Volume"));
    fdVolumeGet->setText(tr("Readback"));

    m_pSchedule->refreshLable();
}
