#include "FormCharging.h"
#include "ui_FormCharging.h"

#include "ConfigManager.h"
#include "LanguageManager.h"
#include <QDebug>
#include "Globals.h"
#include "GeneralInterface.h"
#include "LoggerHelper.h"

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

    m_tcu_cfg = ConfigManager::instance()->tcu_cfg();
    m_ccu_cfg = ConfigManager::instance()->ccu_cfg();

    initWidget();
    initConnect();
}

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

void FormCharging::initWidget()
{
    setChargingBackground();
    setPowerBackground();
    setDurationBackground();
    setPriceBackground();
    setVoltageBackground();
    setCurrentBackground();
    setStopBtnBackground();
}

void FormCharging::initConnect()
{
    connect(ui->stop,                       &QToolButton::clicked,              this, [this]() { emit stopCharging(); });
    connect(LanguageManager::instance(),    &LanguageManager::languageChanged,  this, [this]() { ui->retranslateUi(this); });
    connect(GLOBALS,                        &Globals::chargeInfoValueChanged,   this, &FormCharging::chargeInfoValueChanged);
    connect(GLOBALS,                        &Globals::ccuConfigValueChanged,    this, [this](const CCU_CFG& ccu_cfg) {
        m_ccu_cfg = ccu_cfg;
    });
    connect(GLOBALS,                        &Globals::tcuConfigValueChanged,    this, [this](const TCU_CFG& tcu_cfg) {
        m_tcu_cfg = tcu_cfg;
        auto isOnce = (m_ccu_cfg.GUN_Numb == 1 ? true : false);
        setOnceLayoutStrect(isOnce);
    });
}

void FormCharging::setBackgroundImage(QLabel *label, const QString &imageurl)
{
    if (!label) {
        LOG_WARNING("Invalid label pointer");
        return;
    }
    QPixmap pixmap(imageurl);
    if (!pixmap.isNull()) {
        label->setAlignment(Qt::AlignCenter);
        label->setPixmap(pixmap);
    } else {
        LOG_WARNING("Failed to load background image:" + imageurl);
    }
}

void FormCharging::setChargingBackground()
{
    charging_bg_image = new QLabel();
    ui->charging_layout->addWidget(charging_bg_image,
                                   0,
                                   0,
                                   ui->charging_layout->rowCount(),
                                   ui->charging_layout->columnCount());
    setBackgroundImage(charging_bg_image, ":/images/charging/charging_background.png");
    charging_bg_image->setAlignment(Qt::AlignCenter);
    charging_bg_image->lower();
    // charging_bg_image->setScaledContents(true);
}

void FormCharging::refreshChargingBackground()
{
    bool isOnce;
    if (m_ccu_cfg.GUN_Numb == 1) {
        setBackgroundImage(charging_bg_image, ":/images/charging/charging_background_once.png");
    } else if (m_ccu_cfg.GUN_Numb == 2) {
        setBackgroundImage(charging_bg_image, ":/images/charging/charging_background.png");
    }

    isOnce = (m_ccu_cfg.GUN_Numb == 1 ? true : false);

    setOnceLayoutStrect(isOnce);
}

void FormCharging::setPowerBackground()
{
    power_bg_image = new QLabel();
    ui->power_layout->addWidget(power_bg_image,
                                0,
                                0,
                                ui->power_layout->rowCount(),
                                ui->power_layout->columnCount());
    setBackgroundImage(power_bg_image, ":/images/charging/label_charging_background.png");
    power_bg_image->lower();
    power_bg_image->setScaledContents(true);
}

void FormCharging::setDurationBackground()
{
    duration_bg_image = new QLabel();
    ui->duration_layout->addWidget(duration_bg_image,
                                0,
                                0,
                                ui->duration_layout->rowCount(),
                                ui->duration_layout->columnCount());
    setBackgroundImage(duration_bg_image, ":/images/charging/label_charging_background.png");
    duration_bg_image->lower();
    duration_bg_image->setScaledContents(true);
}

