#include "MaskPageHome.h"
#include "common/Utils.h"
#include "app/Application.h"
#include "app/Station.h"

#include "pv/PvTable.h"
#include "pv/PvChart.h"
#include "pv/PvPopWidget.h"
#include "pv/pages/PanelPolicy.h"


void TestPage(PARAM* p)
{
    //auto& appdata = Application::data();
    //auto pagination = new PvPagination(p, 0, 600, 160, 20);
    //pagination->setPage(5, 10);
}


static int CreatePanel(PARAM* p, int parent, int x, int y, int w, int h, std::string title)
{
    int panelId = PvApp::label(p, parent, x, y, w, h, "", QSS::LABEL_BKG_1);
    int titleId = PvApp::label(p, panelId, 10, 8, w, 22, title, QSS::STYLE_TITLE_ICON);
    PvApp::label(p, panelId, 20, 28, w, 2, "", QSS::QSS_UNDERLINE);
    return panelId;
}

static int CreatePanel1(PARAM* p, int parent, int x, int y, int w, int h, std::string title)
{
    int panelId = PvApp::label(p, parent, x, y, w, h, "");
    PvApp::image(p, panelId, 0, 13, 500, 17, "bkgBox.png");
    PvApp::label(p, panelId, 20, 0, w-20, 30, title);
    return panelId;
}

static int CreateCard1(PARAM* p, int parentId, int x, int y, int w, int h, std::string title, std::string val)
{
    int id = PvApp::label(p, parentId, x, y, w, h, "", QSS::LABEL_BKG_1);
    int idTitle = PvApp::label(p, id, 0, h*0.5, w, h*0.5, title, "background:transparent; font: bold 28px;");
    int idVal = PvApp::label(p, id, 0, 0, w, h*0.5, val, "background:transparent; font: bold 28px; color:rgb(77,215,240);");
    pvSetAlignment(p, idTitle, AlignCenter);
    pvSetAlignment(p, idVal, AlignHCenter | AlignBottom);
    return id;
}

static int CreateCard2(PARAM* p, int parent, int x, int y, int w, int h, std::string title, std::string val)
{
    int id = PvApp::label(p, parent, x, y, w, h, "", QSS::LABEL_BKG_1);

    int idTitle = PvApp::label(p, id, 0, 0, w, h*0.5, title);
    int idVal = PvApp::label(p, id, 0, h*0.5, w, h*0.5, val, "background:transparent; font: bold 16px; color:rgb(77,215,240);");
    pvSetAlignment(p, idTitle, AlignCenter);
    pvSetAlignment(p, idVal, AlignCenter);
    return id;
}

static int CreateBox(PARAM* p, int parent, int x, int y, int w, int h, std::string k, std::string val="")
{
    int id = PvApp::label(p, parent, x, y, w, h, "", "border-radius:0px; background-color: rgb(7, 45, 66); border: 1px solid rgb(27, 88, 105);");
    {
        int len = 10;
        std::string QSS = "background-color: transparent; border: 1px solid rgb(0, 218, 216);";
        PvApp::label(p, id, 0, 0, len, len, "", QSS + "border-width: 2px 0 0 2px");
        PvApp::label(p, id, w-len, 0, len, len, "", QSS + "border-width: 2px 2px 0 0");
        PvApp::label(p, id, w-len, h-len, len, len, "", QSS + "border-width: 0 2px 2px 0");
        PvApp::label(p, id, 0, h-len, len, len, "", QSS + "border-width: 0 0 2px 2px");
    }
    // "border:none; background-color: transparent; font: bold 14px; padding-bottom: 0px;"
    int titleId = PvApp::label(p, id, 0, 0, w, h*0.5-2, k);
    int valId = PvApp::label(p, id, 0, h*0.5+2, w, h*0.5-2, val, QSS::label(16, "rgb(77, 215, 240)"));
    pvSetAlignment(p, titleId, AlignHCenter | AlignBottom);
    pvSetAlignment(p, valId, AlignHCenter | AlignTop);
    return valId;
}

static int CreateIconBox(PARAM* p, int parent, int x, int y, std::string k, std::string val, std::string icon)
{
    //PvApp::label(p, parent, x, y, 50, 50, "", "background-color: rgb(2, 41, 60); border: 1px solid rgb(61, 254, 250);");
    //PvApp::label(p, parent, x+3, y+3, 44, 44, "", "background-color: transparent; border: 2px solid rgb(31, 91, 139);");
    PvApp::image(p, parent, x, y, 50, 50, icon.c_str());
    PvApp::label(p, parent, x+52, y, 100, 20, k, "background-color: transparent; border: none; font: bold 14px;");
    int valId = PvApp::label(p, parent, x+52, y+25, 100, 20, val, "background-color: transparent; border: none; font: bold 14px;");
    return valId;
}

