#include "FormError.h"
#include "ui_FormError.h"

#include "LanguageManager.h"
#include "TcpServerThread.h"
#include "ConfigManager.h"
#include "DataTypeDef.h"
#include "Globals.h"

FormError::FormError(const int &gun_id, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormError)
    , m_gun_id(gun_id)
    , m_error_tips_visible(false)
{
    ui->setupUi(this);

    // 设置事件传递属性
    setAttribute(Qt::WA_TransparentForMouseEvents, false);
    setAttribute(Qt::WA_AcceptTouchEvents, true);

    m_error_tips_visible = ConfigManager::instance()->tcu_cfg().error_tips_visible;
    ui->error_tips->setText("No client connection.");
    ui->error_tips->setWordWrap(true);
    ui->error_tips->setVisible(m_error_tips_visible);

    connect(GLOBALS, &Globals::tcuConfigValueChanged,                           this, [this](const TCU_CFG& tcu_cfg) {
        ui->error_tips->setVisible(tcu_cfg.error_tips_visible);
    });

    if (gun_id == CHARGE_GUN_DEFAULT_1) {
        connect(GLOBALS, &Globals::gunErrorValueChanged_1,                      this, &FormError::slotValueChanged);
    } else if (gun_id == CHARGE_GUN_DEFAULT_2) {
        connect(GLOBALS, &Globals::gunErrorValueChanged_2,                      this, &FormError::slotValueChanged);
    }

    connect(TcpServerThread::instance(), &TcpServerThread::clientDisconnected,  this, [this]() {
        setError(GunError::GUN_TCP_DISCONNECT, "CCU Client disconnect!");
    });

    connect(TcpServerThread::instance(), &TcpServerThread::heartbeatTimeout,    this, [this](const int& gun_id) {
        if (gun_id == m_gun_id) {
            setError(GunError::GUN_TCP_HEART_TIMIEOUT, "CCU heart msg timeout!");
        }
    });

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

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

bool FormError::event(QEvent *event)
{
    // 确保事件能够继续传递到父窗口
    if (event->type() == QEvent::TouchBegin ||
        event->type() == QEvent::TouchUpdate ||
        event->type() == QEvent::TouchEnd ||
        event->type() == QEvent::MouseButtonPress ||
        event->type() == QEvent::MouseButtonRelease) {
        // 将事件传递给父窗口
        QWidget::event(event);
        return true;
    }
    return QWidget::event(event);
}

void FormError::setError(const int &error, const QString &error_str)
{
    auto imageUrl = getErrorImageUrl(error);
    ui->error->setPixmap(QPixmap(imageUrl));
    ui->error_tips->setText(error_str);
}

void FormError::slotValueChanged(const UINT32_QSTRING_MAP &error)
{
    if (error.isEmpty()) {
        ui->error_tips->setText("");
        return;
    }

    auto values = error.values();
    QStringList error_list;
    error_list.clear();
    for (const auto& error_str : std::as_const(values)) {
        error_list.append(error_str);
    }

    ui->error_tips->setText(error.values()[0]);
}

QString FormError::getErrorImageUrl(const int &error)
{
    switch (error) {
    case GunError::GUN_UNAVAILABEL:
    case GunError::GUN_TCP_DISCONNECT:
    case GunError::GUN_TCP_NO_CLIENT:
    case TcpError::INIT_FAILED:
    case TcpError::SEND_FAILED:
    case TcpError::INVALID_SOCKET:
    case GunError::GUN_TCP_HEART_TIMIEOUT:
        return QString(":/images/alarm/devfault.png");
    case GunError::GUN_OUT_OF_SERVICE:
        return QString(":/images/alarm/outtimeserver.png");
    case GunError::GUN_MAX_POWER_LIMIT:
        return QString(":/images/alarm/powerlimit.png");
    default: return QString(":/images/alarm/devfault.png");
    }
}
