#include "MainWindow.h"

#include <chrono>
#include <sstream>
#include <iomanip>
static std::string DatetimeNow(char c1 = '-', char c2 = ':', char c3 = ' ')
{
    std::stringstream ss;
    ss << "%Y" << c1 << "%m" << c1 << "%d" << c3 << "%H" << c2 << "%M" << c2 << "%S";
    std::string fmt = ss.str();
    ss.str("");
    auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    ss << std::put_time(std::localtime(&t), fmt.c_str());
    return ss.str();
}

static const std::string STY_BTN_MENU =
"QPushButton{background:rgba(50,128,218,200);color:white;border-radius:5px;border:2px solid rgb(10,120,215);font:bold 18px;}"
"QPushButton:hover{background-color:rgb(32,164,128);}"
"QPushButton:pressed{border-width:3px 0 0 3px;background-color:rgb(150,150,150);border-style:inset;}"
"QPushButton:disabled{color:rgb(150,150,150);}";

static const std::string STY_BTN_MENU_ACTIVE =
"QPushButton{background:rgba(32,164,128,200);color:white;border-radius:5px;border:2px solid rgb(10,255,215);font:bold 18px;}"
"QPushButton:hover{background-color:rgb(32,164,128);}"
"QPushButton:pressed{border-width:3px 0 0 3px;background-color:rgb(150,150,150);border-style:inset;}"
"QPushButton:disabled{color:rgb(150,150,150);}";


void Menu::init(
    QWidget* parent, 
    int x, int y, 
    std::vector<std::string>& vecMenuId, 
    std::function<void(std::string id)> cb)
{
    int num = vecMenuId.size();
    x = (1920 - (num*110 - 10))*0.5;

    cb_ = cb;
    for (int i = 0; i<num; i++)
    {
        std::string menuName = vecMenuId[i];
        auto btn = std::make_shared<QPushButton>(parent);
        btn->setGeometry(x+i*110, y, 100, 35);
        btn->setText(menuName.c_str());
        btn->setFont(QFont("微软雅黑"));
        if (menuName == curMenuId_)
        {
            btn->setStyleSheet(STY_BTN_MENU_ACTIVE.c_str());
            activeBtn_ = btn.get();
        }
        else
        {
            btn->setStyleSheet(STY_BTN_MENU.c_str());
        }
        vecBtn_.push_back(btn);
        QObject::connect(btn.get(), &QPushButton::clicked, this, &Menu::onMenuBtnClicked);
    }
}    
void Menu::onMenuBtnClicked()
{
    QPushButton* btn = qobject_cast<QPushButton*>(sender());
    std::string menuName = btn->text().toStdString();

    if (menuName == curMenuId_)
    {
        return;
    }

    curMenuId_ = menuName;
    if (activeBtn_) { activeBtn_->setStyleSheet(STY_BTN_MENU.c_str()); }
    activeBtn_ = btn;
    activeBtn_->setStyleSheet(STY_BTN_MENU_ACTIVE.c_str());

    if (cb_) { cb_(curMenuId_); }
}

#include "WebHandler.h"
#include <QWebChannel>

MainWindow::MainWindow()
{
    if (1) 
    {
        this->initWebView();
        return;
    }
    
    QUI::label(labBkg_, this, 0, 0, 1920, 1080, "");
    labBkg_.setPixmap(QPixmap("assets/ui/bkg01.png"));

    // 顶部区域： 显示系统信息、时间、登录用户信息
    //QUI::label(labBkgHead_, this, 0, 0, 1920, 114, "");
    //labBkgHead_.setPixmap(QPixmap("assets/ui/bkg_head.png"));

    // 系统名称
    QUI::label(labTitle_, this, 100, 20, 520, 50, "", "font:bold 42px;color:white;");
    // 时钟标签
    QUI::label(labTime_, this, 50, 25, 320, 40, DatetimeNow().c_str(), "");
    labTime_.setFont(QFont("微软雅黑", 16, 1000));

    //wPage_ = std::make_shared<WidgetPageHome>(this);
    //wPage_->setGeometry(0, 115, 1920, 1080-115);
    //wPage_->show();

    this->initMenu();

    // 登录框
    wLogin_.setParent(this);
    wLogin_.show();
    
    connect(&wLogin_, &WidgetLogin::signalLoginSuccess, this, &MainWindow::slotLoginSuccess);

    QObject::connect(&timer_, &QTimer::timeout, this, &MainWindow::onTimeout);
    timer_.start(1000);
}

void MainWindow::initWebView()
{
    webView_ = std::make_shared<QWebEngineView>(this);

    MyWebHandler* myWebHandler = new MyWebHandler();
    QWebChannel* webChannel = new QWebChannel();
    webChannel->registerObject("cppNative", myWebHandler);
    webView_->page()->setWebChannel(webChannel);

    webView_->setGeometry(0, 0, 1920, 1080);
    // 默认设置透明, 解决加载时的白屏闪烁
    webView_->page()->setBackgroundColor(Qt::transparent);

    webView_->setContextMenuPolicy(Qt::NoContextMenu);
    //webView.load(QUrl("https://www.baidu.com"));
    webView_->load(QUrl("file:///assets/html/main.html"));
    //connect(wWebView.get(), &QWebEngineView::loadFinished, this, &MyWidget::slotLoadFinished);

    //std::string htmlContent = "HelloWorld";
    //webView->setHtml(htmlContent.c_str(), QUrl("file:///assets/html/"));
    webView_->show();
}

void MainWindow::initMenu()
{
    std::vector<std::string> vecMenuId =
    {
        "系统总览", "运行监控", "预测监控", "统计分析", "安全管理", "系统管理", "服务管理", "参数管理"
    };

    menu_ = std::make_shared<Menu>();
    menu_->init(this, 740, 1030, vecMenuId, [=](std::string menuId) { this->onMenuClicked(menuId); });
    
    // 考虑处理二级菜单
}

void MainWindow::resizeEvent(QResizeEvent* event)
{
    auto& size = event->size();
    if (webView_)
    {
        webView_->resize(size);
    }
}

void MainWindow::slotLoginSuccess()
{
    wLogin_.hide();
    onMenuClicked("系统总览");
}

void MainWindow::onMenuClicked(std::string menuId)
{
    auto w = WidgetPage::create(this, menuId);
    if (w)
    {
        w->setGeometry(0, 90, 1920, 950);
        wPage_ = w;
        wPage_->show();
        //wPage_->updateData();
    }
    else
    {
        wPage_ = nullptr;
    }
}

void MainWindow::onTimeout()
{
    if (wPage_)
    {
        //wPage_->updateData();
    }
    labTime_.setText(DatetimeNow().c_str());
}