class PopStation
{
public:
    PARAM* p {};
    int widget = PV_ID_NUL;
    int width = 1100;
    int height = 800;
    int labelTitle = PV_ID_NUL;

    PopStation(PARAM* p) : p(p)
    {
        int wMask = 1920, hMask = 960;
        widget = PvApp::widget(p, PV_ID_MAIN, 0, 0, wMask, hMask);
        PvApp::label(p, widget, 0, 0, 1920, 960, "", "background-color: rgb(1, 32, 54);");

        // 站名称Title
        labelTitle = PvApp::label(p, widget, 0, 40, wMask, 30, "", "border: none; background-color: transparent; font: bold 20px;");
        pvSetAlignment(p, labelTitle, AlignCenter);

        {
            int pid = PvApp::label(p, widget, (wMask-width)*0.5, (hMask-height)*0.5, width, height, "", "background-color: rgb(2, 58, 87); border: 1px solid rgb(31, 198, 255);");
            PvApp::label(p, pid, 0, 0, 80, height, "", "background-color: transparent; border: 3px solid rgb(31, 198, 255); border-width: 3px 0 3px 3px;");
            PvApp::label(p, pid, width-80, 0, 80, height, "", "background-color: transparent; border: 3px solid rgb(31, 198, 255); border-width: 3px 3px 3px 0;");

            int btnClose = PvApp::button(p, pid, width-40, 5, 30, 30, "✕", "background-color: white; border: none; border-radius: 15px; color: rgb(2, 59, 87); font: bold 20px;");
            PvApp::bind(p, PvEvent::BUTTON_EVENT, btnClose, [=](std::string) {
                pvHide(p, widget);
            });

            int x = 20, y = 40, w = 500, h = 240;
            {
                int box = CreatePanel1(p, pid, x, y, w, h, "预制舱信息");
                int x0 = 50, y0 = 40, w0 = 150, h0 = 60;
                CreateBox(p, box, x0 = 60, y0, w0, h0, "电池类型", "磷酸铁锂电池");
                CreateBox(p, box, w-x0-w0, y0, w0, h0, "冷却模式", "风冷");
                CreateBox(p, box, x0 = 20, y0 += (h0+5), w0, h0, "运行模式", "最优经济化");
                CreateBox(p, box, w-x0-w0, y0, w0, h0, "电池额定电压", "300 V");
                CreateBox(p, box, x0 = 60, y0 += (h0+5), w0, h0, "PCS额定功率", "100 kW");
                CreateBox(p, box, w-x0-w0, y0, w0, h0, "电池储能容量", "100 kWh");
            }
            {
                y += (h+10);
                int box = CreatePanel1(p, pid, x, y, w, h, "运行与统计信息");

                CreateIconBox(p, box, 40, 40, "舱内温度", "20 ℃", "icon1.png");
                CreateIconBox(p, box, 40+160, 40, "舱内湿度", "30 %", "icon2.png");
                CreateIconBox(p, box, 40+160*2, 40, "电压", "200 V", "icon3.png");
                CreateIconBox(p, box, 40, 90, "电流", "30 A", "icon4.png");
                CreateIconBox(p, box, 40+160, 90, "功率", "6 kW", "icon5.png");
                CreateIconBox(p, box, 40+160*2, 90, "功率因数", "1.0", "icon5.png");

                int x0 = 10, y0 = 160, w0 = 90, h0 = 60;
                CreateBox(p, box, x0, y0, w0, h0, "场站运行天数", "365 天");
                CreateBox(p, box, x0 += (w0+10), y0, w0, h0, "储能充电电量", "22 kWh");
                CreateBox(p, box, x0 += (w0+10), y0, w0, h0, "储能放电电量", "11 kWh");
                CreateBox(p, box, x0 += (w0+10), y0, w0, h0, "场站累计收益", "123.12 元");
                CreateBox(p, box, x0 += (w0+10), y0, w0, h0, "设备利用率", "10 %");
            }
            {
                y += (h+10);
                int box = CreatePanel1(p, pid, x, y, w, h, "环境信息");

                CreateIconBox(p, box, 80, 50, "光照强度", "27.1 Lux", "icon1.png");
                CreateIconBox(p, box, 80+200*1, 50, "风速", "1.2 m/s", "icon2.png");
                CreateIconBox(p, box, 80, 120, "环境温度", "35.7 ℃", "icon1.png");
                CreateIconBox(p, box, 80+200*1, 120, "环境湿度", "31.3 %", "icon2.png");
            }

            {
                x = 580; y = 40; h = 240;
                int box = CreatePanel1(p, pid, x, y, w, h, "储能充放电量");
                PvChartCurve chart(p, box, 0, 40, 500, 200);
                chart.setBackground(p, 2, 58, 87);
                chart.setLabelYLeft("电量（kWh）");
                chart.insertItem(0, "充电电量", 120, 10, 80);
                chart.insertItem(0, "放电电量", 200, 10, 80);
            }
            {
                y += (h+10);
                int box = CreatePanel1(p, pid, x, y, w, h, "场站收益");
                PvChartCurve chart(p, box, 0, 40, 500, 200);
                chart.setBackground(p, 2, 58, 87);
                chart.setLabelYLeft("电量（元）");
                chart.insertItem(0, "收益", 120, 10, 80);
            }
            {
                y += (h+10);
                int box = CreatePanel1(p, pid, x, y, w, h, "设备利用率");
                PvChartCurve chart(p, box, 0, 40, 500, 200);
                chart.setBackground(p, 2, 58, 87);
                chart.setLabelYLeft("利用率（%）");
                chart.insertItem(0, "利用率", 120, 10, 80);
            }
        }
    }

