#include "alarmwidget.h"
#include "alarmdialog.h"

AlarmWidget::AlarmWidget(QMap<QString, QMap<QString, QPair<QString, QString>>> &translate,
                        const QString &sensorId,
                         const QString &group,
                         const QString &date,
                         const QString &time,
                         const QString &timeEnd,
                         SensorService *sensorService,
                         QWidget *parent)
    : QWidget(parent)
    , _timeEnd(timeEnd)
    , _sensorService(sensorService)
    , _translate(translate)
    , _sensorId(sensorId)
{

    setAttribute(Qt::WA_StyledBackground);
    setStyleSheet(R"(
        QWidget {
            border: 1px solid #E5E7EB;
            background-color: transparent;
        }
    )");

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(70);
    mainLayout->setContentsMargins(25, 5, 25, 5);
    setLayout(mainLayout);

    //groupLabel = createLabel(_groupToLocalgroup[group], 100, 250);
    //sensorLabel = createLabel(_sensorToLocalSensor[sensorId], 100, 350);
    groupLabel = createLabel(translate[group][sensorId].first, 100, 250);
    sensorLabel = createLabel(translate[group][sensorId].second, 100, 350);
    dateLabel = createLabel(date, 100, 150);
    timeLabel = createLabel(time, 100, 100);

    mainLayout->addWidget(groupLabel);
    mainLayout->addWidget(sensorLabel);
    mainLayout->addWidget(dateLabel);
    mainLayout->addWidget(timeLabel);

    infoButton = new QPushButton("Подробнее");
    infoButton->setFixedHeight(40);
    infoButton->setFixedSize(140, 40);
    infoButton->setStyleSheet(R"(
        QPushButton {
            border: 1px solid #13385F;
            border-radius: 5px;
            color: #13385F;
            font-family: Inter;
            font-size: 20px;
            font-weight: 500;
            background-color: white;
        }
        QPushButton:hover {
            background-color: #e0f2f7;
            border-color: #1A4A73;
            color: #1A4A73;
        }
        QPushButton:pressed {
            background-color: #102E47;
            color: white;
            border-color: #102E47;
        }
    )");

    mainLayout->addWidget(infoButton);
    connect(infoButton, &QPushButton::clicked, this, &AlarmWidget::onInfoButtonClicked);
}

void AlarmWidget::onInfoButtonClicked() {
    alarmdialog *dialog = new alarmdialog(nullptr, _sensorId, sensorLabel->text(),
                                          groupLabel->text(), dateLabel->text() + ' ' + timeLabel->text(),
                                          _timeEnd, _sensorService);

    connect(dialog, &alarmdialog::dialogClosed, this, &AlarmWidget::onDialogClosed);

    dialog->exec();
}

void AlarmWidget::onDialogClosed() {
    QDateTime start = QDateTime::fromString(dateLabel->text() + ' ' + timeLabel->text(), "dd.MM.yyyy HH:mm");
    QDateTime end = QDateTime::fromString(_timeEnd, "dd.MM.yyyy HH:mm");
    if(end.isNull()) {
        end = QDateTime::currentDateTime();
    }
    emit dialogClosedFromWidget(groupLabel->text() + '/' + sensorLabel->text(), start, end);
}

QLabel *AlarmWidget::createLabel(const QString &text, int minSize, int maxSize)
{
    QLabel *label = new QLabel(text);
    label->setAlignment(Qt::AlignLeft);
    label->setWordWrap(true);
    label->setMinimumWidth(minSize);
    label->setMaximumWidth(maxSize);
    label->setFixedHeight(25);
    label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    label->setStyleSheet(R"(
        background: transparent;
        font-family: Inter;
        font-size: 20px;
        font-weight: 400;
        color: #13385F;
        border: none;
    )");
    return label;
}
