#include "MainApp.h"
#include "common/Spdlogger.h"


#include "app/Config.h"
#include "common/Utils.h"
#include "MyQUI.h"
#include "app/Application.h"
#include "app/AppData.h"
#include "app/Station.h"
#include "protocol/MqttEntity.h"

MainApp::MainApp()
{
    // 设置背景色
    QPalette palette(this->palette());
    palette.setColor(QPalette::Window, QColor(14, 35, 74));
    this->setAutoFillBackground(true);
    this->setPalette(palette);
    this->setStyleSheet("color: white;");
    this->setFixedSize(1600, 900);
    this->show();
    this->setAttribute(Qt::WA_StyledBackground, true); // 启用样式表背景绘制

    widgetMenu = MyQUI::Widget(this, "menu", "background-color:rgba(120,120,120,30); border-radius:5px;");
    widgetWorkspace = MyQUI::Widget(this, "workspace", "background-color:rgba(120,120,120,30); border-radius:5px;");

    this->initMenu();
    this->setMyLayout();
    this->setActiveMenu("系统总览");

    timer = std::make_shared<QTimer>(this);
    QObject::connect(timer.get(), &QTimer::timeout, this, &MainApp::onTimer);
    timer->start(1000); // 每隔1000毫秒更新一次
}

MainApp::~MainApp()
{
}


QPushButton* MainApp::addMenuItem(int index, string itemName)
{
    auto itemBtn = new QPushButton(itemName.c_str());
    itemBtn->setStyleSheet(QSS_BTN_MENU.c_str());
    //itemBtn->setFont(QFont("微软雅黑", 16, 600));
    mapMenuBtn[itemName] = itemBtn;
    QObject::connect(itemBtn, &QPushButton::clicked, this, [=]() { this->setActiveMenu(itemName); });

    QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    itemBtn->setSizePolicy(sizePolicy);

    layoutMenu->addWidget(itemBtn, index, 0, 1, 1);
    // 设置列宽和行高
    layoutMenu->setRowMinimumHeight(index, 50); // 设置第0列的最小宽度为100像素
    // 设置列和行的伸缩因子
    layoutMenu->setRowStretch(index, 0); // 设置第0列的伸缩因子为0，不伸缩
    layoutMenu->setRowStretch(index + 1, 2); // 设置第1列的伸缩因子为2，使其更宽
    return itemBtn;
}

void MainApp::initMenu()
{
    layoutMenu = new QGridLayout(widgetMenu.get());
    layoutMenu->setSpacing(2);
    layoutMenu->setContentsMargins(2, 2, 2, 2);

    labelDT = new QLabel(); // 由其父对象（layout）自动释放
    labelDT->setAlignment(Qt::AlignCenter);
    labelDT->setFont(QFont("优设标题黑", 18));
    this->setLabelDateTime();
    
    layoutMenu->addWidget(labelDT, 0, 0, 1, 1);
    layoutMenu->setRowMinimumHeight(0, 80); // 设置第0列的最小高度
    layoutMenu->setRowStretch(0, 0); // 设置第0列的伸缩因子为0，不伸缩

    std::vector<std::string > menuItems = {"系统总览", "运行监控", "统计分析", "系统管理"};
    for (int i=0; i< menuItems.size(); ++i)
    {
        auto itemBtn = this->addMenuItem(i + 1, menuItems[i]);
    }
    widgetMenu->show();
}

void  MainApp::setActiveMenu(string menuName)
{
    auto iter = mapMenuBtn.find(menuName);
    if (iter == mapMenuBtn.end()) return;

    auto itemBtn = iter->second;
    if (curActiveMenuBtn == itemBtn) { return; }
    if (curActiveMenuBtn) { curActiveMenuBtn->setStyleSheet(QSS_BTN_MENU.c_str()); }
    curActiveMenuBtn = itemBtn;
    if (curActiveMenuBtn)
    {
        curActiveMenuBtn->setStyleSheet(QSS_BTN_MENU_ACTIVE.c_str());

        std::shared_ptr<MyWidget> widget = getWorkWidget(menuName);
        if (widget == curActiveWidget) { return; }
        if (curActiveWidget)
        {
            curActiveWidget->hide();
        }
        if (widget)
        {
            widget->setParent(widgetWorkspace.get());
            widget->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
            widget->show();
        }
        curActiveWidget = widget;
    }
}


void MainApp::setMyLayout()
{
    layoutMain = new QGridLayout(this);
    layoutMain->setObjectName(QString::fromUtf8("layout.main"));
    layoutMain->setSpacing(6);
    layoutMain->setContentsMargins(11, 11, 11, 11);
    //// 设置间距和边距
    //layout->setHorizontalSpacing(10); // 设置列间距为10像素
    //layout->setVerticalSpacing(10); // 设置行间距为10像素
    //layout->setContentsMargins(10, 10, 10, 10); // 设置内容边距为10像素

    // 布局：左侧菜单widget
    widgetMenu->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    
    layoutMain->addWidget(widgetMenu.get(), 0, 0, 1, 1);
    // 布局：右侧工作区widget
    widgetWorkspace->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    layoutMain->addWidget(widgetWorkspace.get(), 0, 1, 1, 1);

    // 设置列宽和行高
    layoutMain->setColumnMinimumWidth(0, 200); // 设置第0列的最小宽度为100像素
    //gridLayout->setRowMinimumHeight(0, 200); // 设置第0行的最小高度为50像素

    // 设置列和行的伸缩因子
    layoutMain->setColumnStretch(0, 0); // 设置第0列的伸缩因子为0，不伸缩
    layoutMain->setColumnStretch(1, 2); // 设置第1列的伸缩因子为2，使其更宽
}

std::shared_ptr<MyWidget> MainApp::getWorkWidget(std::string name)
{
    auto iter = mapWorkWidgets.find(name);
    if (iter != mapWorkWidgets.end())
    {
        return iter->second;
    }
    std::shared_ptr<MyWidget> widget {nullptr};
    if (name == "系统总览") { widget = make_shared<QWHome>(nullptr); }
    else if(name == "运行监控") { widget = make_shared<QWMonitor>(nullptr); }
    else if (name == "统计分析") { widget = make_shared<QWStatistics>(nullptr); }
    //else if (name == "预测管理") { widget = make_shared<QWPredict(nullptr); }
    else if (name == "系统管理") { widget = make_shared<QWSysmgr>(nullptr); }
    if (widget) { mapWorkWidgets[name] = widget; }
    return widget;
}

void MainApp::setWorkspaceWidget(std::shared_ptr<MyWidget> widget)
{
    widgetWorkspace = widget;
}

void MainApp::setLabelDateTime()
{
    string curDateTime = Utils::timeStr();
    curDateTime[10] = '\n';
    labelDT->setText(curDateTime.c_str());
}

void MainApp::onTimer()
{
    this->setLabelDateTime();

    if (curActiveWidget && curActiveWidget->isVisible())
    {
        curActiveWidget->onTimer();
    }
}