#include "sensordialog.h"
#include "ui_sensordialog.h"

#include "savesensorservice.h"
#include "sensorlogervice.h"

#include <QCloseEvent>
#include <QPainterPath>

#include <QDebug>

SensorDialog::SensorDialog(QWidget *parent, SensorWidget *sensor, HttpClient *httpClient)
    : QDialog(parent)
    , ui(new Ui::SensorDialog)
    , sensorWidget(sensor)
    , _httpClient(httpClient)
{
    ui->setupUi(this);

    ui->isAlarm->addItems({"Да", "Нет"});
    ui->isAlarm->setCurrentText(sensorWidget->getSensor().isAlarmEnabled ? "Да" : "Нет");
    ui->isBooled->addItems({"Да", "Нет"});
    ui->isBooled->setCurrentText(sensorWidget->getSensor().isBooled ? "Да" : "Нет");
    ui->isVisible->addItems({"Да", "Нет"});
    ui->isVisible->setCurrentText(sensorWidget->getSensor().isHide ? "Да" : "Нет");

    ui->saveButton->setCursor(Qt::PointingHandCursor);
    ui->cancelButton->setCursor(Qt::PointingHandCursor);

    ui->nameHeader->setText(sensorWidget->getSensor().id);
    ui->name->setText(sensorWidget->getSensor().name);
    ui->unitName->setText(sensorWidget->getSensor().unitName);
    ui->alarmLow->setText(QString::number(sensorWidget->getSensor().alarmLowerBound));
    ui->alarmUp->setText(QString::number(sensorWidget->getSensor().alarmUpperBound));
    ui->alarmCooldown->setText(QString::number(sensorWidget->getSensor().alarmCooldown));

    SensorLogService *sensorLogService = new SensorLogService(httpClient);
    sensorLogs = sensorLogService->getSensorLog(sensorWidget->getSensor().id);

    sensorC = sensorWidget->getSensor();
    sensorLogsC = sensorLogs;

    ui->logError->setText(sensorLogs.getLogError());
    ui->logManual->setText(sensorLogs.getLogManual());
    ui->alarmSleepage->setText(QString::number(sensorLogs.getAlarmSleepage()));
    ui->logHugeError->setText(sensorLogs.getLogHugeError());

    connect(ui->cancelButton, &QPushButton::clicked, this, [this]() {
        isSavingSuccess = false;
        close();
    });
    connect(ui->saveButton, &QPushButton::clicked, this, &SensorDialog::onSaveButtonClicked);
    connect(ui->isAlarm, &QComboBox::currentTextChanged, this, &SensorDialog::onIsAlarmChanged);
    connect(ui->isBooled, &QComboBox::currentTextChanged, this, &SensorDialog::onIsBooledChanged);
    connect(ui->isVisible, &QComboBox::currentTextChanged, this, &SensorDialog::onIsVisibleChanged);
    connect(ui->name, &QLineEdit::textChanged, this, &SensorDialog::onNameChanged);
    connect(ui->unitName, &QLineEdit::textChanged, this, &SensorDialog::onUnitNameChanged);
    connect(ui->alarmLow, &QLineEdit::textChanged, this, &SensorDialog::onAlarmLowChanged);
    connect(ui->alarmUp, &QLineEdit::textChanged, this, &SensorDialog::onAlarmUpChanged);
    connect(ui->alarmCooldown, &QLineEdit::textChanged, this, &SensorDialog::onAlarmCooldownChanged);
    connect(ui->logError, &QTextEdit::textChanged, this, &SensorDialog::onLogErrorChanged);
    connect(ui->logManual, &QTextEdit::textChanged, this, &SensorDialog::onLogManualChanged);
    connect(ui->alarmSleepage, &QLineEdit::textChanged, this, &SensorDialog::onAlarmSleepageChanged);
    connect(ui->logHugeError, &QTextEdit::textChanged, this, &SensorDialog::onLogHugeErrorChanged);
}

void SensorDialog::onIsAlarmChanged(const QString &value)
{
    sensorC.isAlarmEnabled = (value == "Да");
}

void SensorDialog::onIsBooledChanged(const QString &value)
{
    sensorC.isBooled = (value == "Да");
}

void SensorDialog::onIsVisibleChanged(const QString &value)
{
    sensorC.isHide = (value == "Да");
}

void SensorDialog::onNameChanged(const QString &value)
{
    sensorC.name = value;
}

void SensorDialog::onUnitNameChanged(const QString &value)
{
    sensorC.unitName = value;
}

void SensorDialog::onAlarmLowChanged(const QString &value)
{
    sensorC.alarmLowerBound = value.toDouble();
}

void SensorDialog::onAlarmUpChanged(const QString &value)
{
    sensorC.alarmUpperBound = value.toDouble();
}

void SensorDialog::onAlarmCooldownChanged(const QString &value)
{
    sensorC.alarmCooldown = value.toDouble();
}

void SensorDialog::onLogErrorChanged()
{
    sensorLogsC.setLogError(ui->logError->toPlainText());
}

void SensorDialog::onLogManualChanged()
{
    sensorLogsC.setLogManual(ui->logManual->toPlainText());
}

void SensorDialog::onAlarmSleepageChanged(const QString &value)
{
    sensorLogsC.setAlarmSleepage(value.toInt());
}

void SensorDialog::onLogHugeErrorChanged()
{
    sensorLogsC.setLogHugeError(ui->logHugeError->toPlainText());
}

void SensorDialog::onSaveButtonClicked()
{
    sensorWidget->setSensor(sensorC);
    SaveSensorService *saveSensorService = new SaveSensorService(_httpClient);

    if (saveSensorService->saveSensorLog(sensorC, sensorLogsC)) {
        isSavingSuccess = true;
        emit resultReady(SaveSuccess);
        accept();
    } else {
        isSavingSuccess = false;
        emit resultReady(SaveError);
        reject();
    }
}

void SensorDialog::closeEvent(QCloseEvent *event)
{
    if (!isSavingSuccess) {
        emit resultReady(NoSave);
    }
    event->accept();
}

SensorDialog::~SensorDialog()
{
    delete ui;
}
