#include "WidgetPageHome.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;
}

class InfoItem : public QWidget
{
public:
    QLabel labTitle_;
    QLabel labVal_;
    QLabel labUnit_;

    InfoItem::InfoItem(QWidget* parent, QRect rt, std::string title, std::string unit, std::string v="")
        : QWidget(parent)
    {
        this->setGeometry(rt);
        QUI::setBackground(this, "InfoCardItem", QColor(200, 200, 200, 30));

        int xHalf = rt.width()/2;
        int yHalf = rt.height()/2;
        QUI::label(labTitle_, this, 0, 0, rt.width(), yHalf-10, title);
        labTitle_.setAlignment(Qt::AlignBottom | Qt::AlignHCenter);
        QUI::label(labVal_, this, 0, yHalf, xHalf+20, 30, v, "font:bold 24px;color:rgb(38,220,172);");
        labVal_.setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        QUI::label(labUnit_, this, xHalf+30, yHalf, 30, 30, unit);
    }

    void InfoItem::setVal(std::string v)
    {
        labVal_.setText(v.c_str());
    }
};

class EnvItem
{
public:
    QLabel labBkg_;
    QLabel labIcon_;
    QLabel labVal_;

    EnvItem(QWidget* parent, int x, int y, std::string icon, std::string v)
    {
        QUI::label(labBkg_, parent, x, y, 60, 60, "", "background-color:rgb(42, 74, 132);border-radius:30px;");
        QUI::label(labIcon_, parent, x+6, y+6, 48, 48, "");
        labIcon_.setPixmap(QPixmap(icon.c_str()));
        QUI::label(labVal_, parent, x-10, y+65, 80, 40, v, "font:bold 14px;color:white;");
        labVal_.setAlignment(Qt::AlignCenter);
    }
};
void WidgetPageHome::slotLoadFinished(bool bOK)
{
    QString jsCode = QString("showalert('%1')").arg("Hello QtWebEngine!");
    wWebView->page()->runJavaScript(jsCode, [](const QVariant& v) { qDebug() << v.toString(); });
}
WidgetPageHome::WidgetPageHome(QWidget* parent) : WidgetPage(parent)
{
    const std::string STY_VAL = "font:bold 18px;color:rgb(38,220,172);";
    const int H = 300;

    // 地图
    //QUI::label(labMapBkg_, this, 500, 10, 920, H*3-10, "");
    //labMapBkg_.setPixmap(QPixmap("assets/ui/bkg_map.png"));

    wWebView = std::make_shared<QWebEngineView>(this);
    wWebView->setGeometry(500, 10, 920, H*3-10);

    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();

    panelStation_ = std::make_shared<Panel>(this, QRect(10, 10, 480, H-10), "信息概览");
    {
        QWidget* wParent = panelStation_.get();
        infoNum1_ = std::make_shared<InfoItem>(wParent, QRect(20, 50, 210, 100), "风电设备数量", "台", "5");
        infoNum2_ = std::make_shared<InfoItem>(wParent, QRect(240, 50, 210, 100), "光伏设备数量", "台", "27");
        infoNum3_ = std::make_shared<InfoItem>(wParent, QRect(20, 160, 210, 100), "储能设备", "台", "16");
        infoNum4_ = std::make_shared<InfoItem>(wParent, QRect(240, 160, 210, 100), "充电设备", "台", "12");
    }

    panelStat_ = std::make_shared<Panel>(this, QRect(10, 10+H, 480, H-10), "统计信息");
    {
        int x = 40; int y = 50;
        int w = 180;
        int h = 60; int h1 = 40;
        QWidget* wParent = panelStat_.get();
        labSwapCount_ = QUI::labelPairV(wParent, x, y, w, h, h1, "风电今日发电量", "100.000");
        labElectCount_ = QUI::labelPairV(wParent, x + 200, y, w, h, h1, "风电累计发电量", "100.000");
        labSwapCountDay_ = QUI::labelPairV(wParent, x, y += (h+10), w, h, h1, "光伏今日发电量", "100.000");
        labElectCountDay_ = QUI::labelPairV(wParent, x+ 200, y, w, h, h1, "光伏累计发电量", "100.000");
        labCarbonCount_ = QUI::labelPairV(wParent, x, y += (h+10), w, h, h1, "信息5", "100.000");
        labP1_ = QUI::labelPairV(wParent, x+ 200, y, w, h, h1, "信息4", "100.000");
    }


    panelEnv_ = std::make_shared<Panel>(this, QRect(10, 10+H*2, 480, H-10), "环境信息");
    {
        QWidget* wParent = panelEnv_.get();

        int x = 40;
        int y = 50;
 
        envTemperature_ = std::make_shared<EnvItem>(wParent, x, y, "./assets/ui/temperature.png", "温度\n31℃");
        envHumidity_ = std::make_shared<EnvItem>(wParent, x+100, y, "./assets/ui/humidity.png", "湿度\n42%");
        envWind_ = std::make_shared<EnvItem>(wParent, x+200, y, "./assets/ui/windsSolid.png", "风速\n1.5m/s");
        envSolar_ = std::make_shared<EnvItem>(wParent, x+300, y, "./assets/ui/brightness.png", "辐照度\n120W/m2");

        y += 120;
        QUI::label(labProgressCpu_, wParent, 40, y, 100, 24, "CPU使用率");
        progressCpu_ = std::make_shared<ProgressView>(wParent, 120, y, 24);
        progressCpu_->setVal(30);

        y += 40;
        QUI::label(labProgressMem_, wParent, 40, y, 100, 24, "内存使用率");
        progressMem_ = std::make_shared<ProgressView>(wParent, 120, y, 24);
        progressMem_->setVal(60);

        y += 40;
        QUI::label(labProgressDisk_, wParent, 40, y, 100, 24, "磁盘使用率");
        progressDisk_ = std::make_shared<ProgressView>(wParent, 120, y, 24);
        progressDisk_->setVal(60);
    }

    int y = 10;
    int h = 280;
    panelSwap_ = std::make_shared<Panel>(this, QRect(1430, 10+H*0, 480, H-10), "信息1");
    {
        //chartbar1_ = std::make_shared<ChartBarView>(panelSwap_.get(), QRect(0, 30, 480, h-20), 7);
        //chartbar1_->addItem("数据1");
        //chartbar1_->addItem("数据2");
        //chartbar1_->addItem("数据3");
        //chartbar1_->addItem("数据4");
    }

    y += (h+10);
    panel2_ = std::make_shared<Panel>(this, QRect(1430, 10+H*1, 480, H-10), "信息2");
    {
        //chartbar2_ = std::make_shared<ChartBarView>(panel2_.get(), QRect(0, 30, 480, h-20), 7);
        //chartbar2_->addItem("数据1");
        //chartbar2_->addItem("数据2");
        //chartbar2_->addItem("数据3");
    }

    y += (h+10);
    panel3_ = std::make_shared<Panel>(this, QRect(1430, 10+H*2, 480, H-10), "信息3");
    {
        chartbar3_ = std::make_shared<ChartBarView>(panel3_.get(), QRect(0, 30, 480, h-20), 7);
        chartbar3_->addItem("数据1");
        chartbar3_->addItem("数据2");

        // 测试数据
        //chartbar3_->updateItem(0, RANDT(7, 1, 10));
        //chartbar3_->updateItem(1, RANDT(7, 1, 10));
    }
    

    //chartbar1_->updateItem(0, RANDT(7, 1, 10));
    //chartbar1_->updateItem(1, RANDT(7, 1, 10));
    //chartbar1_->updateItem(2, RANDT(7, 1, 10));
    //chartbar1_->updateItem(3, RANDT(7, 1, 10));

    //chartbar2_->updateItem(0, RANDT(7, 1, 10));
    //chartbar2_->updateItem(1, RANDT(7, 1, 10));
    //chartbar2_->updateItem(2, RANDT(7, 1, 10));



    //panelPower_ = std::make_shared<Panel>(this, QRect(500, 590, 920, 280), "今日负荷曲线");
    //{
    //    QWidget* wParent = panelPower_.get();
    //    chartline7_ = std::make_shared<ChartLineView>(wParent, QRect(10, 30, 900, 240));
    //    chartline7_->addItem("数据1");
    //    chartline7_->addItem("数据2");
    //    chartline7_->setAxisYLeft(-1, 1, 7);

    //    //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);
    //}
};