#include "formscreensaver.h"
#include "ui_formscreensaver.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QPainter>
#include <QRandomGenerator>

#include "mainwindow.h"

FormScreenSaver::FormScreenSaver(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormScreenSaver)
{
    ui->setupUi(this);

    m_nCount = 0;

    m_nScreenWidth = qApp->desktop()->size().width();
    m_nScreenHeight = qApp->desktop()->size().height();

    m_Color = new int[25];

    int i=0;
    for(i=0 ; i<25 ; i++)
    {
        m_Color[i] = 100;
    }

    connect(&m_TimerScreenSaver, SIGNAL(timeout()), this, SLOT(ScreenUpdate()));

    setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
    setGeometry(0, 0, m_nScreenWidth, m_nScreenHeight);

    showFullScreen();
}

FormScreenSaver::~FormScreenSaver()
{
    disconnect(&m_TimerScreenSaver, SIGNAL(timeout()), this, SLOT(ScreenUpdate()));
    delete ui;

    SAFE_ARRAY_DELETE(m_Color);
}

void FormScreenSaver::Init()
{
    m_nCount = 0;
    raise();
    show();
    //grabMouse();
    m_TimerScreenSaver.setInterval(100);
    m_TimerScreenSaver.start();
}

void FormScreenSaver::Exit()
{
    if(isVisible()==false)
    {
        return;
    }
    if(m_TimerScreenSaver.isActive()==true)
    {
        m_TimerScreenSaver.stop();
    }
    hide();

    m_nCount = 0;


    MainWindow* pMainWindow = MainWindow::GetMainWindow();
    pMainWindow->ResetScreenSaverTimer();
    pMainWindow->StartScreenSaverTimer();
}

void FormScreenSaver::ScreenUpdate()
{
    m_nCount++;
    update();
}

void FormScreenSaver::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QSize nSize = size();
    int nPaddingLeft = 0;

    //QRect rectDisplay = QRect(nPaddingLeft, 0, nSize.width()-nPaddingLeft, nSize.height());

    //QColor colorBackground;

    painter.setRenderHint(QPainter::SmoothPixmapTransform);

    int nColor = QRandomGenerator::global()->bounded(0, 127) + 128;
    int nIndex = QRandomGenerator::global()->bounded(0, 24);


    float fGabWidth = (float)m_nScreenWidth/5.0f;
    float fGabHeight = (float)m_nScreenHeight/5.0f;

    if(nIndex<0)
    {
        nIndex = 0;
    }
    if(nIndex>24)
    {
        nIndex = 24;
    }

    m_Color[nIndex] = nColor;

    int i=0;
    for(i=0 ; i<25 ; i++)
    {
        int nX = i%5;
        int nY = i/5;

        nColor = m_Color[i];
        if(nColor<0)
        {
            nColor = 0;
        }
        if(nColor>255)
        {
            nColor = 25;;
        }
        painter.fillRect(QRect(nX*fGabWidth, nY*fGabHeight, (fGabWidth+0.5), (fGabHeight+0.5f)), QColor(0, 0, nColor));
    }


    //painter.fillRect(QRect(0, 0, nSize.width(), nSize.height()), QColor(0, 0, nColor));


}

void FormScreenSaver::mousePressEvent(QMouseEvent *event)
{
    Exit();
}

void FormScreenSaver::mouseReleaseEvent(QMouseEvent *event)
{
    Exit();
}

void FormScreenSaver::mouseDoubleClickEvent(QMouseEvent *event)
{
    Exit();
}

void FormScreenSaver::mouseMoveEvent(QMouseEvent *event)
{
    Exit();
}

void FormScreenSaver::keyPressEvent(QKeyEvent *event)
{
    Exit();
}

void FormScreenSaver::keyReleaseEvent(QKeyEvent *event)
{
    Exit();
}
