#include "FormOCPP21GeneralSettings.h"
#include "ui_FormOCPP21GeneralSettings.h"

#include "LanguageManager.h"

FormOCPP21GeneralSettings::FormOCPP21GeneralSettings(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormOCPP21GeneralSettings)
{
    ui->setupUi(this);

    initWidget();
    initConnect();
}

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

ocpp2_1_info_t FormOCPP21GeneralSettings::ocpp_info()
{
    updateData();
    return m_ocpp_info;
}

void FormOCPP21GeneralSettings::setOcpp_info(const ocpp2_1_info_t &newOcpp_info)
{
    m_ocpp_info = newOcpp_info;
    refreshData();
}

void FormOCPP21GeneralSettings::initWidget()
{
    ui->connect_ip_string->setFormType(General::Type::IP_ADDR);
}

void FormOCPP21GeneralSettings::initConnect()
{
    connect(LanguageManager::instance(), &LanguageManager::languageChanged, this, [this]() { ui->retranslateUi(this); });
    connect(ui->status, &QToolButton::clicked, this, [this]() {
        if (m_is_spread) {
            ui->ocpp_general_settings->hide();
            ui->status->setIcon(QIcon(":/icons/spread.png"));
            m_is_spread = false;
        } else {
            ui->ocpp_general_settings->show();
            ui->status->setIcon(QIcon(":/icons/packup.png"));
            m_is_spread = true;
        }
    });

    connect(ui->enable,                     &QCheckBox::stateChanged,                           this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->connect_ip_string,          &CustomLineEdit::textChanged,                       this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->connect_port,               QOverload<int>::of(&CustomSpinBox::valueChanged),   this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->ssl_ca_filepath,            &CustomLineEdit::textChanged,                       this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->ssl_cert_filepath,          &CustomLineEdit::textChanged,                       this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->ssl_private_key_filepath,   &CustomLineEdit::textChanged,                       this, &FormOCPP21GeneralSettings::valueChanged);
    connect(ui->enable_ssl,                 &QCheckBox::stateChanged,                           this, &FormOCPP21GeneralSettings::valueChanged);
}

void FormOCPP21GeneralSettings::refreshData()
{
    ui->enable->setChecked(m_ocpp_info.enable);
    ui->connect_ip_string->setText(m_ocpp_info.connect_ip_string);
    ui->connect_port->setValue(m_ocpp_info.connect_port);
    ui->ssl_ca_filepath->setText(m_ocpp_info.ssl_ca_filepath);
    ui->ssl_cert_filepath->setText(m_ocpp_info.ssl_cert_filepath);
    ui->ssl_private_key_filepath->setText(m_ocpp_info.ssl_private_key_filepath);
    ui->enable_ssl->setChecked(m_ocpp_info.enable_ssl);
}

void FormOCPP21GeneralSettings::updateData()
{
    m_ocpp_info.enable                      = ui->enable->isChecked();
    m_ocpp_info.connect_ip_string           = ui->connect_ip_string->text();
    m_ocpp_info.connect_port                = ui->connect_port->value();
    m_ocpp_info.ssl_ca_filepath             = ui->ssl_ca_filepath->text();
    m_ocpp_info.ssl_cert_filepath           = ui->ssl_cert_filepath->text();
    m_ocpp_info.ssl_private_key_filepath    = ui->ssl_private_key_filepath->text();
    m_ocpp_info.enable_ssl                  = ui->enable_ssl->isChecked();
}
