#include "controlhdmiwidget.h"
#include "gqt.h"
#include "tools.h"
#include "globaldefine.h"
#include "base/waitingdlg.h"
#include <QMessageBox>
#include <QButtonGroup>

ControlHdmiWidget::ControlHdmiWidget(QWidget *parent,QList<LedCard *> *list) : QWidget(parent) {
    auto vBox = new QVBoxLayout(this);
    labelHdmiConfiguration = new QLabel;
    labelHdmiConfiguration->setAlignment(Qt::AlignCenter);
    vBox->addWidget(labelHdmiConfiguration);

    auto line = new QFrame;
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    vBox->addWidget(line);

    auto hBox = new QHBoxLayout();
    hBox->addStretch();

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

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

    vBox->addLayout(hBox);

    m_pStackedW = new QStackedWidget;
    m_pManual = new QWidget;
    {
        auto vBox = new VBox(m_pManual);
        auto hBox = new HBox(vBox);
        hBox->addStretch();

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

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

        lbSync = new QLabel;
        hBox->addWidget(lbSync);
        hBox->addStretch();

        vBox->addSpacing(20);

        hBox = new HBox(vBox);
        hBox->addStretch();
        btnGetSync = new QPushButton;
        btnGetSync->setMinimumSize(60, 30);
        btnGetSync->setProperty("ssType", "progManageTool");
        connect(btnGetSync, &QPushButton::clicked, this, [this] {
            if(gSelCards->isEmpty()) {
                QMessageBox::information(gMainWin, tr("Tip"), tr("NoSelectedController"));
                return;
            }
            QJsonObject json;
            json.insert("_id", "IsSync");
            json.insert("_type", "IsSync");
            if(gSelCards->count() == 1) {
                auto waitingDlg = new WaitingDlg(this, tr("IsSync"));
                Def_CtrlReqPre
                connect(reply, &QNetworkReply::finished, this, [this, reply, waitingDlg] {
                    Def_CtrlSingleGetReply
                    waitingDlg->success();
                    qDebug()<<"IsSync res"<<json;
                    swich->setCheckedStatus(json["result"].toBool());
                });
            } else {
                foreach(auto card, *gSelCards) {
                    auto reply = Tools::netManager().post(reqForJson("http://"+card->m_strCardIp+":2016/settings"), QJsonDocument{json}.toJson(QJsonDocument::Compact));
                    connect(reply, &QNetworkReply::finished, this, [reply, card] {
                        QJsonDocument json;
                        QString err = checkReplyForJson(reply, &json);
                        if(err.isEmpty()) err = json["result"].toBool() ? "true" : "false";
                        gFdResInfo->append(card->m_strCardId+" "+tr("IsSync")+" "+err);
                    });
                }
            }
        });
        hBox->addWidget(btnGetSync);
        hBox->addStretch();

        vBox->addStretch();
    }
    m_pSchedule = new ControlHdmiSchedule(this, list);
    m_pStackedW->addWidget(m_pManual);
    m_pStackedW->addWidget(m_pSchedule);
    vBox->addWidget(m_pStackedW);

    auto m_buttonGroup = new QButtonGroup;
    m_buttonGroup->addButton(radioButtonManual, 0);
    m_buttonGroup->addButton(radioButtonSchedule, 1);
    connect(m_buttonGroup,SIGNAL(buttonClicked(int)),m_pStackedW,SLOT(setCurrentIndex(int)));
    radioButtonManual->setChecked(true);
    m_pStackedW->setCurrentIndex(0);

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

void ControlHdmiWidget::showEvent(QShowEvent *event) {
    QWidget::showEvent(event);
    init();
}

void ControlHdmiWidget::init() {
    bool isSingle = gSelCards->count()==1;
    if(! isSingle) return;
    QJsonObject json;
    json.insert("_id", "IsSync");
    json.insert("_type", "IsSync");
    auto card = gSelCards->at(0);
    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()) {
            QMessageBox::critical(gMainWin, tr("Error"), err);
            return;
        }
        swich->setCheckedStatus(json["result"].toBool());
    });
}
void ControlHdmiWidget::changeEvent(QEvent *event) {
    QWidget::changeEvent(event);
    if(event->type() == QEvent::LanguageChange) transUi();
}
void ControlHdmiWidget::transUi() {
    labelHdmiConfiguration->setText(tr("HDMI Configuration"));
    radioButtonManual->setText(tr("Manual"));
    radioButtonSchedule->setText(tr("Schedule"));

    lbSync->setText(tr("HDMI IN"));
    lbAsync->setText(tr("Async"));
    btnGetSync->setText(tr("Readback"));
}
