#include "FormLogView.h"
#include "ui_FormLogView.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QHeaderView>
#include <QFileDialog>
#include <QMessageBox>
#include <QLabel>
#include <QScroller>
#include <QMessageBox>

#include "LanguageManager.h"
#include "ConfigManager.h"
#include "GeneralInterface.h"

FormLogView::FormLogView(BaseWidget *parent)
    : BaseWidget(parent)
    , ui(new Ui::FormLogView)
{
    ui->setupUi(this);
    initUI();
    setupConnections();

    GeneralInterface::setTouchScroller(ui->scrollArea);
}

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

bool FormLogView::returnParent()
{
    if (isModify()) {
        auto ret = QMessageBox::question(this, tr("Save"),
                                         tr("The content has been modified. Do you want to save the changes?"),
                                         QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes);
        if (ret == QMessageBox::Yes) {      // save
            m_tcu_cfg.log_config.general_log_level = ui->regular_log_level->currentIndex();
            m_tcu_cfg.log_config.slave_log_level = ui->slave_machine_log_level->currentIndex();
            m_tcu_cfg.log_config.ignored_duplicate_log = ui->ignored_duplicate_log->isChecked();
            m_tcu_cfg.log_config.single_log_size = ui->single_log_size->text().toInt();
            m_tcu_cfg.log_config.total_log_size = ui->total_log_size->text().toInt();
            m_tcu_cfg.log_config.history_log_save_time = ui->save_time->text().toInt();
            return ConfigManager::instance()->saveConfig(TCU_CONFIG, QVariant::fromValue<TCU_CFG>(m_tcu_cfg));
        } else if (ret == QMessageBox::No){ // no save
            return true;
        } else {                            // cancel
            return false;
        }
    }

    return true;
}

void FormLogView::initUI()
{
    initComboBox();
    ui->regular_log->setMainText("View general logs");
    ui->regular_log->setDescriptionText("");
    ui->ocpp16_log->setMainText("View OCPP1.6 log");
    ui->ocpp16_log->setDescriptionText("");
    ui->lower_machine_log->setMainText("Check slave log");
    ui->lower_machine_log->setDescriptionText("");
}

void FormLogView::setupConnections()
{
    connect(ui->regular_log_level,          &CustomComboBox::currentIndexChanged,   this, [this]() { setIsModify(true); });
    connect(ui->slave_machine_log_level,    &CustomComboBox::currentIndexChanged,   this, [this]() { setIsModify(true); });
    connect(ui->ignored_duplicate_log,      &QCheckBox::stateChanged,               this, [this]() { setIsModify(true); });
    connect(ui->single_log_size,            &CustomLineEdit::textChanged,           this, [this]() { setIsModify(true); });
    connect(ui->total_log_size,             &CustomLineEdit::textChanged,           this, [this]() { setIsModify(true); });
    connect(ui->save_time,                  &CustomLineEdit::textChanged,           this, [this]() { setIsModify(true); });

    // connect(ui.)

    connect(LanguageManager::instance(),    &LanguageManager::languageChanged,      this, [this]() { ui->retranslateUi(this); });

    connect(ui->regular_log,                &CustomCommandLinkButton::clicked,      this, [this]() { emit itemClicked(FormItem::GENERAL); });
    connect(ui->ocpp16_log,                 &CustomCommandLinkButton::clicked,      this, [this]() { emit itemClicked(FormItem::OCPP16); });
    connect(ui->lower_machine_log,          &CustomCommandLinkButton::clicked,      this, [this]() { emit itemClicked(FormItem::SLAVE); });
}

void FormLogView::initComboBox()
{
    QStringList itemList;
    itemList << "ALL"
             << "DEBUG"
             << "INFO"
             << "WARNING"
             << "ERROR"
             << "FATIL";
    comboBoxAddItem(ui->regular_log_level, itemList);
    comboBoxAddItem(ui->slave_machine_log_level, itemList);
}

void FormLogView::comboBoxAddItem(CustomComboBox *combox, const QStringList &list)
{
    combox->clear();
    foreach (const auto item, list) {
        combox->addItem(item);
    }
}

void FormLogView::refreshShow()
{
    ui->regular_log_level->setCurrentIndex(m_tcu_cfg.log_config.general_log_level);
    ui->slave_machine_log_level->setCurrentIndex(m_tcu_cfg.log_config.slave_log_level);
    ui->ignored_duplicate_log->setChecked(m_tcu_cfg.log_config.ignored_duplicate_log);
    ui->single_log_size->setText(QString::number(m_tcu_cfg.log_config.single_log_size));
    ui->total_log_size->setText(QString::number(m_tcu_cfg.log_config.total_log_size));
    ui->save_time->setText(QString::number(m_tcu_cfg.log_config.history_log_save_time));
}

void FormLogView::showEvent(QShowEvent *event)
{
    setIsModify(false);
    m_tcu_cfg = ConfigManager::instance()->tcu_cfg();
    refreshShow();
    BaseWidget::showEvent(event);
}
