#include "WidgetPageRunning.h"
#include "common/Logger.h"

static void ReviseQueuePos(int i, int maxCol, int w, int h, int& x, int& y)
{
    if (i > 0)
    {
        if (i%maxCol == 0)
        {
            x = 0;
            y += (h);
        }
        else
        {
            x += (w);
        }
    }
}

class PanelCard : public Panel
{
public:
    QLabel labIcon_;
    QLabel labName_;
    QLabel labTyle_;
    QLabel labStatus_;
    std::map<std::string, std::pair<std::shared_ptr<QLabel>, std::shared_ptr<QLabel>>> mapLabParams_;

    std::string styLabelK_ = "QLabel {font:bold 15px;color:rgb(166, 184, 221);}";
    std::string styLabel_ = "QLabel {font:bold 15px;color:white;}";

    PanelCard(QWidget* parent, QRect rt, std::string type) : Panel(parent, rt, "")
    {
        //this->setObjectName("MyPanel");
        //std::string sty = "#MyPanel {background-color:rgb(8, 54, 91);border:1px solid rgba(255,255,255,10);border-radius:15px;}";
        this->setAutoFillBackground(true);
        //this->setStyleSheet(sty.c_str());
        this->setBackground(QColor(8, 54, 91), "border:1px solid rgba(255,255,255,10);border-radius:15px;");

        QUI::labelImage(labIcon_, this, 10, 20, 64, 64, "assets/ui/solarPanel.png");
        QUI::label(labName_, this, 90, 20, 120, 20, "#NAME", styLabel_);
        QUI::label(labStatus_, this, 90, 40, 120, 20, "#STATUS", styLabel_);
        QUI::label(labTyle_, this, 90, 60, 120, 20, type, "QLabel {font:bold 15px;color:rgb(8, 165, 255);}");
    }

    void setIcon(std::string icon)
    {
        labIcon_.setPixmap(QPixmap(icon.c_str()));
    }
    void setName(std::string name)
    {
        labName_.setText(name.c_str());
    }
    void setStatus(int status)
    {
        std::vector<std::string> VEC_STATUS = {"运行", "空闲", "离线", "故障"};
        if (status >=0 && status <=3)
        {
            labStatus_.setText(VEC_STATUS[status].c_str());
        }
        else
        {
            labStatus_.setText("#STATUS");
        }
    }
    void setParam(std::string k, std::string v)
    {
        std::shared_ptr<QLabel> labK = nullptr;
        std::shared_ptr<QLabel> labV = nullptr;

        auto iter = mapLabParams_.find(k);
        if (iter == mapLabParams_.end())
        {
            int y = 100 + mapLabParams_.size() * 24;
            labK = QUI::label(this, 20, y, 80, 20, k + ":", styLabelK_);
            labV = QUI::label(this, 110, y, 120, 20, v, styLabel_);
            mapLabParams_[k] = {labK, labV};
        }
        else
        {
            labV = iter->second.second;
            labV->setText(v.c_str());
        }
    }

    virtual void enterEvent(QEvent* e) override
    {
        this->setBackground(QColor(30, 100, 100, 120), "border: 1px solid white;border-radius:15px;");
    }
    virtual void leaveEvent(QEvent* e) override
    {
        this->setBackground(QColor(8, 54, 91), "border:1px solid rgba(255,255,255,10);border-radius:15px;");
    }
};

class PanelDeviceDetail : public Panel
{
public:
    PanelDeviceDetail(QWidget* parent, QRect rt) : Panel(parent, rt)
    { 
    }
};


class PanelDeviceWind : public PanelDeviceDetail
{
public:
    std::vector<std::shared_ptr<PanelCard>> vecCards_;

    PanelDeviceWind(QWidget* parent, QRect rt) : PanelDeviceDetail(parent, rt)
    {
        int col = 5;
        int x = 0;
        int y = 0;
        int W = 280;
        int H = 320;
        for (int i = 0; i<col; i++)
        {
            ReviseQueuePos(i, 5, W+10, H+10, x, y);
            auto card = std::make_shared<PanelCard>(this, QRect(x+10, y+10, W, H), "风机");
            card->setIcon("assets/ui/icon1.png");
            card->setName("风电设备-" + std::to_string(i+1));
            card->setStatus(0);
            vecCards_.push_back(card);

            card->setParam("位置", "能源站xxxx-xxx");
            card->setParam("工作状态", "发电/空闲");
            card->setParam("在线状态", "在线/离线");
            card->setParam("故障状态", "正常/故障");
            card->setParam("电压", "220 V");
            card->setParam("电流", "50 A");
            card->setParam("额定功率", "30 KW");
            card->setParam("实时功率", "28 KW");
        }
    }
};

