#include "sensorwidget.h"
#include <QMouseEvent>

SensorWidget::SensorWidget(QWidget *parent, const Sensor &sensor)
    : QFrame(parent), sensor(sensor),
    titleLabel(new QLabel(this)), valueLabel(new QLabel(this)), incidentLabel(new QLabel(this)),
    layout(new QVBoxLayout(this))
{
    setFrameStyle(QFrame::Box);
    setLineWidth(1);

    setFixedSize(250, 200);

    layout->addWidget(titleLabel);
    layout->addWidget(valueLabel);
    layout->addWidget(incidentLabel);

    setErrorState();

    QString titleText = sensor.name;
    if (titleText.length() > 40) {
        titleText = titleText.left(37) + "...";
    }
    titleLabel->setText(titleText);

    titleLabel->setWordWrap(true);
    titleLabel->setMinimumHeight(100);
    titleLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);

    if(sensor.isBooled) {
        valueLabel->setText(sensor.lastValue != 0 ? "Да" : "Нет");
    } else {
        valueLabel->setText(QString::number(sensor.lastValue) + ' ' + sensor.unitName);
        incidentLabel->setText(QString::number(sensor.alarmLowerBound) + "-" + QString::number(sensor.alarmUpperBound));
    }
    layout->setContentsMargins(10, 10, 10, 10);
    layout->setSpacing(5);
    setLayout(layout);
}

void SensorWidget::mousePressEvent(QMouseEvent *event) {
    emit clicked(this);  // Генерация сигнала о клике
    QFrame::mousePressEvent(event);
}

void SensorWidget::setErrorState()
{
    if (sensor.isAlarmEnabled) {
        setStyleSheet("QFrame { border: 2px solid #FF453A; border-radius: 10px; background: "
                      "rgba(255, 69, 58, 0.25);}");

        titleLabel->setStyleSheet("font-family: Inter;"
                                  "font-size: 24px;"
                                  "font-weight: 400;"
                                  "line-height: 29.05px;"
                                  "text-align: left;"
                                  "color: #13385F;"
                                  "border: 0;"
                                  "background: transparent;");

        valueLabel->setStyleSheet("font-family: Inter;"
                                  "font-size: 24px;"
                                  "font-weight: 400;"
                                  "line-height: 29.05px;"
                                  "text-align: left;"
                                  "color: #FF453A;"
                                  "border: 0;"
                                  "background: transparent;");

        incidentLabel->setStyleSheet("font-family: Inter;"
                                     "font-size: 24px;"
                                     "font-weight: 400;"
                                     "line-height: 29.05px;"
                                     "text-align: left;"
                                     "color: #13385F;"
                                     "border: 0;"
                                     "background: transparent;");
    } else {
        setStyleSheet("QFrame { border: 2px solid rgba(229, 231, 235, 0.70);; border-radius: 10px; "
                      "background: rgba(255, 255, 255, 0.70)}");

        titleLabel->setStyleSheet("font-family: Inter;"
                                  "font-size: 24px;"
                                  "font-weight: 400;"
                                  "line-height: 29.05px;"
                                  "text-align: left;"
                                  "color: #13385F;"
                                  "border: 0;"
                                  "background: transparent;");

        valueLabel->setStyleSheet("font-family: Inter;"
                                  "font-size: 24px;"
                                  "font-weight: 400;"
                                  "line-height: 29.05px;"
                                  "text-align: left;"
                                  "color: #13385F;"
                                  "border: 0;"
                                  "background: transparent;");

        incidentLabel->setStyleSheet("font-family: Inter;"
                                     "font-size: 24px;"
                                     "font-weight: 400;"
                                     "line-height: 29.05px;"
                                     "text-align: left;"
                                     "color: rgba(19, 56, 95, 0.5);"
                                     "border: 0;"
                                     "background: transparent;");
    }
}

Sensor SensorWidget::getSensor() const {
    return sensor;
}

void SensorWidget::setSensor(const Sensor value) {
    sensor = value;
    QString titleText = sensor.name;
    if (titleText.length() > 40) {
        titleText = titleText.left(37) + "...";
    }
    titleLabel->setText(titleText);
    if(sensor.isBooled) {
        valueLabel->setText(sensor.lastValue != 0 ? "Да" : "Нет");
    } else {
        valueLabel->setText(QString::number(sensor.lastValue) + ' ' + sensor.unitName);
        incidentLabel->setText(QString::number(sensor.alarmLowerBound) + "-" + QString::number(sensor.alarmUpperBound));
    }
}