#include "FormGeneralSettings.h"
#include "ui_FormGeneralSettings.h"

#include <QScroller>

#include "LanguageManager.h"
#include "DataTypeDef.h"
#include "GeneralInterface.h"

// General Settings Form
#include "languageCfg/FormLanguageConfig.h"
#include "passwordManage/FormPasswordManage.h"
#include "chargingSetup/FormChargingSetup.h"
#include "dwellTimeSet/FormDwellTimeSet.h"
#include "ocppCfg/FormOcppConfig.h"
#include "screenDrightnessSet/FormScreenDrightnessSet.h"
#include "workMode/FormWorkMode.h"

FormGeneralSettings::FormGeneralSettings(BaseWidget *parent)
    : BaseWidget(parent)
    , ui(new Ui::FormGeneralSettings)
{
    // 设置用户界面
    ui->setupUi(this);    

    // 设置触摸滚动区域
    GeneralInterface::setTouchScroller(ui->scrollArea);

    // 初始化小部件
    initWidget();
    // 初始化命令链接按钮
    initCommandLinkButton();
    // 设置连接
    setupConnections();
}

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

bool FormGeneralSettings::returnParent()
{
    return true;
}

void FormGeneralSettings::slotReturnPrevForm()
{
    BaseWidget *widget = qobject_cast<BaseWidget *>(ui->stackedWidget->currentWidget());
    if (!widget) return;
    if (widget->returnParent()) {
        showForm(prevForm());
    }
}

void FormGeneralSettings::initWidget()
{
    // setCurrentForm(Workspace::Settings::GENERAL_SETTINGS);
    // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
    ui->general->setWindowTitle(tr("General Settings"));

    // m_formMap[Workspace::Settings::GENERAL_SETTINGS]            = ui->general;
    m_formMap[Workspace::Settings::General::LANGUAGE]           = new FormLanguageConfig();
    m_formMap[Workspace::Settings::General::ADMIN_PWD]          = new FormPasswordManage();
    m_formMap[Workspace::Settings::General::CHARGING_SETUP]     = new FormChargingSetup();
    m_formMap[Workspace::Settings::General::DWELL_TIME]         = new FormDwellTimeSet();
    m_formMap[Workspace::Settings::General::SCREEN_BRIGHTNESS]  = new FormScreenDrightnessSet();
    m_formMap[Workspace::Settings::General::NETWORK_SETUP]      = new FormOcppConfig();
    m_formMap[Workspace::Settings::General::WORK_MODE]          = new FormWorkMode();

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

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

    // 显示初始页面
    // showForm(Workspace::Settings::GENERAL_SETTINGS);
}

void FormGeneralSettings::initCommandLinkButton()
{
    ui->language->setMainText(tr("Language"));
    ui->language->setDescriptionText(tr("Multiple Language Selection"));

    ui->admin_pwd_setting->setMainText(tr("Admin Password"));
    ui->admin_pwd_setting->setDescriptionText("");

    ui->charging_setup->setMainText(tr("Charging setup"));
    ui->charging_setup->setDescriptionText("");

    ui->dwell_time_seting->setMainText(tr("Page dwell time setting"));
    ui->dwell_time_seting->setDescriptionText(tr("Set the duration to stay on a page when no user interaction"));

    ui->screen_brightness_set->setMainText(tr("Screen brightness setting"));
    ui->screen_brightness_set->setDescriptionText(tr("Set the screen brightness when charging. no user interaction etc"));

    ui->network_setup->setMainText(tr("Network Setup"));
    ui->network_setup->setDescriptionText(tr("The network functionality may be limited here. it is recommended to configure the network via android setting"));

    ui->work_mode->setMainText("Work Mode");
    ui->work_mode->setDescriptionText("");
}

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

    connect(ui->language,               &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::LANGUAGE);
    });
    connect(ui->admin_pwd_setting,      &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::ADMIN_PWD);
    });
    connect(ui->charging_setup,         &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::CHARGING_SETUP);
    });
    connect(ui->dwell_time_seting,      &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::DWELL_TIME);
    });
    connect(ui->screen_brightness_set,  &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::SCREEN_BRIGHTNESS);
    });
    connect(ui->network_setup,          &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::NETWORK_SETUP);
    });
    connect(ui->work_mode,              &CustomCommandLinkButton::clicked,  this, [this]() {
        // setPrevForm(Workspace::Settings::GENERAL_SETTINGS);
        showForm(Workspace::Settings::General::WORK_MODE);
     });
}

void FormGeneralSettings::showForm(int form)
{
    if (!m_formMap.contains(form) || !m_formMap[form]) {
        return;
    }

    setCurrentForm(form);
    ui->stackedWidget->setCurrentWidget(m_formMap[form]);

    // 更新标题栏标题
    emit refreshTitles(m_formMap[form]->windowTitle());
}