class PanelDeviceSolar : public PanelDeviceDetail
{
public:
    std::vector<std::shared_ptr<PanelCard>> vecCards_;

    PanelDeviceSolar(QWidget* parent, QRect rt) : PanelDeviceDetail(parent, rt)
    {
        int col = 5;
        int x = 0;
        int y = 0;
        int W = 280;
        int H = 320;
        for (int i = 0; i<col; i++)
        {
            ReviseQueuePos(i, 5, W+10, H+10, x, y);
            std::string type = (i==0) ? "逆变器" : (i==1 ? "汇流箱" : "光伏板");
            auto card = std::make_shared<PanelCard>(this, QRect(x+10, y+10, W, H), type);
            card->setIcon("assets/ui/solarPanel.png");
            card->setName("设备-" + std::to_string(i+1));
            card->setStatus(0);
            vecCards_.push_back(card);
            card->setParam("位置", "能源站xxxx-xxx");
            card->setParam("电压", "220V");
            card->setParam("电流", "0.5A");
            card->setParam("额定功率", "22kw");
            card->setParam("实时功率", "6.83kw");
        }
    }
};

class PanelDeviceEnergyStorage : public PanelDeviceDetail
{
public:
    std::vector<std::shared_ptr<PanelCard>> vecCards_;

    PanelDeviceEnergyStorage(QWidget* parent, QRect rt) : PanelDeviceDetail(parent, rt)
    {
        int col = 10;
        int x = 0;
        int y = 0;
        int W = 280;
        int H = 320;
        for (int i = 0; i<col; i++)
        {
            ReviseQueuePos(i, 5, W+10, H+10, x, y);
            auto card = std::make_shared<PanelCard>(this, QRect(x+10, y+10, W, H), "储能电池");
            card->setIcon("assets/ui/energyStorage.png");
            card->setName("储能设备-" + std::to_string(i+1));
            card->setStatus(0);
            vecCards_.push_back(card);
            card->setParam("位置", "能源站xxxx-xxx");
            card->setParam("电压", "220V");
            card->setParam("电流", "0.5A");
            card->setParam("额定功率", "22kw");
            card->setParam("实时功率", "6.83kw");
        }
    }
};

class PanelDeviceCharge : public PanelDeviceDetail
{
public:
    std::vector<std::shared_ptr<PanelCard>> vecCards_;

    PanelDeviceCharge(QWidget* parent, QRect rt) : PanelDeviceDetail(parent, rt)
    {
        int col = 5;
        int x = 0;
        int y = 0;
        int W = 280;
        int H = 320;
        for (int i = 0; i<col; i++)
        {
            ReviseQueuePos(i, 5, W+10, H+10, x, y);
            auto card = std::make_shared<PanelCard>(this, QRect(x+10, y+10, W, H), "充电机");
            card->setIcon("assets/ui/charger.png");
            card->setName("充电设备-" + std::to_string(i+1));
            card->setStatus(0);
            vecCards_.push_back(card);
            card->setParam("位置", "能源站xxxx-xxx");
            card->setParam("电压", "220V");
            card->setParam("电流", "0.5A");
            card->setParam("额定功率", "22kw");
            card->setParam("实时功率", "6.83kw");
        }
    }
};

class PanelCardCamera
{
public:
    Panel panel_;
    QLabel labName_;
    QLabel labVideo_;

    PanelCardCamera(QWidget* parent, QRect rt, std::string type) : panel_(parent, rt, "")
    {
        panel_.setObjectName("MyPanel");
        std::string sty = "#MyPanel {background-color:rgb(8, 54, 91);border:1px solid rgba(255,255,255,10);border-radius:15px;}";
        panel_.setAutoFillBackground(true);
        panel_.setStyleSheet(sty.c_str());

        QUI::label(labName_, &panel_, 10, 10, rt.width()-20, 30, "xxx监控点", "font:bold 16px; color:white;");
        QUI::labelImage(labVideo_, &panel_, 10, 40, rt.width()-20, rt.height()-50, "assets/ui/camera.png");
    }
};

