//
// Created by Krad on 2021/12/16.
//

#include "TopBarWidget.h"
#include <QLabel>
#include <QHBoxLayout>
#include "components/RollingMessageWidget.h"
#include <QDateTime>
#include "event/EventCenter.h"
#include "errorhandle/GUIErrorLW.h"
#include "json/jsonobject.h"

TopBarWidget::TopBarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setMargin(0);
    QLabel * logo = new QLabel(this);
    QPixmap img(":/icons/logo.png");
    logo->setPixmap(img.scaledToHeight(33, Qt::SmoothTransformation));
    logo->setObjectName("logo");
    layout->addWidget(logo);
    QLabel * company = new QLabel(this);
    company->setObjectName("company");
    layout->addWidget(company);
    company->setText(tr("浙江衡玖医疗科技"));
    QWidget* spacerLine0 = new QWidget(this);
    spacerLine0->setFixedWidth(2);
    spacerLine0->setObjectName("spacer_2");
    layout->addWidget(spacerLine0);
    QLabel * lbl_msglogo = new QLabel(this);
    lbl_msglogo->setObjectName("lbl_msglogo");
    lbl_msglogo->setPixmap(QPixmap(":/icons/msg.png").scaledToHeight(26, Qt::SmoothTransformation));
    layout->addWidget(lbl_msglogo);

    QWidget* widgetMsg = new QWidget(this);
    layout->addWidget(widgetMsg,1);
//    widgetMsg->setObjectName("need_border");
    widgetMsg->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
    layout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding));
    QWidget* statusBarWidget = new QWidget(this);
    layout->addWidget(statusBarWidget);
    QLabel* hosp = new QLabel(this);
    hosp->setText(JsonObject::Instance()->institutionName());
    QLabel* nowDate = new QLabel(this);
    nowDate->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    QLabel* linkIcon = new QLabel(this);
    linkIcon->setPixmap(QPixmap(":/icons/link_g.png").scaledToHeight(22, Qt::SmoothTransformation));
    QLabel* readyIcon = new QLabel(this);
    readyIcon->setPixmap(QPixmap(":/icons/ready.png").scaledToHeight(22, Qt::SmoothTransformation));
    QLabel* lockIcon = new QLabel(this);
    lockIcon->setText("");
    //    lockIcon->setPixmap(QPixmap(":/icons/lock.png").scaledToHeight(22,Qt::SmoothTransformation));
    QHBoxLayout* status_layout = new QHBoxLayout(statusBarWidget);
    status_layout->setMargin(0);

    status_layout->addWidget(hosp);
    status_layout->addWidget(nowDate);
    QWidget* spacerLine = new QWidget(this);
    spacerLine->setFixedWidth(2);
    spacerLine->setObjectName("spacer_2");

    layout->addWidget(spacerLine);
    layout->addWidget(linkIcon);
    layout->addWidget(readyIcon);
    layout->addWidget(lockIcon);

    QVBoxLayout* l = new QVBoxLayout(widgetMsg);
    l->setMargin(0);
    auto w = new RollingMessageWidget(this);

    w->setMessageList(GUIErrorLW::getWARNMessages());
    w->setObjectName("need_border");
    l->addWidget(w);
    connect(EventCenter::Default(), &EventCenter::ResponseDeviceTemperature, [=](QObject*, QObject* msg) {
        nowDate->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        if (!msg) return;
        QString* s = (QString*)msg;
        lockIcon->setText(QString("%1%2").arg(*s).arg(tr("°C")));
    });
    connect(EventCenter::Default(), &EventCenter::WarnStateFlagChange, [=](QObject*, QObject* msg) {
        QVariant* var = (QVariant*)msg;
        int v = var->toInt();
        int f = 0x00000001;
        for (int i = 0; i < 32; ++i) {
            int d = (f<<i);
            if (d & v){
                printf("warn %d, val:%d\r\n", i, v);
                if (w)w->updateMessagePriority(i,1);
            } else{
                if (w)w->updateMessagePriority(i,-1);
            }
        }
    });
}
