#include "MainWindow.h"

#include <QApplication>
#include <QFile>
#include <QDir>
#include <QObject>
#include <QFontDatabase>
#include <QStyleFactory>

#include "ConfigManager.h"
#include "TcpServerThread.h"
#include "LoggerHelper.h"
#include "Globals.h"
#include "DimmingController.h"

static void initGlobals() {
    // 初始化充电枪基本信息
    BASIC_INFO basic_info_1;
    GLOBALS->setValue(GLOBALS_GUN_BASIC_INFO_1, QVariant::fromValue(basic_info_1));
    BASIC_INFO basic_info_2;
    GLOBALS->setValue(GLOBALS_GUN_BASIC_INFO_2, QVariant::fromValue(basic_info_2));

    // 初始化充电枪充电信息
    CHARGING_INFO charging_gun_info_1;
    GLOBALS->setValue(GLOBALS_CHARGING_INFO_1, QVariant::fromValue(charging_gun_info_1));
    CHARGING_INFO charging_gun_info_2;
    GLOBALS->setValue(GLOBALS_CHARGING_INFO_2, QVariant::fromValue(charging_gun_info_2));

    // 初始化充电枪错误码结构体
    QMap<uint8_t, QString> gun_error_1;
    GLOBALS->setValue(GLOBALS_GUN_ERROR_1, QVariant::fromValue(gun_error_1));
    QMap<uint8_t, QString> gun_error_2;
    GLOBALS->setValue(GLOBALS_GUN_ERROR_2, QVariant::fromValue(gun_error_2));

    // 初始化充电枪状态码结构体
    QMap<uint8_t, QString> gun_state_1;
    GLOBALS->setValue(GLOBALS_GUN_STATE_1, QVariant::fromValue(gun_state_1));
    QMap<uint8_t, QString> gun_state_2;
    GLOBALS->setValue(GLOBALS_GUN_STATE_2, QVariant::fromValue(gun_state_2));

    // 初始化充电枪类型
    QMap<uint8_t, QString> gun_type_1;
    GLOBALS->setValue(GLOBALS_GUN_TYPE_1, QVariant::fromValue(gun_type_1));
    QMap<uint8_t, QString> gun_type_2;
    GLOBALS->setValue(GLOBALS_GUN_TYPE_2, QVariant::fromValue(gun_type_2));

    // 鉴权列表
    AUTH_LIST auth_list;
    GLOBALS->setValue(GLOBALS_AUTH_LIST, QVariant::fromValue(auth_list));

    // CCU 配置信息
    CCU_CFG ccu_cfg;
    GLOBALS->setValue(GLOBALS_CCU_CFG, QVariant::fromValue(ccu_cfg));

    // 充电配置信息
    CHARGING_PROFILES charging_profiles;
    GLOBALS->setValue(GLOBALS_CHARGING_PROFILES, QVariant::fromValue(charging_profiles));

    // ocpp config key
    ocpp_info_t ocpp_cfg_key;
    GLOBALS->setValue(GLOBALS_CFG_KEY, QVariant::fromValue(ocpp_cfg_key));

    // charging records
    CHARGING_RECORDS_INFO charging_records;
    GLOBALS->setValue(GLOBALS_OFFLINE_CHARGING_RECORDS, QVariant::fromValue(charging_records));

    // tcu config
    TCU_CFG tcu_cfg;
    GLOBALS->setValue(GLOBALS_TCU_CFG, QVariant::fromValue(tcu_cfg));
}

static int init() {
    // 初始化全局变量
    initGlobals();

    // 初始化日志管理模块
    auto ret = LoggerHelper::intance()->init(LOG_FILE_PATH, DEBUG);
    if (ret != INIT_OK) return ret;

    // 加载 CCU 配置文件
    ret = ConfigManager::instance()->loadCcuConfigFile(CCU_CONFIG_FILE_PATH);
    if (ret != INIT_OK) return ret;

    // 加载 Charging profiles 配置文件
    ret = ConfigManager::instance()->loadChargingProfiles(CHARGING_PROFILES_PATH);
    if (ret != INIT_OK) return ret;

    // 加载 OCPP Config key 配置文件
    ret = ConfigManager::instance()->loadConfigKey(OCPP_CONFIG_KEY_FILE_PATH);
    if (ret != INIT_OK) return ret;

    // 加载离线充电记录配置文件
    ret = ConfigManager::instance()->loadOffileChargingRecords(OFFLINE_CHARGIN_RECORDS_PATH);
    if (ret != INIT_OK) return ret;

    // 加载鉴权卡配置文件
    ret = ConfigManager::instance()->loadAuthorizationList(AUTH_CONFIG_FILE_PATH);
    if (ret != INIT_OK) return ret;

    // 加载 GUI 终端配置文件
    ret = ConfigManager::instance()->loadTcuConfigFile(TCU_CONFIG_FILE_PATH);
    if (ret != INIT_OK) return ret;

    // 初始化 TCP 服务端
    ret = TcpServerThread::instance()->init();
    if (ret != INIT_OK) return ret;

    // 加载 GUI 终端默认样式
    LoggerHelper::intance()->info("Load the default style of the GUI terminal.");
    QFile file(":/styles/Dark.qss");
    /* 判断文件是否存在 */
    if (file.exists() ) {
        file.open(QFile::ReadOnly);
        QString styleSheet = QLatin1String(file.readAll());
        qApp->setStyleSheet(styleSheet);
        file.close();
    } else {
        return INIT_GUI_STYLE_LOAD_ERROR;
    }

    QApplication::setStyle(QStyleFactory::create("Fusion"));

    return INIT_OK;
}

void fondLoad(QWidget *app) {
    int fontId = QFontDatabase::addApplicationFont(":/fonts/Source Han Sans K Regular.ttf");
    if (fontId != -1) {
        QStringList family = QFontDatabase::applicationFontFamilies(fontId);
        if (!family.isEmpty()) {
            QFont font(family.at(0));
            app->setFont(font);
            LoggerHelper::intance()->info("font load ok !!!!!");
        } else {
            LoggerHelper::intance()->warning("font load ok !!!!!");
        }
    } else {
        LoggerHelper::intance()->warning("font load ok !!!!!");
    }
}

int main(int argc, char *argv[])
{
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    // 禁用Qt的高DPI自动缩放
    QGuiApplication::setAttribute(Qt::AA_DisableHighDpiScaling);

    // 加载虚拟键盘模块
    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    // qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard_compositor"));

    // 设置默认语言（例如英文）
    QLocale locale(QLocale::English);
    QLocale::setDefault(locale);

    QApplication a(argc, argv);

    auto ret = init();
    if (ret != INIT_OK) {
        auto error = QString("error code : %1").arg(QString::number(ret));
        LoggerHelper::intance()->error(error);
        return 0;
    }

    MainWindow w;
    fondLoad(&w);
    w.init();
    w.showFullScreen();
    // w.resize(1280, 880);
    // w.show();

    // 创建变暗控制器
    // DimmingController dimController;

    // 监听用户活动重置计时器
    // a.installEventFilter(&dimController);
    return a.exec();
}
