#include "FormCcuSetting.h"
#include "ui_FormCcuSetting.h"

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

#include <QMessageBox>

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

    initWidget();
    initConnect();
}

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

bool FormCcuSetting::returnParent()
{
    if (getCCUConfig(m_ccu_cfg)) {
        return ConfigManager::instance()->saveConfig(CCU_CONFIG, QVariant::fromValue<CCU_CFG>(m_ccu_cfg));
    } else {
        return false;
    }
}

void FormCcuSetting::initWidget()
{
    GeneralInterface::setTouchScroller(ui->scrollArea);

}

void FormCcuSetting::initConnect()
{
    connect(LanguageManager::instance(), &LanguageManager::languageChanged, this, [this]() { ui->retranslateUi(this); });
    connect(GLOBALS, &Globals::ccuConfigValueChanged, this, [this](const CCU_CFG& ccu_cfg) {
        if (!this->isVisible()) return;
        if (isModify()) {
            auto ret = QMessageBox::information(this,
                                                tr("tips"),
                                                tr("Do you want to load the updated configuration file content?"),
                                                QMessageBox::No | QMessageBox::Yes);
            if (ret == QMessageBox::Yes) {
                m_ccu_cfg = ccu_cfg;
                refreshData(m_ccu_cfg);
            } else if (ret == QMessageBox::No){
                return;
            }
        } else {
            m_ccu_cfg = ccu_cfg;
            refreshData(m_ccu_cfg);
        }
    });

    connectAllValueChanged(this);
}

void FormCcuSetting::connectAllValueChanged(QWidget *widget)
{
    // QLineEdit
    if (auto edit = qobject_cast<CustomLineEdit*>(widget)) {
        connect(edit, &QLineEdit::textChanged,                      this, &FormCcuSetting::onValueChanged);
    }
    // CustomLineEdit
    else if (auto edit = qobject_cast<CustomLineEdit*>(widget)) {
        connect(edit, &CustomLineEdit::textChanged,                 this, &FormCcuSetting::onValueChanged);
    }
    // CustomSpinBox
    else if (auto spin = qobject_cast<CustomSpinBox*>(widget)) {
        connect(spin, QOverload<int>::of(&QSpinBox::valueChanged),  this, &FormCcuSetting::onValueChanged);
    }
    // QCheckBox
    else if (auto check = qobject_cast<QCheckBox*>(widget)) {
        connect(check, &QCheckBox::stateChanged,                    this, &FormCcuSetting::onValueChanged);
    }

    // 递归遍历所有子控件
    auto list = widget->findChildren<QWidget*>();
    for (auto child : std::as_const(list)) {
        connectAllValueChanged(child);
    }
}

void FormCcuSetting::refreshData(const CCU_CFG &data)
{
    ui->tcu_id->setValue(data.TCU_ID);
    ui->gun_number->setValue(data.GUN_Numb);
    ui->ccu_sn->setText(data.CCU_SN);
    ui->ccu_series->setText(data.CCU_Series);
    ui->ccu_vendor->setText(data.CCU_Vendor);
    ui->ccu_available->setChecked(data.CCU_Available);

    ui->username->setText(data.username);
    ui->password->setText(data.password);
    ui->BoxOverTemp_Warning->setValue(data.BoxOverTemp_Warning);
    ui->BoxOverTemp_Stopping->setValue(data.BoxOverTemp_Stopping);

    ui->enable_offline_price->setChecked(data.enalbe_offline_price);
    ui->iso4217_currencySymbol->setText(data.ISO4217_CurrencySymbol);
    ui->iso4217_minor_unit->setValue(data.ISO4217_MinorUnit);
    ui->iso4217_least_sign->setValue(data.ISO4217_LeastSign);
}

bool FormCcuSetting::getCCUConfig(CCU_CFG &data)
{
    data.TCU_ID                 = ui->tcu_id->value();
    data.GUN_Numb               = ui->gun_number->value();
    data.CCU_SN                 = ui->ccu_sn->text();
    data.CCU_Series             = ui->ccu_series->text();
    data.CCU_Vendor             = ui->ccu_vendor->text();
    data.CCU_Available          = ui->ccu_available->isChecked();
    data.username               = ui->username->text();
    data.password               = ui->password->text();
    data.BoxOverTemp_Warning    = ui->BoxOverTemp_Warning->value();
    data.BoxOverTemp_Stopping   = ui->BoxOverTemp_Stopping->value();
    data.enalbe_offline_price   = ui->enable_offline_price->isChecked();
    data.ISO4217_CurrencySymbol = ui->iso4217_currencySymbol->text();
    data.ISO4217_MinorUnit      = ui->iso4217_minor_unit->value();
    data.ISO4217_LeastSign      = ui->iso4217_least_sign->value();
    return true;
}

void FormCcuSetting::showEvent(QShowEvent *event)
{
    setIsModify(false);
    m_ccu_cfg = ConfigManager::instance()->ccu_cfg();
    refreshData(m_ccu_cfg);
    BaseWidget::showEvent(event);
}

void FormCcuSetting::hideEvent(QHideEvent *event)
{
    setIsModify(false);
    BaseWidget::hideEvent(event);
}

void FormCcuSetting::onValueChanged()
{
    setIsModify(true);
}
