#include "MaskPageStat.h"
#include "pv/PvTable.h"
#include "pv/PvChart.h"

static int CreatePanel(PARAM* p, int parentId, int x, int y, int w, int h, std::string title)
{
    int id = PvApp::label(p, parentId, x, y, w, h, "", QSS::LABEL_BKG_2);
    PvApp::label(p, id, 10, 10, w, 20, title, QSS::STYLE_TITLE_ICON);
    PvApp::label(p, id, 20, 30, w, 2, "", QSS::QSS_UNDERLINE);
    return id;
}

MaskPageStat::MaskPageStat(PARAM* p) : PvMask(p)
{
}

struct StatChartInfo
{
    std::string name;
    std::vector<std::string> vecAxis;
    std::vector<std::pair<std::string, int>> vecItems;
    StatChartInfo(std::string name, std::vector<std::string> axis, std::vector<std::pair<std::string, int>> items) : name(name), vecAxis(axis), vecItems(items) {};
};
using VecChartDef = std::vector<StatChartInfo>;
static VecChartDef chartStorageDef =
{
    StatChartInfo {"充放电分析", {"电量(kWh)"}, {{"充电电量", 0}, { "放电电量", 0 }}},
    StatChartInfo {"运行状态分析", {"时长(h)", "次数"}, {{"充电时长", 0}, {"放电时长", 0}, {"故障次数", 0}}},
    StatChartInfo {"电压/电流分析", {"电压(V)", "电流(A)"}, {{"电压", 0}, {"电流", 0}}},
    StatChartInfo {"功率分析", {"功率(kW)"}, {{"功率", 0}}}
};
static VecChartDef chartSolarDef =
{
    StatChartInfo {"发电电量分析", {"电量(kWh)"}, {{"发电电量", 0}}},
    StatChartInfo {"运行状态分析", {"时长(h)", "次数"}, {{"发电时长", 0}, {"故障次数", 0}}},
    StatChartInfo {"电压/电流分析", {"电压(V)", "电流(A)"}, {{"电压", 0}, {"电流", 0}}},
    StatChartInfo {"功率分析", {"功率(kW)"}, {{"功率", 0}}}
};
static VecChartDef chartChargeDef =
{
    StatChartInfo {"充电分析", {"电量(kWh)"}, {{"充电电量", 0}}},
    StatChartInfo {"运行状态分析", {"时长(h)", "次数"}, {{"充电时长", 0}, {"充电次数", 0}, {"故障次数", 0}}},
    StatChartInfo {"电压/电流分析", {"电压(V)", "电流(A)"}, {{"电压", 0}, {"电流", 0}}},
    StatChartInfo {"功率分析", {"功率(kW)"}, {{"功率", 0}}}
};
using VecStatDef = std::vector<std::pair<std::string, VecChartDef>>;
static VecStatDef statDef = {
    {"储能设备运行分析", chartStorageDef},
    {"光伏设备运行分析", chartSolarDef},
    {"充电设备运行分析", chartChargeDef}
};

int MaskPageStat::initUI(EPvCode pvcode)
{
    PvApp::label(p, PV_ID_MAIN, 10, 100, 1900, 850, "", QSS::LABEL_BKG_1);

    if (pvcode == EPvCode::MASK_STAT) { pvcode = EPvCode::MASK_STAT_STORAGE; }
    std::string curModuleName;
    VecChartDef chartDef {};

    for (int i = 0; i<statDef.size(); ++i)
    {
        std::string moduleName = statDef[i].first;
        // 创建按钮
        bool isActive = (PvApp::getPvCode(moduleName) == pvcode);
        int pageBtn = PvApp::button(p, PV_ID_MAIN, 10+(i*190), 110, 180, 40, moduleName, isActive ? QSS::QSS_BTN_MGR_ACTIVE : QSS::QSS_BTN_MGR);
        mapSubpage_[pageBtn] = moduleName;
        if (isActive) 
        { 
            curModuleName = moduleName; 
            chartDef = statDef[i].second;
        }
    }

    // 初始化统计图表
    if (!chartDef.empty())
    {
        int x=10, y=160, w=467, h=300, delta = 32;
        for (int i = 0; i<chartDef.size(); ++i)
        {
            auto& chartInfo = chartDef[i];
            std::string title = chartInfo.name;
            auto& vecAxis = chartInfo.vecAxis;
            auto& vecItems = chartInfo.vecItems;

            int panel = CreatePanel(p, 0, x+(w+10)*i, y, w, h, title);
            if (i == 0) 
            {
                chart1 = std::make_shared<PvChartBar>(p, panel, 0, delta, w, h-delta);
                this->initChartBar(chart1, vecAxis, vecItems);
            }
            else if (i == 1) 
            {
                chart2 = std::make_shared<PvChartBar>(p, panel, 0, delta, w, h-delta);
                this->initChartBar(chart2, vecAxis, vecItems);
            }
            else if (i == 2) 
            {
                chart3 = std::make_shared<PvChartCurve>(p, panel, 0, delta, w, h-delta);
                chart3->setBackground(p, 8, 54, 91);
                this->initChartCurve(chart3, vecAxis, vecItems);
            }
            else if (i == 3) 
            {
                chart4 = std::make_shared<PvChartCurve>(p, panel, 0, delta, w, h-delta);
                chart4->setBackground(p, 8, 54, 91);
                this->initChartCurve(chart4, vecAxis, vecItems);
            }
        }
    }

    // 初始化表格
    {
        PvTable::Options option;
        auto table = new PvTable(p, 0, 10, 480, 1900, 12, option);
        table->addHead({"日期", "设备ID", "设备名称", "设备类型", "充电次数", "放电次数", "故障次数"});
    }

    //
    {

    }
    
    return 0;
}

void MaskPageStat::initChartBar(std::shared_ptr<PvChartBar> chart, std::vector<std::string>& vecAxis, std::vector<std::pair<std::string, int>>& vecItems)
{
    if (!chart) { return; }
    for (int pos = 0; pos<vecAxis.size(); ++pos)
    {
        auto text = vecAxis[pos];
        if (pos == 0) chart->setLabelYLeft(text);
        if (pos == 1) chart->setLabelYRight(text);
    }
    for (int index = 0; index<vecItems.size(); ++index)
    {
        chart->insertItem(vecItems[index].first);
    }
}

void MaskPageStat::initChartCurve(std::shared_ptr<PvChartCurve> chart, std::vector<std::string>& vecAxis, std::vector<std::pair<std::string, int>>& vecItems)
{
    if (!chart) { return; }
    for (int pos = 0; pos<vecAxis.size(); ++pos)
    {
        auto text = vecAxis[pos];
        if (pos == 0) chart->setLabelYLeft(text);
        if (pos == 1) chart->setLabelYRight(text);
    }
    for (int index = 0; index<vecItems.size(); ++index)
    {
        chart->insertItem(vecItems[index].second, vecItems[index].first, 100+80*index, 10, 80);
    }
}

EPvCode MaskPageStat::onEventButton(int pvid)
{
    std::string title;
    auto iter = mapSubpage_.find(pvid);
    if (iter != mapSubpage_.end())
    {
        title = iter->second;
        return PvApp::getPvCode(title);
    }
    return EPvCode::NUL;
}