#include <QGraphicsScene>
#include <QGLWidget>
#include <QPushButton>
#include <QGraphicsProxyWidget>
#include "GridsView.h"
#include "NuuoGLRenderer.h"

CGridsView::CGridsView(QWidget* parent):
            QGraphicsView(parent),
            m_pGGridLayout(new QGraphicsGridLayout()),
            m_pForm(new QGraphicsWidget()),
            m_pGLWidget(new QGLWidget(this)),
            m_pScene(new CGLScene())
{
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    m_pGLWidget->makeCurrent();
    setViewport(m_pGLWidget);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    setScene(m_pScene);

    m_pGGridLayout->setSpacing(0);
    m_pGGridLayout->setContentsMargins(2,2,2,2);
    m_pForm->setLayout(m_pGGridLayout);
    m_pScene->addItem(m_pForm);

    // Since GLWidget can't be updated partially, use a global timer to update the whole graphics view.
    m_FrameUpdateTimer.setInterval(33);
    connect(&m_FrameUpdateTimer, SIGNAL(timeout()), this, SLOT(UpdateGridsView()));
}

CGridsView::~CGridsView()
{
    if( m_pGGridLayout->count() > 0 )
    { qDebug("[CGridsView] Layout isn't empty. Potential memory leak!"); }

    if(m_pForm)
        delete m_pForm;     // m_pGGridLayout is also deleted.

    if( m_pScene )
        delete m_pScene;
}

void CGridsView::AddWidget(QGraphicsWidget* pWidget, int row, int col, int rowS, int colS)
{
    // Since QGraphicsGridLayout doesn't provide getItemPosition(), we need to remember the corresponding position in the layout.
    m_PositionLookupTable[pWidget] = CLayoutPos(row, col, rowS, colS);

    m_pGLWidget->makeCurrent();
    m_pGGridLayout->addItem(pWidget, row, col, rowS, colS);
    pWidget->show();
    m_pScene->MakeDirty();
}

QGraphicsWidget* CGridsView::TakeAt(int idx)
{
    if( m_pGGridLayout->count() <= 0 )
        return NULL;

    m_pGLWidget->makeCurrent();

    QGraphicsWidget* pWidget = dynamic_cast<QGraphicsWidget*>(m_pGGridLayout->itemAt(idx));
    
    if(pWidget)
    {
        m_pGGridLayout->removeAt(idx);
        m_PositionLookupTable.erase(pWidget);
        m_pScene->MakeDirty();
        pWidget->hide();
    }

    return pWidget;
}

int CGridsView::IndexOf(QGraphicsWidget* pWidget)
{
    if( pWidget == NULL )
        return -1;

    // Since QGraphicsGridLayout doesn't provide indexOf(), we need to go through layout items to find its index value.
    for(int i=0; i<m_pGGridLayout->count(); i++)
    {
        if( pWidget == WidgetAt(i) )
            return i;
    }

    return -1;
}

bool CGridsView::GetWidgetPosition(QGraphicsWidget* pWidget, int* row, int* col, int* rowS, int* colS)
{
    std::map<QGraphicsWidget*, CLayoutPos>::const_iterator Iter = m_PositionLookupTable.find(pWidget);
    if( Iter == m_PositionLookupTable.end() )
        return false;

    if(row)
        *row = (Iter->second).row;
    if(col)
        *col = (Iter->second).col;
    if(rowS)
        *rowS = (Iter->second).rowS;
    if(colS)
        *colS = (Iter->second).colS;

    return true;
}

void CGridsView::EnableFullScreen(bool bEnable)
{
    if( bEnable )
    {
        setWindowFlags(Qt::Window);
        showFullScreen();
    }
    else
    {
        setWindowFlags(0);
        showNormal();
    }
}

