#include "FormPlugIn.h"
#include "ui_FormPlugIn.h"

#include <QDateTime>
#include <QDebug>
#include "ConfigManager.h"
#include "Globals.h"

FormPlugIn::FormPlugIn(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormPlugIn)
    , isStart(false)
    , m_gun_id(1)
    , m_gun_type(GUNTYPE_NACS)
    , m_auth_result(false)
{
    ui->setupUi(this);

    initWidget();

    m_timer = new QTimer();
    m_timer->setInterval(1000);

    m_gun_variable_names.clear();
    m_charge_variable_names.clear();

    m_gun_variable_names << GLOBALS_GUN_BASIC_INFO_1
                         << GLOBALS_GUN_BASIC_INFO_2;

    m_charge_variable_names << GLOBALS_CHARGING_INFO_1
                            << GLOBALS_CHARGING_INFO_2;

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

    initConnect();
}

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

void FormPlugIn::setChargeGunDetail(int index)
{
    m_gun_id = index;
}

void FormPlugIn::slotControlButtonCLicked()
{
    // test
    emit authentication();
    m_timer->start();
}

void FormPlugIn::authTimeout()
{

}

void FormPlugIn::globalsValueChanged(const QString &key, const QVariant &value)
{
    switch (GLOBALS->getKeyIndex(key)) {
    case GLOBALS_GUN_BASIC_INFO_KEY_1:
    case GLOBALS_GUN_BASIC_INFO_KEY_2:
        displayGunBasicInfo(value.value<BASIC_INFO>());
        break;
    case GLOBALS_CHARGING_INFO_KEY_1:
    case GLOBALS_CHARGING_INFO_KEY_2:
        displayChargeGunDetail(value.value<CHARGING_INFO>());
        break;
    case GLOBALS_GUN_ERROR_KEY_1:
        // refreshGunError(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_1);
        break;
    case GLOBALS_GUN_ERROR_KEY_2:
        // refreshGunError(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_2);
        break;
    case GLOBALS_GUN_STATE_KEY_1:
        // refreshGunState(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_1);
        break;
    case GLOBALS_GUN_STATE_KEY_2:
        // refreshGunState(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_2);
        break;
    case GLOBALS_GUN_TYPE_KEY_1:
        // refreshGunType(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_1);
        break;
    case GLOBALS_GUN_TYPE_KEY_2:
        // refreshGunType(value.value<QMap<uint8_t, QString>>(), CHARGE_GUN_DEFAULT_2);
        break;
    default:
        break;
    }
}

void FormPlugIn::displayChargeGunDetail(const CHARGING_INFO& info)
{
    qDebug() << info.GUNID;
}

void FormPlugIn::displayGunBasicInfo(const BASIC_INFO& info)
{
    if (info.GUNID != m_gun_id) return;
}

void FormPlugIn::setGunType(uint8_t newGun_type)
{
    m_gun_type = newGun_type;
}

void FormPlugIn::setAuthenticationResult(const bool &result)
{
    if (!result) m_timer->stop();
    m_auth_result = result;
}

void FormPlugIn::refreshChargingBackground()
{
    if (m_ccu_cfg.GUN_numb == 1) {
        ui->background->setPixmap(QPixmap(":/images/charging/charge_gun_background_once.png"));
    } else if (m_ccu_cfg.GUN_numb == 2) {
        ui->background->setPixmap(QPixmap());
        ui->background->setPixmap(QPixmap(":/images/charging/charge_gun_background.png"));
    }
}

void FormPlugIn::initWidget()
{
    this->setAttribute(Qt::WA_TranslucentBackground);
}

void FormPlugIn::initConnect()
{}
