#include "mainwindow.h"

#include <QApplication>

#include <QLibraryInfo>
#include <QScreen>
#include <QSharedMemory>
#include "commondata.h"

#include "qrtspthread.h"
#include "sthreadimagesave.h"
#include "sthreadsenddicom.h"
#include "sthreadwatchvideolink.h"

#include "seventfilter.h"

#include "common.h"

#include "sapplication.h"
#include <exception>
#include "sexception.h"

#include "sv4l2device.h"

QRTSPThread t;
SThreadImageSave s;
SThreadSendDICOM p;
SThreadWatchVideoLink v;
bool bSync = false;

void InitOpenGL()
{
#ifdef _PC
    QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
#else
    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
#endif

    pid_t ppid = getppid();
    qDebug() << "ppid: " << ppid;

    QString strLibraryPath = QLibraryInfo::location(QLibraryInfo::PluginsPath);
    QSurfaceFormat format;

    bSync = false;
    //bSync = true;

    if(bSync==true)
    {
        format.setSwapInterval(1);
        format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
    }
    else
    {
        format.setSwapInterval(0);
        format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
        //format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
    }

    if(QOpenGLContext::openGLModuleType()==QOpenGLContext::LibGL)
    {
        format.setVersion(3, 3);
        format.setProfile(QSurfaceFormat::CoreProfile);
        format.setDepthBufferSize(24);
    }
    else
    {
        format.setVersion(3, 0);
        format.setDepthBufferSize(24);
    }

    QSurfaceFormat::setDefaultFormat(format);
}

int main(int argc, char *argv[])
{
    //SV4L2Device v4l2;
    //v4l2.open_device("/dev/video0");
    //v4l2.StreamOn();
    QSharedMemory shared("SmartQuadra");

    if(shared.create(512, QSharedMemory::ReadWrite)==false)
    {
        bool bAttach = shared.attach();

        if(bAttach==true)
        {
            int nSize = shared.size();
            QString strInputData = argv[1];

            shared.lock();
            {
                const char* pData1 = (const char*)shared.constData();
                char* pPipe = (char*)shared.data();
                memcpy(pPipe, strInputData.toStdString().c_str(), strInputData.length());
                memset(pPipe, 0, sizeof(char)*512);
            }
            shared.unlock();

            shared.detach();
        }
        //return -1;
    }

    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);

    InitOpenGL();

    //QApplication a(argc, argv);
    SApplication a(argc, argv);
    MainWindow w;



    t.start();
    s.start();
    p.start();
    v.start();

    int nDisplayType = SDISPLAY_1920X1080;
    {
        QScreen *screen = QGuiApplication::primaryScreen();
        QRect  screenGeometry = screen->geometry();
        int height = screenGeometry.height();
        int width = screenGeometry.width();

        nDisplayType = SDISPLAY_1280X1024;
        //nDisplayType = SDISPLAY_1920X1080;
    }

    w.SetDisplayType(nDisplayType);
    //MainWindow::GetCommonData()->SetRTSPThread(&t);
    w.SetRTSPThread(&t);
    w.SetThreadCaptureImage(&s);
    w.SetThreadSendDICOM(&p);

    SEventFilter* pFilter = new SEventFilter(&w);
    a.installEventFilter(pFilter);
    w.show();
    //return a.exec();


    int nRet = a.exec();


    a.removeEventFilter(pFilter);
    SAFE_DELETE(pFilter);

    t.ExitThread();
    s.exit();
    p.exit();
    v.ExitThread();

    w.DeleteUI();

    return nRet;
}
