#include "PvApp.h"
#include "PvUser.h"
#include "TimeUtils.h"


#define FONT_NAME "微软雅黑"

PvRect::PvRect()
{
}

PvRect::PvRect(int ix, int iy, int iw, int ih)
    : x(ix), y(iy), w(iw), h(ih)
{
}

PvRect& PvRect::set(int ix, int iy, int iw, int ih)
{
    x = ix;
    y = iy;
    w = iw;
    h = ih;
    return *this;
}

PvRect& PvRect::setx(int ix)
{
    x = ix;
    return *this;
}

PvRect& PvRect::sety(int iy)
{
    y = iy;
    return *this;
}

PvRect& PvRect::setw(int iw)
{
    w = iw;
    return *this;
}

PvRect& PvRect::seth(int ih)
{
    h = ih;
    return *this;
}
PvRect& PvRect::shiftx(int ix)
{
    x += ix;
    return *this;
}

PvRect& PvRect::shifty(int iy)
{
    y += iy;
    return *this;
}

PvRect& PvRect::shiftw(int iw)
{
    w += iw;
    return *this;
}

PvRect& PvRect::shifth(int ih)
{
    h += ih;
    return *this;
}


void PvApp::setPvUser(PARAM* p, PvUser* pvuser)
{
    p->user = pvuser;
}

PvUser* PvApp::getPvUser(PARAM* p)
{
    return static_cast<PvUser*>(p->user);
}

int PvApp::pvid(PARAM* p)
{
    auto pvuser = PvApp::getPvUser(p);
    return (++pvuser->pvidIndex);
}

void PvApp::reset(PARAM* p)
{
    auto pvuser = PvApp::getPvUser(p);
    if (pvuser) { pvuser->reset(); }
}

EPvCode PvApp::getPvCode(std::string name)
{
    static std::map<std::string, EPvCode> mapPvCode = 
    {
        {"系统总览", EPvCode::MASK_HOME}, 
        {"运行监控", EPvCode::MASK_RUNNING}, 
        {"预测管理", EPvCode::MASK_FORECAST}, 
        {"储能充放电预测", EPvCode::MASK_FORECAST_STORAGE},
        {"光伏发电预测", EPvCode::MASK_FORECAST_SOLAR},
        {"充电负荷预测", EPvCode::MASK_FORECAST_CHARGE},
        {"统计分析", EPvCode::MASK_STAT}, 
        {"储能设备运行分析", EPvCode::MASK_STAT_STORAGE},
        {"光伏设备运行分析", EPvCode::MASK_STAT_SOLAR},
        {"充电设备运行分析", EPvCode::MASK_STAT_CHARGE},
        {"系统管理", EPvCode::MASK_SYSMGR},
        {"用户管理", EPvCode::MASK_MGR_USER},
        {"角色管理", EPvCode::MASK_MGR_ROLE},
        {"权限管理", EPvCode::MASK_MGR_PERMISSION},
        {"场站管理", EPvCode::MASK_MGR_STATION},
        {"设备管理", EPvCode::MASK_MGR_DEVICE},
        {"策略管理", EPvCode::MASK_MGR_POLICY},
        {"系统日志", EPvCode::MASK_MGR_SYSLOG},
        {"告警日志", EPvCode::MASK_MGR_ALERTLOG},
    };
    auto iter = mapPvCode.find(name);
    if (iter != mapPvCode.end())
    {
        return iter->second;
    }
    else
    {
        return EPvCode::NUL;
    }
}

void PvApp::bind(PARAM* p, PvEvent eid, int pvid, std::function<void(std::string text)> cb)
{
    auto pvuser = PvApp::getPvUser(p);
    if (pvuser)
    {
        std::string event = std::to_string((int)eid) + ":" + std::to_string((int)pvid);
        pvuser->mapEventCallback[event] = cb;
    }
}

void PvApp::dispatch(PARAM* p, PvEvent eid, int pvid, std::string text)
{
    auto pvuser = PvApp::getPvUser(p);
    if (pvuser)
    {
        std::string event = std::to_string((int)eid) + ":" + std::to_string((int)pvid);
        auto iter = pvuser->mapEventCallback.find(event);
        if (iter != pvuser->mapEventCallback.end())
        {
            (iter->second)(text);
        }
    }
}