void FormCharging::setPriceBackground()
{
    price_bg_image = new QLabel();
    ui->price_layout->addWidget(price_bg_image,
                                   0,
                                   0,
                                   ui->price_layout->rowCount(),
                                   ui->price_layout->columnCount());
    setBackgroundImage(price_bg_image, ":/images/charging/label_charging_background.png");
    price_bg_image->lower();
    price_bg_image->setScaledContents(true);
}

void FormCharging::setVoltageBackground()
{
    voltage_bg_image = new QLabel();
    ui->voltage_layout->addWidget(voltage_bg_image,
                                0,
                                0,
                                ui->voltage_layout->rowCount(),
                                ui->voltage_layout->columnCount());
    setBackgroundImage(voltage_bg_image, ":/images/charging/label_charging_background.png");
    voltage_bg_image->lower();
    voltage_bg_image->setScaledContents(true);
}

void FormCharging::setCurrentBackground()
{
    current_bg_image = new QLabel();
    ui->current_layout->addWidget(current_bg_image,
                                  0,
                                  0,
                                  ui->current_layout->rowCount(),
                                  ui->current_layout->columnCount());
    setBackgroundImage(current_bg_image, ":/images/charging/label_charging_background.png");
    current_bg_image->lower();
    current_bg_image->setScaledContents(true);
}

void FormCharging::setStopBtnBackground()
{
    stop_btn_bg_image = new QLabel();
    ui->stop_layout->addWidget(stop_btn_bg_image,
                                0,
                                0,
                                ui->stop_layout->rowCount(),
                                ui->stop_layout->columnCount());
    setBackgroundImage(stop_btn_bg_image, ":/images/charging/stop_charging.png");
    stop_btn_bg_image->lower();
    stop_btn_bg_image->setScaledContents(true);
}

void FormCharging::setOnceLayoutStrect(bool isOnce)
{
    setChargeLayoutStrect(isOnce);
    setSocLayoutStrect(isOnce);
}

void FormCharging::setChargeLayoutStrect(bool isOnce)
{
    if (isOnce) {
        ui->charging_layout->setRowStretch(0, m_tcu_cfg.charge_layout_strect.once_row_strect[0]);
        ui->charging_layout->setRowStretch(1, m_tcu_cfg.charge_layout_strect.once_row_strect[1]);
        ui->charging_layout->setRowStretch(2, m_tcu_cfg.charge_layout_strect.once_row_strect[2]);

        ui->charging_layout->setColumnStretch(0, m_tcu_cfg.charge_layout_strect.once_column_strect[0]);
        ui->charging_layout->setColumnStretch(1, m_tcu_cfg.charge_layout_strect.once_column_strect[1]);
        ui->charging_layout->setColumnStretch(2, m_tcu_cfg.charge_layout_strect.once_column_strect[2]);
        ui->charging_layout->setColumnStretch(3, m_tcu_cfg.charge_layout_strect.once_column_strect[3]);
        ui->charging_layout->setColumnStretch(4, m_tcu_cfg.charge_layout_strect.once_column_strect[4]);
    } else {
        ui->charging_layout->setRowStretch(0, m_tcu_cfg.charge_layout_strect.row_strect[0]);
        ui->charging_layout->setRowStretch(1, m_tcu_cfg.charge_layout_strect.row_strect[1]);
        ui->charging_layout->setRowStretch(2, m_tcu_cfg.charge_layout_strect.row_strect[2]);

        ui->charging_layout->setColumnStretch(0, m_tcu_cfg.charge_layout_strect.column_strect[0]);
        ui->charging_layout->setColumnStretch(1, m_tcu_cfg.charge_layout_strect.column_strect[1]);
        ui->charging_layout->setColumnStretch(2, m_tcu_cfg.charge_layout_strect.column_strect[2]);
        ui->charging_layout->setColumnStretch(3, m_tcu_cfg.charge_layout_strect.column_strect[3]);
        ui->charging_layout->setColumnStretch(4, m_tcu_cfg.charge_layout_strect.column_strect[4]);
    }
}

