#include "FormChargeGunDetail.h"
#include "ui_FormChargeGunDetail.h"

#include "ConfigManager.h"
#include "LanguageManager.h"
#include "GeneralInterface.h"
#include "OtherSettings/dialog/DialogChargingPopup.h"

FormChargeGunDetail::FormChargeGunDetail(int gun_id, BaseWidget *parent)
    : BaseWidget(parent)
    , ui(new Ui::FormChargeGunDetail)
    , m_gun_id(gun_id)
{
    ui->setupUi(this);

    GeneralInterface::setTouchScroller(ui->scrollArea);

    initComboBox();
    initConnect();
    setWindowTitle(QString("Gun #%1 Detail Settings").arg(m_gun_id));
}

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

bool FormChargeGunDetail::returnParent()
{
    updateData();
    return ConfigManager::instance()->saveConfig(CCU_CONFIG, QVariant::fromValue(m_ccu_cfg));
}

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

    // test
    connect(ui->requestPowerByMinutes, &QCheckBox::clicked,     this, [this](bool status) {
        if (status) {
            DialogChargingPopup* dialog = new DialogChargingPopup(this, EditType::TIME);
            dialog->exec();
        }
    });
    connect(ui->requestPowerByPowerKW, &QCheckBox::clicked,     this, [this](bool status) {
        if (status) {
            DialogChargingPopup* dialog = new DialogChargingPopup(this, EditType::ENERGY);
            dialog->exec();
        }
    });
    connect(ui->requestPowerBySOC, &QCheckBox::clicked,         this, [this](bool status) {
        if (status) {
            DialogChargingPopup* dialog = new DialogChargingPopup(this, EditType::SOC);
            dialog->exec();
        }
    });
}

void FormChargeGunDetail::initComboBox()
{
    initGunTypeComboBox();
    initSlacIfnameComboBox();
    initCanIfnameComboBox();
}

void FormChargeGunDetail::initGunTypeComboBox()
{
    QStringList type_list;
    type_list << "NACS"
              << "CCS1"
              << "CCS2"
              << "CHAdeMO"
              << "GBDC";
    ui->GUN_Type->addItems(type_list);
}

void FormChargeGunDetail::initSlacIfnameComboBox()
{
    ui->slac_ifname->addItem("eth2");
    ui->slac_ifname->addItem("eth3");
}

void FormChargeGunDetail::initCanIfnameComboBox()
{
    ui->can_ifname->addItem("can0");
    ui->can_ifname->addItem("can1");
    ui->can_ifname->addItem("can2");
    ui->can_ifname->addItem("can3");
}

void FormChargeGunDetail::refreshData()
{
    foreach (const auto& data, m_ccu_cfg.GUN_Detail) {
        if (data.GUN_ID != m_gun_id) continue;
        ui->GUN_ID->setText(QString::number(data.GUN_ID));
        ui->GUN_Type->setCurrentText(data.GUN_Type);
        ui->GUN_Available->setChecked(data.GUN_Available);
        ui->GUN_maxI->setValue(data.GUN_maxI);
        ui->slac_ifname->setCurrentText(data.slac_ifname);
        ui->can_ifname->setCurrentText(data.can_ifname);
        ui->QRCode_enable->setChecked(data.QRCode_enable);
        ui->QRCode_URL->setText(data.QRCode_URL);
        ui->requestPowerByMinutes->setChecked(data.requestPowerByMinutes);
        ui->requestPowerByPowerKW->setChecked(data.requestPowerByPowerKW);
        ui->requestPowerBySOC->setChecked(data.requestPowerBySOC);
        ui->GUNOverTemp_Warning->setValue(data.GUNOverTemp_Warning);
        ui->GUNOverTemp_Stopping->setValue(data.GUNOverTemp_Stopping);
        ui->enable_current_limit->setChecked(data.enable_current_limit);
        ui->permitted_current->setValue(data.permitted_current);
        ui->enable_powerKW_limit->setChecked(data.enable_powerKW_limit);
        ui->permitted_powerKW->setValue(data.permitted_powerKW);
    }
}

void FormChargeGunDetail::updateData()
{
    for(int index = 0; index < m_ccu_cfg.GUN_Detail.size(); index ++) {
        if (m_ccu_cfg.GUN_Detail[index].GUN_ID != m_gun_id) continue;
        m_ccu_cfg.GUN_Detail[index].GUN_ID                 = ui->GUN_ID->text().toInt();
        m_ccu_cfg.GUN_Detail[index].GUN_Type               = ui->GUN_Type->currentText();
        m_ccu_cfg.GUN_Detail[index].GUN_Available          = ui->GUN_Available->isChecked();
        m_ccu_cfg.GUN_Detail[index].GUN_maxI               = ui->GUN_maxI->value();
        m_ccu_cfg.GUN_Detail[index].slac_ifname            = ui->slac_ifname->currentText();
        m_ccu_cfg.GUN_Detail[index].can_ifname             = ui->can_ifname->currentText();
        m_ccu_cfg.GUN_Detail[index].QRCode_enable          = ui->QRCode_enable->isChecked();
        m_ccu_cfg.GUN_Detail[index].QRCode_URL             = ui->QRCode_URL->text();
        m_ccu_cfg.GUN_Detail[index].requestPowerByMinutes  = ui->requestPowerByMinutes->isChecked();
        m_ccu_cfg.GUN_Detail[index].requestPowerByPowerKW  = ui->requestPowerByPowerKW->isChecked();
        m_ccu_cfg.GUN_Detail[index].requestPowerBySOC      = ui->requestPowerBySOC->isChecked();
        m_ccu_cfg.GUN_Detail[index].GUNOverTemp_Warning    = ui->GUNOverTemp_Warning->value();
        m_ccu_cfg.GUN_Detail[index].GUNOverTemp_Stopping   = ui->GUNOverTemp_Stopping->value();
        m_ccu_cfg.GUN_Detail[index].enable_current_limit   = ui->enable_current_limit->isChecked();
        m_ccu_cfg.GUN_Detail[index].permitted_current      = ui->permitted_current->value();
        m_ccu_cfg.GUN_Detail[index].enable_powerKW_limit   = ui->enable_powerKW_limit->isChecked();
        m_ccu_cfg.GUN_Detail[index].permitted_powerKW      = ui->permitted_powerKW->value();
    }
}

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

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

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

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