#include "uartconfigwindow.h"
#include "ui_uartconfigwindow.h"
#include "mainwindow.h"
#include "qjsonarray.h"
#include "qjsondocument.h"
#include "qjsonobject.h"
#include "qjsonvalue.h"
#include "QMessageBox"
#include "hexvalidator.h"

UartConfigWindow::UartConfigWindow(QWidget *parent,  QString cfg) :
    QDialog(parent),
    ui(new Ui::UartConfigWindow)
{
    ui->setupUi(this);
    configContent = cfg;

    ui->leWifiConnMsg->setValidator( new HexValidator(this) );
    ui->leWifiDisconnMsg->setValidator( new HexValidator(this) );

    json = QJsonDocument::fromJson(configContent.toUtf8());

    const QJsonValue v = json["communication"];

    if (v.isNull())
    {
        QMessageBox::information(this, "Waring", "Missing communication section oin the config file", QMessageBox::Ok);
        this->close();
        return;
    }
    const QJsonValue juart = v["uart"];

    int hardware_ver = json["hardware_version"].toInt();
    ui->widgetRs485Address->setVisible(false);
    ui->widgetWifi1->setVisible(false);
    ui->widgetWifi2->setVisible(false);

    if (hardware_ver == C2_V1_HARDWARE)
    {
        ui->cbUsbProto->addItem("Binnary");
        ui->cbUsbProto->addItem("Console logs");
        ui->cbUsbProto->addItem("HID keyboard");

        ui->cbUart2Proto->addItem("Binnary");
        ui->cbUart2Proto->addItem("Console logs");
    }
    else
    {
        ui->cbUsbProto->addItem("Binnary");
        ui->cbUsbProto->addItem("Console logs");

        ui->cbUart2Proto->addItem("Binnary");
        ui->cbUart2Proto->addItem("Console logs");
        ui->cbUart2Proto->addItem("RS485 - Modbus");
        ui->cbUart2Proto->addItem("RS485 - binary");
        ui->cbUart2Proto->addItem("Passthru");
        ui->cbUart2Proto->addItem("Disabled (UART pins can be used as GPIO)");
    }

    if (juart["uart0_proto"].toInt() > PROTOCOL_CONSOLE)
    {
        ui->cbUsbProto->setCurrentIndex(2);
    }
    else
    {
        ui->cbUsbProto->setCurrentIndex(juart["uart0_proto"].toInt());
    }

    ui->cbUsbSpeed->setCurrentIndex(juart["uart0_baud"].toInt());
    ui->cbUart2Proto->setCurrentIndex(juart["uart2_proto"].toInt());
    ui->cbUart2Speed->setCurrentIndex(juart["uart2_baud"].toInt());

    on_cbUsbProto_currentIndexChanged(0);

    ui->spAddress->setValue(juart["rs485_address"].toInt());
    ui->leWifiConnMsg->setText(juart["passthru_wifi_ok_msg"].toString());
    ui->leWifiDisconnMsg->setText(juart["passthru_wifi_fail_msg"].toString());

    ui->cbEnter->setChecked(juart["hid_enter"].toInt());
}

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

void UartConfigWindow::on_pushButton_2_clicked()
{
    this->close();
}

void UartConfigWindow::on_cbUsbProto_currentIndexChanged(int index)
{
    Q_UNUSED(index);

    if (ui->cbUart2Proto->currentIndex() == 0 && ui->cbUsbProto->currentIndex() == 0)
    {
        ui->btOk->setEnabled(false);
    }
    else if (ui->cbUart2Proto->currentIndex() == 1 && ui->cbUsbProto->currentIndex() == 1)
    {
        ui->btOk->setEnabled(false);
    }
    else
    {
        ui->btOk->setEnabled(true);
    }

    ui->hidEnterWidget->setVisible(ui->cbUsbProto->currentIndex() == 2);
    ui->widgetUsbSpeed->setVisible(ui->cbUsbProto->currentIndex() != 2);
    ui->widgetRs485Address->setVisible((ui->cbUart2Proto->currentIndex() == 2 || ui->cbUart2Proto->currentIndex() == 3));
    ui->widgetWifi1->setVisible(ui->cbUart2Proto->currentIndex() == 4);
    ui->widgetWifi2->setVisible(ui->cbUart2Proto->currentIndex() == 4);
}

void UartConfigWindow::on_btOk_clicked()
{
    //store all data in the json file
    QJsonObject rootObject = json.object();
    QJsonObject jcomm = rootObject["communication"].toObject();
    QJsonObject juart = jcomm["uart"].toObject();

    if (ui->cbUsbProto->currentIndex() == 2)
    {
        juart["uart0_proto"] = PROTOCOL_HID;
    }
    else
    {
        juart["uart0_proto"] = ui->cbUsbProto->currentIndex();
    }

    if (ui->cbEnter->isChecked())
    {
        juart["hid_enter"] = 1;
    }
    else
    {
        juart["hid_enter"] = 0;
    }

    juart["uart2_proto"] = ui->cbUart2Proto->currentIndex();

    juart["uart0_baud"] = ui->cbUsbSpeed->currentIndex();
    juart["uart2_baud"] = ui->cbUart2Speed->currentIndex();

    juart["rs485_address"] = ui->spAddress->value();
    juart["passthru_wifi_ok_msg"] = ui->leWifiConnMsg->text().remove(QChar(' '), Qt::CaseSensitive);
    juart["passthru_wifi_fail_msg"] = ui->leWifiDisconnMsg->text().remove(QChar(' '), Qt::CaseSensitive);

    jcomm.remove("uart");
    jcomm.insert("uart", juart);

    rootObject.remove("communication");
    rootObject.insert("communication", jcomm);

    json.setObject(rootObject);

    configContent = json.toJson();
    emit updateConfiguration(configContent);
    this->close();
}

void UartConfigWindow::on_cbUart2Proto_currentIndexChanged(int index)
{
    on_cbUsbProto_currentIndexChanged(index);
}