    void setStatus(std::string title)
    {
        pvSetText(p, labelTitle, title.c_str());
    }
    void show(int v)
    {
        v ? pvShow(p, widget) : pvHide(p, widget);
    }
};

//background-color: qlineargradient(x1:0, y1:0, x2:1, y2:0,stop:1 rgba(0,255,240,200),stop:0 rgba(0,255,240,0));
int MaskPageHome::initUI(EPvCode pvcode)
{
    int x = 10, y = 100, w = 500, h1 = 260, h2=290, h3=290;

    // 左侧区域： 上
    {
        int x0 = 0, y0=40, w0=160, h0=64; 
        int panel = CreatePanel(p, 0, x, y, w, h1, "运行总览");
        ui.labelRunDays = CreateBox(p, panel, x0 = 50, y0, w0, h0, "运行时间");
        ui.labelStationNum = CreateBox(p, panel, w-10-w0-x0, y0, w0, h0, "能源站数量");
        ui.labelEnergyCapacity = CreateBox(p, panel, x0 = 10, y0+=(h0+10), w0, h0, "储能总容量");
        ui.labelIncome = CreateBox(p, panel, w-10-w0-x0, y0, w0, h0, "累计收益");
        ui.labelStorageIn = CreateBox(p, panel, x0 = 50, y0 += (h0+10), w0, h0, "累计储能充电量");
        ui.labelStorageOut = CreateBox(p, panel, w-10-w0-x0, y0, w0, h0, "累计储能放电量");
    }

    // 左侧区域： 中
    {
        int panel = CreatePanel(p, 0, x, y+h1+10, w, h2, "储能设备");

        int x1 = 10;
        CreateCard2(p, panel, x1, 40, 235, 50, "今日充电电量", "0 kWh");
        CreateCard2(p, panel, x1+=245, 40, 235, 50, "今日放电电量", "0 kWh");

        auto bar = PvChartBar::create(p, panel, 10, 100, w-20, h2-100);
        bar->setLabelYLeft("电量(kWh)");
        bar->insertItem("日充电电量");
        bar->insertItem("日放电电量");
        bar->updateItem(0, {{"08/01", 10}, {"08/02", 20}, {"08/03", 30}, {"08/04", 40}, {"08/05", 50}, {"08/06", 60}, {"08/07", 70}});
        bar->updateItem(1, {{"08/01", 1}, {"08/02", 2}, {"08/03", 3}, {"08/04", 4}, {"08/05", 5}, {"08/06", 6}, {"08/07", 7}});
    }

    // 左侧区域： 下
    {
        int panel = CreatePanel(p, 0, x, y+h1+h2+20, w, h3, "光伏设备");

        int x1 = 10;
        CreateCard2(p, panel, x1, 40, 235, 50, "今日发电电量", "0 kWh");
        CreateCard2(p, panel, x1 += 245, 40, 235, 50, "今日入网电量", "0 kWh");

        auto bar = PvChartBar::create(p, panel, 10, 100, w-20, h3-100);
        bar->setLabelYLeft("电量(kWh)");
        bar->insertItem("日发电电量");
        bar->insertItem("日入网电量");
        bar->updateItem(0, {{"08/01", 10}, {"08/02", 20}, {"08/03", 30}, {"08/04", 40}, {"08/05", 50}, {"08/06", 60}, {"08/07", 70}});
    }

    // 右侧区域：上
    {
        int panel = CreatePanel(p, 0, x += (880+10+500+10), y, w = 500, h1, "运行分析");

    }
    // 右侧区域：中
    {
        int panel = CreatePanel(p, 0, x, y+h1+10, w = 500, h2, "充电设备");

        int x1 = 10;
        CreateCard2(p, panel, x1, 40, 235, 50, "今日充电电量", "0 kWh");
        CreateCard2(p, panel, x1 += 245, 40, 235, 50, "今日充电次数", "0 次");

        auto bar = PvChartBar::create(p, panel, 10, 100, w-20, h2-100);
        bar->setLabelYLeft("电量(kWh)");
        bar->insertItem("日充电电量");
        bar->insertItem("日充电次数");
        bar->updateItem(0, {{"08/01", 10}, {"08/02", 20}, {"08/03", 30}, {"08/04", 40}, {"08/05", 50}, {"08/06", 60}, {"08/07", 70}});

    }
    // 右侧区域：下
    {
        int panel = CreatePanel(p, 0, x, y+h1+h2+20, w = 500, h3, "告警分析");
         
        int x1 = 10;
        CreateCard2(p, panel, x1, 40, 153, 50, "今日储能告警", "0 次");
        CreateCard2(p, panel, x1 += 163, 40, 153, 50, "今日光伏告警", "0 次");
        CreateCard2(p, panel, x1 += 163, 40, 153, 50, "今日充电告警", "0 次");

        auto bar = PvChartBar::create(p, panel, 10, 100, w-20, h3-100);
        bar->setLabelYLeft("次数");
        bar->insertItem("日储能告警");
        bar->insertItem("日光伏告警");
        bar->insertItem("日充电告警");
        bar->updateItem(0, {{"08/01", 10}, {"08/02", 20}, {"08/03", 30}, {"08/04", 40}, {"08/05", 50}, {"08/06", 60}, {"08/07", 70}});
    }

    // 中间区域
    {
        int panel = PvApp::label(p, 0, x = 10+500+10, y, w = 880, h1+h2+h3+20, "", QSS::LABEL_BKG_1);

        ////// 饼图
        //int left = PvApp::widget(p, panel, 100, 100, 100, 100);
        //pvSetStyleSheet(p, left, "border: 1px solid gray; border-radius:50px;");

        //int id = PvApp::pvid();
        //pvDownloadFile(p, "red.png");
        //pvQImage(p, id, left, "");
        //pvSetGeometry(p, id, 10, 10, 100, 100);
        //pvSetImage(p, id, "red.png", 0);
        ////pvSetStyleSheet(p, id, "border-radius: 50px;");

        //int right = PvApp::label(p, panel, 200, 100, 100, 200, "", "border: 1px solid gray;");
        //std::string qss = "background: qlineargradient(x1:0, y1 : 0, x2 : 1, y2 : 1,  stop : 0 red, stop:1 blue); border-radius: 50px;";
        //std::string qss1 = "width: 200px;height: 200px;background: red;transform: rotate(45deg); border-radius: 100px;";
        //PvApp::label(p, left, 0, 0, 200, 200, "", qss1);

        PvApp::image(p, panel, 0, 20, 880, 800, "map.png");
        
        auto popStation = new PopStation(p);
        popStation->show(0);
        int btn = PvApp::button(p, panel, 10, 20, 100, 30, "场站一", QSS::BTN);
        PvApp::bind(p, PvEvent::BUTTON_EVENT, btn, [=](std::string) {
            popStation->setStatus("场站一");
            pvShow(p, popStation->widget);
        });

        PvApp::image(p, panel, 500, 420, 50, 50, "mapMarker.png");
        PvApp::image(p, panel, 420, 500, 50, 50, "mapMarker.png");
        PvApp::image(p, panel, 520, 532, 50, 50, "mapMarker.png");
        PvApp::image(p, panel, 363, 380, 50, 50, "mapMarker.png");
        PvApp::image(p, panel, 545, 564, 50, 50, "mapMarker.png");
    }

    this->updateUI();

    TestPage(p);

    return 0;
}

void MaskPageHome::updateUI()
{
    auto& appdata = Application::data();

    int stationNum = appdata.mapStation.size(); // 场站数量
    double energyCapacity {};                   // 储能容量
    double incomeTotal {};                      // 累计收益
    double electStorageIn {};                   // 储能充电电量
    double electStorageOut {};                  // 储能放电电量
    for (auto& item : appdata.mapStation)
    {
        auto& station = item.second;
        energyCapacity += station->energyCapacity;
        incomeTotal += station->incomeTotal;
    }

    pvSetText(p, ui.labelRunDays, "100 天");
    pvSetText(p, ui.labelStationNum, (Utils::toStr(stationNum) + " 个").c_str());
    pvSetText(p, ui.labelEnergyCapacity, (Utils::toStr(energyCapacity) + " kWh").c_str());
    pvSetText(p, ui.labelIncome, (Utils::toStr(incomeTotal) + " 元").c_str());
    pvSetText(p, ui.labelStorageIn, (Utils::toStr(electStorageIn) + " kWh").c_str());
    pvSetText(p, ui.labelStorageOut, (Utils::toStr(electStorageOut) + " kWh").c_str());
}