#include "FormErrorCodeNote.h"
#include "ui_FormErrorCodeNote.h"

#include <QScroller>

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

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

    initWidget();
    initConnect();
}

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

bool FormErrorCodeNote::returnParent()
{
    return ConfigManager::instance()->saveConfig(ERROR_CODE_NOTE, QVariant::fromValue(m_tcu_cfg.error_code_note));
}

void FormErrorCodeNote::initWidget()
{
    m_error_dialog.reset(new DialogErrorNoteCode());
    m_error_dialog->setParent(this);
    m_error_dialog->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    m_error_dialog->setWindowModality(Qt::WindowModal);

    // ui->dailyTablewidget->horizontalHeader()->hide();
    ui->error_code_tableWidget->verticalHeader()->hide();

    ui->error_code_tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->error_code_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->error_code_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->error_code_tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

    QScroller::grabGesture(ui->error_code_tableWidget,              QScroller::TouchGesture);
    QScroller::grabGesture(ui->error_code_tableWidget->viewport(),  QScroller::LeftMouseButtonGesture);
}

void FormErrorCodeNote::initConnect()
{
    connect(LanguageManager::instance(),    &LanguageManager::languageChanged,  this, [this]() { ui->retranslateUi(this); });
    connect(ui->btn_add,                    &QPushButton::clicked,              this, &FormErrorCodeNote::addBtnClicked);
    connect(ui->btn_delete,                 &QPushButton::clicked,              this, &FormErrorCodeNote::deleteBtnClicked);
    connect(ui->error_code_tableWidget,     &QTableWidget::itemDoubleClicked,   this, &FormErrorCodeNote::tableWidgetItemDoubleClicked);
    connect(ui->error_code_tableWidget,     &QTableWidget::itemChanged,         this, [this]() { setIsModify(true); });
}

void FormErrorCodeNote::refreshData()
{
    ui->error_code_tableWidget->clearContents();
    ui->error_code_tableWidget->setRowCount(0);
    foreach (const auto& data, m_tcu_cfg.error_code_note) {
        appendTableWidgetItem(data);
    }
}

void FormErrorCodeNote::appendTableWidgetItem(const ERROR_NOTE &error_info)
{
    auto row = ui->error_code_tableWidget->rowCount();
    ui->error_code_tableWidget->insertRow(row);

    auto error_note_item = new QTableWidgetItem(error_info.error_note);
    error_note_item->setTextAlignment(Qt::AlignCenter);
    ui->error_code_tableWidget->setItem(row, 0, error_note_item);

    auto error_code_item = new QTableWidgetItem(QString("0x%1").arg(QString::number(error_info.error_code, 16).toUpper()));
    error_code_item->setTextAlignment(Qt::AlignCenter);
    ui->error_code_tableWidget->setItem(row, 1, error_code_item);

    ui->error_code_tableWidget->setRowHeight(row, 60);
    ui->error_code_tableWidget->setCurrentItem(nullptr);
}

void FormErrorCodeNote::updateData()
{

}

void FormErrorCodeNote::updateData(const ERROR_NOTE &data, const int &index)
{
    m_tcu_cfg.error_code_note[index] = data;
}

void FormErrorCodeNote::showEvent(QShowEvent *event)
{
    setIsModify(true);
    m_tcu_cfg = ConfigManager::instance()->tcu_cfg();
    refreshData();
    BaseWidget::showEvent(event);
}

void FormErrorCodeNote::addBtnClicked()
{
    auto error_info = ERROR_NOTE();
    error_info.error_code = GeneralInterface::getMaxErrorCode(m_tcu_cfg.error_code_note) + 1;
    m_error_dialog->setError_info(error_info);
    auto ret = m_error_dialog->exec();
    if (ret == QDialog::Accepted) {
        auto error_note_info = m_error_dialog->error_info();
        m_tcu_cfg.error_code_note.append(error_note_info);
    }
}

void FormErrorCodeNote::deleteBtnClicked()
{
    auto row = ui->error_code_tableWidget->currentRow();
    m_tcu_cfg.error_code_note.removeAt(row);
}

void FormErrorCodeNote::tableWidgetItemDoubleClicked(QTableWidgetItem *item)
{
    auto row = item->row();
    m_error_dialog->setError_info(m_tcu_cfg.error_code_note[row]);
    auto ret = m_error_dialog->exec();
    if (ret == QDialog::Accepted) {
        auto error_note_info = m_error_dialog->error_info();
        updateData(error_note_info, row);
    }
}
