#include "FormHome.h"
#include "ui_FormHome.h"

#include <QDebug>
#include <QShowEvent>

#include "workspace/home/FormStandby.h"

#include "workspace/settings/FormSettings.h"
#include "workspace/settings/general/FormGeneralSettings.h"
#include "workspace/settings/device/FormDeviceInfo.h"
#include "workspace/settings/key/FormConfigurationKey.h"
#include "workspace/settings/charging/FormChargeRecords.h"
#include "workspace/settings/firmware/FormFirmwareInfo.h"
#include "workspace/settings/transfer/FormTransfer.h"
#include "workspace/settings/log/FormLogView.h"
#include "workspace/settings/payment/FormPayment.h"

FormHome::FormHome(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormHome)
    , m_currentForm(workspace_form_enum::STANDBY)
{
    ui->setupUi(this);

    // 安装事件过滤器到应用程序对象
    qApp->installEventFilter(this);
}

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

void FormHome::init()
{
    initWidget();

    setupConnections();
}

void FormHome::initWidget()
{
    m_login = new FormLogin();
    // 设置父窗口
    m_login->setParent(this);
    // 设置为独立窗口
    m_login->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    // 设置为应用程序的模态窗口，阻止本应用所有窗口的输入
    m_login->setWindowModality(Qt::WindowModal);

    m_formMap[STANDBY]              = new FormStandby();
    m_formMap[SETTINGS]             = new FormSettings();
    m_formMap[GENERAL_SETTINGS]     = new FormGeneralSettings();
    m_formMap[DEVICE_INFORMATION]   = new FormDeviceInfo();
    m_formMap[CONFIGURATION_KEY]    = new FormConfigurationKey();
    m_formMap[CHARGING_RECORDS]     = new FormChargeRecords();
    m_formMap[FIRMWARE_INFORMATION] = new FormFirmwareInfo();
    m_formMap[IMPORT_EXPORT]        = new FormTransfer();
    m_formMap[LOG_VIEW]             = new FormLogView();
    m_formMap[PAYMENT]              = new FormPayment();

    // 添加页面到堆叠窗口并隐藏
    foreach (const auto& widget, m_formMap.values()) {
        ui->workspace->addWidget(widget);

        // 强制布局系统更新
        ui->workspace->updateGeometry();
    }

    // 显示初始页面
    showForm(STANDBY);
}

void FormHome::setupConnections()
{
    // 自定义标题栏 settings 按钮点击事件
    connect(ui->titleBar, &TitleBar::showLogin, this, [=]() {
        m_login->show();
    });

    // 自定义标题栏 return 按钮点击事件
    connect(ui->titleBar, &TitleBar::signalReturn, this, [this]() {
        setPrevForm();          // 设置前一页面为 STANDBY
        showForm(m_prevForm);   // 需要维护历史记录
    });

    // 显示 settings 界面事件
    connect(m_login, &FormLogin::showSetings, this, [=]() { showForm(SETTINGS); });

    // 设置页面信号统一处理
    auto settings = qobject_cast<FormSettings*>(m_formMap[SETTINGS]);
    const QVector<QPair<void (FormSettings::*)(), workspace_form_enum>> settingsSignals = {
        {&FormSettings::showGeneralSettings, GENERAL_SETTINGS},
        {&FormSettings::showDeviceInformation, DEVICE_INFORMATION},
        {&FormSettings::showConfigurationKey, CONFIGURATION_KEY},
        {&FormSettings::showChargingRecords, CHARGING_RECORDS},
        {&FormSettings::showFirmwareInfo, FIRMWARE_INFORMATION},
        {&FormSettings::showTransfer, IMPORT_EXPORT},
        {&FormSettings::showLogView, LOG_VIEW},
        {&FormSettings::showPayment, PAYMENT},
    };

    for (const auto& signal : settingsSignals) {
        connect(settings, signal.first, this, [=]() {
            m_prevForm = m_currentForm;
            showForm(signal.second);
        });
    }
}

void FormHome::showForm(workspace_form_enum form)
{
    if (!m_formMap.contains(form)) {
        return;
    }

    m_currentForm = form;
    ui->workspace->setCurrentWidget(m_formMap[form]);

    // 更新标题栏状态
    ui->titleBar->setSettingsEnable(form < SETTINGS);
    ui->titleBar->setReturnVisible(form != STANDBY);

    // 更新标题栏标题
    ui->titleBar->setTitle(m_formMap[form]->windowTitle());
}

void FormHome::setPrevForm()
{
    switch (m_currentForm) {
    case SETTINGS: m_prevForm = STANDBY; break;
    default:
        break;
    }
}

