#include "WidgetPageOverview.h"
#include "common/TimeUtils.h"
#include "common/Utils.h"
static std::vector<double> RANDT(int n, int min, int max)
{
    std::vector<double> v(n);
    for (int i = 0; i<n; i++) { v[i] = Utils::random(min, max); }
    return v;
}

WidgetPageOverview::WidgetPageOverview(QWidget* parent) : WidgetPage(parent)
{
    {
        panel1_ = std::make_shared<Panel>(this, QRect(10, 10, 490, 300), "运行状况");
        QWidget* wParent = panel1_.get();

        labBkg_ = QUI::label(wParent, 10, 40, 150, 250, "", "border:1px solid rgb(27, 88, 105);background-color:rgba(200,200,200,30)");

        labDaysK_ = QUI::label(wParent, 10, 180, 150, 30, "安全运行时间", "color:white;font: bold 20px;");
        labDaysK_->setAlignment(Qt::AlignCenter);

        labDaysV_ = QUI::label(wParent, 10, 100, 105, 50, "223", "color:rgb(77, 215, 240);font: bold 40px;");
        labDaysV_->setAlignment(Qt::AlignBottom | Qt::AlignRight);

        labDaysT_ = QUI::label(wParent, 120, 105, 40, 40, "天", "color:white;font: bold 14px;");
        labDaysT_->setAlignment(Qt::AlignBottom);

        int x = 170; int y = 40;
        int w = 150; int h = 77;
        labp1_ = std::make_shared<LabelParam>(wParent, "站内风机设备", "100", "台", x, y, w, h);
        labp2_ = std::make_shared<LabelParam>(wParent, "站内光伏设备", "100", "台", x += (w+10), y, w, h);

        labp3_ = std::make_shared<LabelParam>(wParent, "当日入网电量", "31.58", "Kwh", x = 170, y += (h+10), w, h);
        labp4_ = std::make_shared<LabelParam>(wParent, "累计入网设备", "54763.01", "Kwh", x += (w+10), y, w, h);

        labp5_ = std::make_shared<LabelParam>(wParent, "累计充放电量", "387.13", "台", x = 170, y += (h+10), w, h);
        labp6_ = std::make_shared<LabelParam>(wParent, "累计碳减排量", "66.72", "吨", x += (w+10), y, w, h);
    }
    
    {
        panel2_ = std::make_shared<Panel>(this, QRect(10, 320, 490, 300), "光伏设备");
        QWidget* wParent = panel2_.get();

        int x = 10;  int y = 40;
        int w = 150; int h = 50;
        labp21_ = std::make_shared<LabelParam>(wParent, "日累计发电量", "223", "Kwh", x, y, w, h);
        labp22_ = std::make_shared<LabelParam>(wParent, "日上网电量", "100", "Kwh", x += (w+10), y, w, h);
        labp23_ = std::make_shared<LabelParam>(wParent, "日发电时长", "152", "h", x += (w+10), y, w, h);

        chartbar2_ = std::make_shared<ChartBarView>(wParent, QRect(10, 95, 470, 210), 5);
        chartbar2_->addItem("日累计发电量");
        chartbar2_->addItem("日上网电量");
        chartbar2_->addItem("日发电时长");
        chartbar2_->updateItem(0, RANDT(7, 1, 10));
        chartbar2_->updateItem(1, RANDT(7, 1, 10));
        chartbar2_->updateItem(2, RANDT(7, 1, 10));
    }

    {
        panel3_ = std::make_shared<Panel>(this, QRect(10, 630, 490, 300), "储能设备");
        QWidget* wParent = panel3_.get();

        int x = 10; int y = 40;
        int w = 225; int h = 50;
        labp31_ = std::make_shared<LabelParam>(wParent, "日累计充电电量", "223.23", "Kwh", x, y, w, h);
        labp32_ = std::make_shared<LabelParam>(wParent, "日累计放电电量", "123.01", "Kwh", x += (w+20), y, w, h);

        chartbar3_ = std::make_shared<ChartBarView>(wParent, QRect(10, 95, 470, 210), 5);
        chartbar3_->addItem("日累充电电量");
        chartbar3_->addItem("日上放电电量");
        chartbar3_->updateItem(0, RANDT(7, 1, 10));
        chartbar3_->updateItem(1, RANDT(7, 1, 10));
   }
    {
        panel4_ = std::make_shared<Panel>(this, QRect(1420, 10, 490, 300), "负荷设备");
        QWidget* wParent = panel4_.get();

        int x = 10; int y = 40;
        int w = 150; int h = 50;
        labp41_ = std::make_shared<LabelParam>(wParent, "日用电功率", "223.23", "Kw", x, y, w, h);
        labp42_ = std::make_shared<LabelParam>(wParent, "日用电次数", "126", "次", x += (w+10), y, w, h);
        labp43_ = std::make_shared<LabelParam>(wParent, "日用电电量", "53.62", "Kwh", x += (w+10), y, w, h);

        chartbar4_ = std::make_shared<ChartBarView>(wParent, QRect(10, 95, 470, 210), 5);
        chartbar4_->addItem("日用电功率");
        chartbar4_->addItem("日用电次数");
        chartbar4_->addItem("日用电电量");
        chartbar4_->updateItem(0, RANDT(7, 1, 10));
        chartbar4_->updateItem(1, RANDT(7, 1, 10));
        chartbar4_->updateItem(2, RANDT(7, 1, 10));
    }
    {
        panel5_ = std::make_shared<Panel>(this, QRect(1420, 320, 490, 300), "充电设备");
        QWidget* wParent = panel5_.get();

        int x = 10; int y = 40;
        int w = 225; int h = 50;
        labp51_ = std::make_shared<LabelParam>(wParent, "日时间利用率", "58", "%", x, y, w, h);
        labp52_ = std::make_shared<LabelParam>(wParent, "日功率利用率", "61", "%", x += (w+20), y, w, h);
        chartbar5_ = std::make_shared<ChartBarView>(wParent, QRect(10, 95, 470, 210), 5);
        chartbar5_->addItem("日时间利用率");
        chartbar5_->addItem("日功率利用率");
        chartbar5_->updateItem(0, RANDT(7, 1, 10));
        chartbar5_->updateItem(1, RANDT(7, 1, 10));
    }
    {
        panel6_ = std::make_shared<Panel>(this, QRect(1420, 630, 490, 300), "告警信息");
        QWidget* wParent = panel6_.get();        
        
        int x = 10; int y = 40;
        int w = 150; int h = 50;
        labp61_ = std::make_shared<LabelParam>(wParent, "日光伏设备告警", "5", "次", x, y, w, h);
        labp62_ = std::make_shared<LabelParam>(wParent, "日储能设备告警", "2", "次", x += (w+10), y, w, h);
        labp63_ = std::make_shared<LabelParam>(wParent, "日负荷设备告警", "3", "次", x += (w+10), y, w, h);

        chartbar6_ = std::make_shared<ChartBarView>(wParent, QRect(10, 95, 470, 210), 5);
        chartbar6_->addItem("光伏设备");
        chartbar6_->addItem("储能设备");
        chartbar6_->addItem("负荷设备");
        chartbar6_->updateItem(0, RANDT(7, 1, 10));
        chartbar6_->updateItem(1, RANDT(7, 1, 10));
        chartbar6_->updateItem(2, RANDT(7, 1, 10));
    }

    {
        panel7_ = std::make_shared<Panel>(this, QRect(510, 680, 900, 250), "功率与辐照度");
        QWidget* wParent = panel7_.get();

        chartline7_ = std::make_shared<ChartLineView>(wParent, QRect(10, 50, 880, 200));
        chartline7_->addItem("功率");
        chartline7_->addItem("辐照度");
        chartline7_->setAxisYLeft(-1, 1, 5);

        double t0 = TimeUtils::datetime2tms(TimeUtils::now_date() + " 00:00:00");
        double tx = t0;
        int n = 360;
        std::vector<std::pair<double, double>> vd0(n+1);
        std::vector<std::pair<double, double>> vd1(n+1);
        for (int i = 0; i<=n; i++)
        {
            tx = t0 + i*(86400/n)*1000;
            vd0[i] = {tx, sin(2*3.1415926*float(i*2)/float(360))};
            vd1[i] = {tx, cos(2*3.1415926*float(i*2)/float(360))};
        }
        chartline7_->setAxisX(t0, tx, 13, "hh:mm");
        chartline7_->updateItem(0, vd0);
        chartline7_->updateItem(1, vd1);
    }

    {
        wWebView = std::make_shared<QWebEngineView>(this);
        wWebView->setGeometry(510, 100, 900, 560);
        // 默认设置透明, 解决加载时的白屏闪烁
        wWebView->page()->setBackgroundColor(Qt::transparent);

        wWebView->setContextMenuPolicy(Qt::NoContextMenu);
        //webEngineView.load(QUrl("https://www.baidu.com"));
        wWebView->load(QUrl("file:///assets/echarts/HTML/maptest.html"));

        //connect(wWebView.get(), &QWebEngineView::loadFinished, this, &WidgetPageHome::slotLoadFinished);

        //std::string htmlContent = "HelloWorld";
        //webEngineView.setHtml(htmlContent.c_str());
        wWebView->show();

        //lab712_.setPixmap(QPixmap(icon.c_str()));

        iconp1_ = std::make_shared<IconParam>(this, "光照", "27.2", "Lux", 600, 40, QColor(246, 155, 82));
        iconp2_ = std::make_shared<IconParam>(this, "风速", "1.2", "m/s", 800, 40, QColor(155, 216, 1));
        iconp3_ = std::make_shared<IconParam>(this, "环境温度", "27.2", "℃", 1000, 40, QColor(61, 254, 250));
        iconp4_ = std::make_shared<IconParam>(this, "环境湿度", "27.2", "%", 1200, 40, QColor(216, 61, 108));
    }
}