#include "MaskMain.h"
#include "pv/pages/MaskPageHome.h"
#include "pv/pages/MaskPageRunning.h"
#include "pv/pages/MaskPageForecast.h"
#include "pv/pages/MaskPageStat.h"
#include "pv/pages/MaskPageSysmgr.h"

#include "common/Fields.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
std::string GetDateTimeWeekday()
{
    static std::vector<std::string> wdaysStr = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    std::time_t now = std::time(nullptr);
    std::tm tm = *std::localtime(&now);
    std::stringstream ss;
    ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S ") << wdaysStr[tm.tm_wday];
    return ss.str();
}

int MaskMain::initUI()
{
    //pvSetStyleSheet(p, PV_ID_MAIN, "color: white; font: normal 14px \"微软雅黑\";");
    ui.bkg = PvApp::pvid(p);
    PvApp::label(p, PV_ID_MAIN, 0, 0, 1920, 1080, "", "background-color: rgb(1, 32, 54)");
    PvApp::image(p, 0, 0, 0, 1920, 90, "bkgHead.png");

    ui.datetime = PvApp::label(p, PV_ID_MAIN, 10, 30, 420, 30, GetDateTimeWeekday(), QSS::label(20));
    pvSetAlignment(p, ui.datetime, AlignCenter);

    int idStationTitle = PvApp::label(p, 0, 620, 0, 660, 90, "能源站监控与运行管理系统", QSS::label(48));
    pvSetAlignment(p, idStationTitle, AlignCenter);

    // 初始化子页面
    int menuActive = PV_ID_NUL;
    switch (pvcode_)
    {
    case EPvCode::NUL:
    case EPvCode::MASK_LOGIN: {}
    case EPvCode::MASK_HOME: { maskPage_ = std::make_shared<MaskPageHome>(p); }
        break;
    case EPvCode::MASK_RUNNING: { maskPage_ = std::make_shared<MaskPageRunning>(p); }
        break;
    case EPvCode::MASK_FORECAST_STORAGE:
    case EPvCode::MASK_FORECAST_SOLAR:
    case EPvCode::MASK_FORECAST_CHARGE:
    case EPvCode::MASK_FORECAST: { maskPage_ = std::make_shared<MaskPageForecast>(p); }
        break;
    case EPvCode::MASK_STAT_STORAGE:
    case EPvCode::MASK_STAT_SOLAR:
    case EPvCode::MASK_STAT_CHARGE:
    case EPvCode::MASK_STAT: { maskPage_ = std::make_shared<MaskPageStat>(p); }
        break;
    case EPvCode::MASK_SYSMGR:
    case EPvCode::MASK_MGR_USER: 
    case EPvCode::MASK_MGR_ROLE: 
    case EPvCode::MASK_MGR_PERMISSION: 
    case EPvCode::MASK_MGR_STATION: 
    case EPvCode::MASK_MGR_DEVICE: 
    case EPvCode::MASK_MGR_POLICY: 
    case EPvCode::MASK_MGR_SYSLOG: 
    case EPvCode::MASK_MGR_ALERTLOG: { maskPage_ = std::make_shared<MaskPageSysmgr>(p); }
        break;
    default: { maskPage_ = std::make_shared<MaskPageHome>(p); }
        break;
    }
    if (maskPage_)
    {
        maskPage_->initUI(pvcode_);
    }

    std::vector<std::string> vecMenuItems = {"系统总览", "运行监控", "预测管理", "统计分析", "系统管理"};
    {
        int n = vecMenuItems.size();
        int y=980, w = 100, margin = 10, h=40;
        int x0 = (1920 - (w*n + margin*(n-1))) * 0.5;
        for (int i = 0; i<vecMenuItems.size(); ++i)
        {
            std::string& title = vecMenuItems[i];
            EPvCode statusTmp = PvApp::getPvCode(title);
            int x = x0 + (w+margin)*i;
            int id = PvApp::button(p, 0, x, y, w, h, title, (statusTmp == pvcode_) ? QSS::BTN_ACTIVE : QSS::BTN);
            mapMenuInfo_[id] = std::make_pair(title, statusTmp);
        }
    }
    return 0;
}

std::shared_ptr<PvMask> MaskMain::createSubPage(int pvid)
{
    return nullptr;
}

EPvCode MaskMain::onEventNull(int pvid)
{
    pvSetText(p, ui.datetime, GetDateTimeWeekday().c_str());
    return EPvCode::NUL;
}

EPvCode MaskMain::onEventButton(int pvid)
{
    EPvCode pvcode = EPvCode::NUL;
    auto iter = mapMenuInfo_.find(pvid);
    if (iter != mapMenuInfo_.end())
    {
        return iter->second.second;
    }
    else
    {
        if (maskPage_) 
        { 
            pvcode = maskPage_->onEventButton(pvid);
        }
    }
    return pvcode;
}