#include "FormFinished.h"
#include "ui_FormFinished.h"

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

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

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

    initWidget();

    setFinishTips(m_finish_state);

    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);
    });

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

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

void FormFinished::refreshChargingBackground()
{
    bool isOnce;
    if (m_ccu_cfg.GUN_Numb == 1) {
        setBackground(finish_bg_image, ":/images/charging/car_background_once.png");
    } else if (m_ccu_cfg.GUN_Numb == 2) {
        setBackground(finish_bg_image, ":/images/charging/car_background.png");
    }

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

    setOnceLayoutStrect(isOnce);
}

void FormFinished::initWidget()
{
    initFinishBackgroundImage();
    initPowerBackground();
    initDurationBackground();
    initPriceBackground();
}

void FormFinished::setBackground(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 FormFinished::setFinishTips(const int &state)
{
    switch (state) {
    case NORMAL_FINISH:
        ui->tips_normal->show();
        ui->tips_recover->hide();
        ui->tips_irrecover->hide();
        break;
    case RECOVERABLE_FINISH:
        ui->tips_normal->hide();
        ui->tips_recover->show();
        ui->tips_irrecover->hide();
        break;
    case IRRECOVERABLE_FINISH:
        ui->tips_normal->hide();
        ui->tips_recover->hide();
        ui->tips_irrecover->show();
        break;
    default:
        break;
    }
}

void FormFinished::initFinishBackgroundImage()
{
    finish_bg_image = new QLabel();
    ui->charging_layout->addWidget(finish_bg_image,
                                   0,
                                   0,
                                   ui->charging_layout->rowCount(),
                                   ui->charging_layout->columnCount());
    setBackground(finish_bg_image, ":/images/charging/car_background.png");
    finish_bg_image->lower();
}

void FormFinished::initPowerBackground()
{
    power_bg_image = new QLabel();
    ui->power_layout->addWidget(power_bg_image,
                                0,
                                0,
                                ui->power_layout->rowCount(),
                                ui->power_layout->columnCount());
    setBackground(power_bg_image, ":/images/charging/power_consumption_label_background.png");
    power_bg_image->lower();
    power_bg_image->setScaledContents(true);
}

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

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

void FormFinished::setOnceLayoutStrect(const bool &is_once)
{
    if (is_once) {
        ui->soc_layout->setRowStretch(0, m_tcu_cfg.finish_layout_strect.once_row_strect[0]);
        ui->soc_layout->setRowStretch(1, m_tcu_cfg.finish_layout_strect.once_row_strect[1]);
        ui->soc_layout->setRowStretch(2, m_tcu_cfg.finish_layout_strect.once_row_strect[2]);

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

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


