#include "pv/PvApp.h"
#include "pv/PvUser.h"
#include "pv/MaskMain.h"

void pvInitResource(PARAM* p)
{
    pvDownloadFile(p, "assets/pv/bkgHead.png");
    pvDownloadFile(p, "assets/pv/icon1.png");
    pvDownloadFile(p, "assets/pv/icon2.png");
    pvDownloadFile(p, "assets/pv/icon3.png");
    pvDownloadFile(p, "assets/pv/icon4.png");
    pvDownloadFile(p, "assets/pv/icon5.png");
    pvDownloadFile(p, "assets/pv/bkgBox.png");
    pvDownloadFile(p, "assets/pv/map.png");
    pvDownloadFile(p, "assets/pv/mapMarker.png");
    pvDownloadFile(p, "assets/pv/downFill.png");
    pvDownloadFile(p, "assets/pv/play1.png");
}

int onPvThreadCleanup(void* ptr)
{
    PARAM* p = (PARAM*)ptr;
    return 0;
}

int pvMain(PARAM* p)
{
    p->sleep = 100;

    // 管理客户端的连接信息
    PvUser pvuser;
    PvApp::setPvUser(p, &pvuser);
    XLOGD() << "Browser client connect: s= " << int(p->s) << ", user=" << (&pvuser);

    // 客户端断开时回调
    pvSetCleanup(p, onPvThreadCleanup, p);

    pvInitResource(p);
    EPvCode pvcode = EPvCode::NUL;

    while (1)
    {
        // 重置控件的ID值、回调函数表
        PvApp::reset(p);

        std::shared_ptr<MaskMain> mask;
        mask = std::make_shared<MaskMain>(p, pvcode);

        // 超过设定数量值的控件将不会显示
        pvStartDefinition(p, 2048);
        if (mask)
        {
            pvSetStyleSheet(p, PV_ID_MAIN, "QWidget { color: white; font: bold 14px 微软雅黑; }");
            mask->initUI();
        }
        pvQLayoutVbox(p, 0, -1);
        pvEndDefinition(p);
        XLOGD() << "===>>> pvidIndex=" << PvApp::pvid(p);

        char event[MAX_EVENT_LENGTH];
        char text[MAX_EVENT_LENGTH];
        int pvid;
        pvClearMessageQueue(p);
        while (1)
        {
            pvcode = EPvCode::NUL;
            pvPollEvent(p, event);
            PvEvent pvEventId = static_cast<PvEvent>(pvParseEvent(event, &pvid, text));
            if (mask)
            {
                PvApp::dispatch(p, pvEventId, pvid, text);
                switch (pvEventId)
                {
                case NULL_EVENT: {
                    pvcode = mask->onEventNull(pvid);
                } break;
                case BUTTON_EVENT: {
                    pvcode = mask->onEventButton(pvid); std::cout << "PvEvent: (" << pvid << ") BUTTON\n";
                } break;
                case TEXT_EVENT: { std::cout << "PvEvent: (" << pvid << ") TEXT_EVENT\n"; } break;
                case SLIDER_EVENT: { std::cout << "PvEvent: (" << pvid << ") SLIDER_EVENT\n"; } break;
                case CHECKBOX_EVENT: { std::cout << "PvEvent: (" << pvid << ") CHECKBOX_EVENT\n"; } break;
                case RADIOBUTTON_EVENT: { std::cout << "PvEvent: (" << pvid << ") RADIOBUTTON_EVENT\n"; } break;
                case GL_IDLE_EVENT: { std::cout << "PvEvent: (" << pvid << ") GL_IDLE_EVENT\n"; } break;
                case GL_PAINT_EVENT: { std::cout << "PvEvent: (" << pvid << ") GL_PAINT_EVENT\n"; } break;
                case GL_INITIALIZE_EVENT: { std::cout << "PvEvent: (" << pvid << ") GL_INITIALIZE_EVENT\n"; } break;
                case GL_RESIZE_EVENT: { std::cout << "PvEvent: (" << pvid << ") GL_RESIZE_EVENT\n"; } break;
                case TAB_EVENT: { std::cout << "PvEvent: (" << pvid << ") TAB_EVENT\n"; } break;
                case TABLE_CLICKED_EVENT: { std::cout << "PvEvent: (" << pvid << ") TABLE_CLICKED_EVENT\n"; } break;
                case TABLE_TEXT_EVENT: { std::cout << "PvEvent: (" << pvid << ") TABLE_TEXT_EVENT\n"; } break;
                case SELECTION_EVENT: { std::cout << "PvEvent: (" << pvid << ") SELECTION_EVENT\n"; } break;
                case CLIPBOARD_EVENT: { std::cout << "PvEvent: (" << pvid << ") CLIPBOARD_EVENT\n"; } break;
                case BUTTON_PRESSED_EVENT: { /*std::cout << "PvEvent: (" << pvid << ") BUTTON_PRESSED_EVENT\n";*/ } break;
                case BUTTON_RELEASED_EVENT: { /*std::cout << "PvEvent: (" << pvid << ") BUTTON_RELEASED_EVENT\n";*/ } break;
                case RIGHT_MOUSE_EVENT: { std::cout << "PvEvent: (" << pvid << ") RIGHT_MOUSE_EVENT\n"; } break;
                case KEYBOARD_EVENT: { std::cout << "PvEvent: (" << pvid << ") KEYBOARD_EVENT\n"; } break;
                case PLOT_MOUSE_MOVED_EVENT: { std::cout << "PvEvent: (" << pvid << ") PLOT_MOUSE_MOVED_EVENT\n"; } break;
                case PLOT_MOUSE_PRESSED_EVENT: { std::cout << "PvEvent: (" << pvid << ") PLOT_MOUSE_PRESSED_EVENT\n"; } break;
                case PLOT_MOUSE_RELEASED_EVENT: { std::cout << "PvEvent: (" << pvid << ") PLOT_MOUSE_RELEASED_EVENT\n"; } break;
                case USER_EVENT: { std::cout << "PvEvent: (" << pvid << ") USER_EVENT\n"; } break;
                case MOUSE_OVER_EVENT: { /*std::cout << "PvEvent: (" << pvid << ") MOUSE_OVER_EVENT\n";*/ } break;
                default: {} break;
                }
            }

            if (pvcode != EPvCode::NUL)
            {
                std::cout << "mask status: " << (int)pvcode << std::endl;
                break;
            }
        }
    }
}