/* -*-c++-*- OpenSceneGraph - Copyright (C) 2009 Wang Rui
 *
 * This library is open source and may be redistributed and/or modified under
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGVIEWER_GRAPHICSWINDOWQT
#define OSGVIEWER_GRAPHICSWINDOWQT

#include <osgViewer/GraphicsWindow>

#include <QMutex>
#include <QEvent>
#include <QQueue>
#include <QSet>
#include <QGLWidget>

class QInputEvent;
class QGestureEvent;

namespace osgViewer {
    class ViewerBase;
}

namespace osgQt
{

// forward declarations
class GraphicsWindowQt;

class GLWidget : public QGLWidget
{
    typedef QGLWidget inherited;

public:

    GLWidget( QWidget* parent = nullptr, const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    GLWidget( QGLContext* context, QWidget* parent = nullptr, const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    GLWidget( const QGLFormat& format, QWidget* parent = nullptr, const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
    virtual ~GLWidget();

    inline void setGraphicsWindow( GraphicsWindowQt* gw ) { _gw = gw; }
    inline GraphicsWindowQt* getGraphicsWindow() { return _gw; }
    inline const GraphicsWindowQt* getGraphicsWindow() const { return _gw; }

protected:

    int getNumDeferredEvents()
    {
        QMutexLocker lock(&_deferredEventQueueMutex);
        return _deferredEventQueue.count();
    }
    void enqueueDeferredEvent(QEvent::Type eventType, QEvent::Type removeEventType = QEvent::None)
    {
        QMutexLocker lock(&_deferredEventQueueMutex);

        if (removeEventType != QEvent::None)
        {
            if (_deferredEventQueue.removeOne(removeEventType))
                _eventCompressor.remove(eventType);
        }

        if (_eventCompressor.find(eventType) == _eventCompressor.end())
        {
            _deferredEventQueue.enqueue(eventType);
            _eventCompressor.insert(eventType);
        }
    }
    void processDeferredEvents();

    friend class GraphicsWindowQt;
    GraphicsWindowQt* _gw;

    QMutex _deferredEventQueueMutex;
    QQueue<QEvent::Type> _deferredEventQueue;
    QSet<QEvent::Type> _eventCompressor;

    qreal _devicePixelRatio;

    void resizeEvent( QResizeEvent* event ) override;
    void moveEvent( QMoveEvent* event ) override;
    void glDraw() override;
    bool event( QEvent* event ) override;
};

class GraphicsWindowQt : public osgViewer::GraphicsWindow
{
public:
    GraphicsWindowQt( osg::GraphicsContext::Traits* traits, QWidget* parent = nullptr, const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f = Qt::WindowFlags() );
    GraphicsWindowQt( GLWidget* widget );
    virtual ~GraphicsWindowQt();

    inline GLWidget* getGLWidget() { return _widget; }
    inline const GLWidget* getGLWidget() const { return _widget; }

    /// deprecated
    inline GLWidget* getGraphWidget() { return _widget; }
    /// deprecated
    inline const GLWidget* getGraphWidget() const { return _widget; }

    struct WindowData : public osg::Referenced
    {
        WindowData( GLWidget* widget = nullptr, QWidget* parent = nullptr ): _widget(widget), _parent(parent) {}
        GLWidget* _widget;
        QWidget* _parent;
    };

    bool init( QWidget* parent, const QGLWidget* shareWidget, Qt::WindowFlags f );

    static QGLFormat traits2qglFormat( const osg::GraphicsContext::Traits* traits );
    static void qglFormat2traits( const QGLFormat& format, osg::GraphicsContext::Traits* traits );
    static osg::GraphicsContext::Traits* createTraits( const QGLWidget* widget );

    bool setWindowRectangleImplementation( int x, int y, int width, int height ) override;
    void getWindowRectangle( int& x, int& y, int& width, int& height ) override;
    bool setWindowDecorationImplementation( bool windowDecoration ) override;
    bool getWindowDecoration() const override;
    void grabFocus() override;
    void grabFocusIfPointerInWindow( )override;
    void raiseWindow() override;
    void setWindowName( const std::string& name ) override;
    std::string getWindowName() override;
    void useCursor( bool cursorOn ) override;
    void setCursor( MouseCursor cursor ) override;

    bool valid() const override;
    bool realizeImplementation() override;
    bool isRealizedImplementation() const override;
    void closeImplementation() override;
    bool makeCurrentImplementation() override;
    bool releaseContextImplementation() override;
    void swapBuffersImplementation() override;
    void runOperations() override;

    void requestWarpPointer( float x, float y ) override;

protected:

    friend class GLWidget;
    GLWidget* _widget;
    bool _ownsWidget;
    QCursor _currentCursor;
    bool _realized;
};

}

#endif