void FormCharging::setSocLayoutStrect(bool isOnce)
{
    if (isOnce) {
        ui->soc_layout->setRowStretch(0, m_tcu_cfg.soc_layout_strect.once_row_strect[0]);
        ui->soc_layout->setRowStretch(1, m_tcu_cfg.soc_layout_strect.once_row_strect[1]);
        ui->soc_layout->setRowStretch(2, m_tcu_cfg.soc_layout_strect.once_row_strect[2]);

        ui->soc_layout->setColumnStretch(0, m_tcu_cfg.soc_layout_strect.once_column_strect[0]);
        ui->soc_layout->setColumnStretch(1, m_tcu_cfg.soc_layout_strect.once_column_strect[1]);
        ui->soc_layout->setColumnStretch(2, m_tcu_cfg.soc_layout_strect.once_column_strect[2]);
    } else {
        ui->soc_layout->setRowStretch(0, m_tcu_cfg.soc_layout_strect.row_strect[0]);
        ui->soc_layout->setRowStretch(1, m_tcu_cfg.soc_layout_strect.row_strect[1]);
        ui->soc_layout->setRowStretch(2, m_tcu_cfg.soc_layout_strect.row_strect[2]);

        ui->soc_layout->setColumnStretch(0, m_tcu_cfg.soc_layout_strect.column_strect[0]);
        ui->soc_layout->setColumnStretch(1, m_tcu_cfg.soc_layout_strect.column_strect[1]);
        ui->soc_layout->setColumnStretch(2, m_tcu_cfg.soc_layout_strect.column_strect[2]);
    }
}

QString FormCharging::getPowerConsumption(const uint32_t &start_power, const uint32_t &cur_power)
{
    double power_consum = (cur_power / 10.0) - (start_power / 10.0);
    return QString("%1/Kwh").arg(QString::number(power_consum, 'f', 2));
}

QString FormCharging::getTimeConsum(const uint32_t &start_time, const uint32_t &cur_time)
{
    int difference = qAbs(static_cast<int>(cur_time - start_time));
    QTime time(0, 0, 0);
    time = time.addSecs(difference);
    return time.toString("hh:mm:ss");
}

QString FormCharging::getExpenditure(const double &power_consum, const bool &is_cross_timezone)
{
    if (is_cross_timezone) { // 充电中跨时区
        // 后续根据时区单价进行计算
    } else { // 充电过程中未跨时区
        // 根据当前时段获取当前单价进行计算
        auto price = 1.0;
        return QString(tr("$%1")).arg(QString::number(power_consum * price, 'f', 2));
    }

    return QString("");
}

void FormCharging::chargeInfoValueChanged(const CHARGING_INFO &info)
{
    if (m_gun_id == info.GUNID) {
        auto power_consumption = getPowerConsumption(info.startKW, info.now_KW);
        auto time_consum = getTimeConsum(info.startUTC, info.now_UTC);
        auto price = getExpenditure((info.now_KW - info.startKW) / 10.0, GeneralInterface::isCrossTime(info.startUTC, info.now_UTC));
        auto soc = QString("%1%").arg(QString::number(info.SOC / 10.0, 'f', 1));
        auto voltage = QString("%1V").arg(QString::number(info.voltage / 10.0, 'f', 2));
        auto current = QString("%1A").arg(QString::number(info.current / 10.0, 'f', 2));

        ui->power_consumption->setText(power_consumption);
        ui->duration->setText(time_consum);
        ui->price->setText(price);
        ui->soc->setText(soc);
        ui->voltage->setText(voltage);
        ui->current->setText(current);
    }
}

void FormCharging::setGun_id(int newGun_id)
{
    m_gun_id = newGun_id;
}