int PvApp::widget(PARAM* p, int parent, int x, int y, int w, int h)
{
    int id = PvApp::pvid(p);
    pvQWidget(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    return id;
}

int PvApp::label(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    static const std::string style = "QLabel { border: none; background-color: transparent; } QLabel:disabled { color: gray;}";
    int id = PvApp::pvid(p);
    pvQLabel(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    if (!text.empty()) { pvSetText(p, id, text.c_str()); }
    pvSetStyleSheet(p, id, QSS.empty() ? style.c_str() : QSS.c_str());
    return id;
}

int PvApp::labelCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    int id = PvApp::label(p, parent, x, y, w, h, text, QSS);
    pvSetAlignment(p, id, AlignCenter);
    return id;
}

int PvApp::labelAlignCenter(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    int id = PvApp::label(p, parent, x, y, w, h, text, QSS);
    pvSetAlignment(p, id, AlignCenter);
    return id;
}

int PvApp::button(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    int id = PvApp::pvid(p);
    pvQPushButton(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    if (!text.empty()) { pvSetText(p, id, text.c_str()); }
    pvSetStyleSheet(p, id, QSS.empty() ? QSS::button().c_str() : QSS.c_str());
    return id;
}

int PvApp::image(PARAM* p, int parent, int x, int y, int w, int h, const char* filename)
{
    int id = PvApp::pvid(p);
    pvQImage(p, id, parent, filename);
    pvSetGeometry(p, id, x, y, w, h);
    return id;
}

int PvApp::combox(PARAM* p, int parent, int x, int y, int w, int h, const std::vector<std::string>& vecItems, int index /*= 0*/)
{
    int id = PvApp::pvid(p);
    pvQComboBox(p, id, parent, 0, 0);
    pvSetGeometry(p, id, x, y, w, h);
    pvSetStyleSheet(p, id, QSS::COMBOX_14.c_str());
    for (int i=0; i<vecItems.size(); ++i)
    {
        pvInsertItem(p, id, i, NULL, vecItems[i].c_str());
        if (index == i) pvSetCurrentItem(p, id, i);
    }
    PvApp::image(p, id, w-18, (h-9)*0.5, 14, 9, "downFill.png");
    return id;
}

int PvApp::textedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    int id = PvApp::pvid(p);
    pvQLineEdit(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    pvSetStyleSheet(p, id, QSS::LINEEDIT.c_str());
    if (!text.empty()) { pvSetText(p, id, text.c_str()); }
    return id;
}

int PvApp::multiTextedit(PARAM* p, int parent, int x, int y, int w, int h, std::string text, std::string QSS)
{
    static std::string style =
        "QTextEdit { background-color: rgb(12, 39, 58); border: 1px solid rgb(18, 251, 255); border-radius: 3px; color:white; font: bold 14px;}"
        "QTextEdit:disabled { border: none; color: gray;}";
    int id = PvApp::pvid(p);
    pvQMultiLineEdit(p, id, parent, true, 10);
    pvSetGeometry(p, id, x, y, w, h);
    pvSetStyleSheet(p, id, style.c_str());
    if (!text.empty()) { pvSetText(p, id, text.c_str()); }
    return id;
}

int PvApp::radioButton(PARAM* p, int parent, int x, int y, int w, int h, std::string text)
{
    static std::string style =
        "QRadioButton { background-color: transparent; color:white; font: bold 14px \"微软雅黑\";}"
        "QRadioButton:disabled { border: none; color: gray;}";

    int id = PvApp::pvid(p);
    pvQRadioButton(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    if (!text.empty()) { pvSetText(p, id, text.c_str()); }
    pvSetStyleSheet(p, id, style.c_str());
    return id;
}

int PvApp::timeEdit(PARAM* p, int parent, int x, int y, int w, int h)
{
    static std::string style =
        "QTimeEdit { background-color: rgb(17, 55, 73); border: 1px solid rgb(0, 185, 208); color:white; font: normal 14px \"微软雅黑\";}"
        "QTimeEdit:disabled { border: none; color: gray;}";

    int id = PvApp::pvid(p);
    pvQTimeEdit(p, id, parent);
    pvSetGeometry(p, id, x, y, w, h);
    pvSetStyleSheet(p, id, style.c_str());
    return id;
}

int PvApp::lineLabel(PARAM* p, int parent, PvRect& rect, int w, std::string key, std::string val)
{
    int pid = PvApp::label(p, parent, rect.x, rect.y, rect.w, rect.h, key, QSS::label(14));
    return PvApp::label(p, pid, w, 0, rect.w-w, rect.h, val);
}

int PvApp::lineTextedit(PARAM* p, int parent, PvRect& rect, int w, std::string key, std::string val)
{
    int pid = PvApp::label(p, parent, rect.x, rect.y, rect.w, rect.h, key, QSS::label(14));
    return PvApp::textedit(p, pid, w, 0, rect.w-w, rect.h, val);
}