class PanelDeviceSecurity : public PanelDeviceDetail
{
public:
    std::vector<std::shared_ptr<PanelCardCamera>> vecCards_;

    PanelDeviceSecurity(QWidget* parent, QRect rt) : PanelDeviceDetail(parent, rt)
    {
        int col = 6;
        int x = 0;
        int y = 0;
        int W = 410;
        int H = 308;
        for (int i = 0; i<col; i++)
        {
            ReviseQueuePos(i, 3, W+50, H+50, x, y);
            auto card = std::make_shared<PanelCardCamera>(this, QRect(x+50, y+50, W, H), "");
            vecCards_.push_back(card);
        }
    }
};

static PanelDevice* g_activePanel_ = nullptr;
class PanelDevice : public Panel
{
public:
    PanelDevice(QWidget* parent, QRect rt, std::string title) : Panel(parent, rt, title)
    {
        this->setBackground(QColor(29,54,102), "border:0px solid white; border-radius:5px;");
    }
    void setBackgroundStatus(int flag)
    {
        if (flag)
        {
            this->setBackground(QColor(29, 100, 102), "border: 1px solid white;");
        }
        else
        {
            this->setBackground(QColor(29, 54, 102), "border:0px solid white; border-radius:5px;");
        }
    }
    void setActive(bool flag)
    {
        if (flag)
        {
            g_activePanel_ = this;
        }
        setBackgroundStatus(flag);
        if (panelDetail_)
        {
            panelDetail_->setVisible(flag);
        }
    }
    virtual void enterEvent(QEvent* e) override
    {
        setBackgroundStatus(1);
    }
    virtual void leaveEvent(QEvent* e) override
    {
        if (g_activePanel_ != this)
        {
            setBackgroundStatus(0);
        }
    }
    void mousePressEvent(QMouseEvent* e) override
    {
        if (e->button() == Qt::LeftButton)
        {
            if (g_activePanel_ != this)
            {
                if (g_activePanel_) { g_activePanel_->setActive(false); }
                this->setActive(true);
            }
        }
    }

    void setDetailPanel(std::shared_ptr<PanelDeviceDetail> panelDetail)
    {
        panelDetail_ = panelDetail;
    }

    std::shared_ptr<PanelDeviceDetail> panelDetail_ = nullptr;
};

WidgetPageRunning::WidgetPageRunning(QWidget* parent) : WidgetPage(parent)
{
    // 设备类型Panel
    int H = 180;
    int i = 0;

    //panel1_ = std::make_shared<PanelDevice>(this, QRect(10, 10+H*0, 380, H-10), "风电设备");
    //panel1_->setDetailPanel(std::make_shared<PanelDeviceWind>(this, QRect(400, 10, 1490, 900)));
    //panel1_->setActive(true);

    panel2_ = std::make_shared<PanelDevice>(this, QRect(10, 10+H*(i++), 380, H-10), "光伏设备");
    panel2_->setDetailPanel(std::make_shared<PanelDeviceSolar>(this, QRect(400, 10, 1490, 900)));
    panel2_->setActive(false);

    panel3_ = std::make_shared<PanelDevice>(this, QRect(10, 10+H*(i++), 380, H-10), "储能设备");
    panel3_->setDetailPanel(std::make_shared<PanelDeviceEnergyStorage>(this, QRect(400, 10, 1490, 900)));
    panel3_->setActive(false);

    panel4_ = std::make_shared<PanelDevice>(this, QRect(10, 10+H*(i++), 380, H-10), "充电设备");
    panel4_->setDetailPanel(std::make_shared<PanelDeviceCharge>(this, QRect(400, 10, 1490, 900)));
    panel4_->setActive(false); 

    panel5_ = std::make_shared<PanelDevice>(this, QRect(10, 10+H*(i++), 380, H-10), "安防设备");
    panel5_->setDetailPanel(std::make_shared<PanelDeviceSecurity>(this, QRect(400, 10, 1490, 900)));
    panel5_->setActive(false);

    panel2_->setActive(true);

    //panelDevice_ = std::make_shared<Panel>(this, QRect(420, 10, 1490, 900), "");
    //{
    //}
}