#include "mcsvirtualjoystickwidget.h"
#include "ui_mcsvirtualjoystickwidget.h"

#include <QApplication>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>

#include "ads_mousegrabber.h"

McsVirtualJoystickWidget::McsVirtualJoystickWidget(IAdsMouseGrabber *theGrabber, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::McsVirtualJoystickWidget)
{
    ui->setupUi(this);
    grabber=theGrabber;
    setCursor(Qt::CrossCursor);

    //setStyleSheet(QLatin1String("{color: transparent; background-color: transparent;}"));
    //setAttribute(Qt::FramelessWindowHint);
    //setAttribute(Qt::WA_TranslucentBackground);
}

McsVirtualJoystickWidget::~McsVirtualJoystickWidget()
{
    delete ui;
}

void McsVirtualJoystickWidget::setGrabber(IAdsMouseGrabber* g)
{
    grabber=g;
}

void McsVirtualJoystickWidget::activate()
{
    if (!grabber)
        return;

    show();

    QPoint glob = mapToGlobal(QPoint(width()/2,height()/2));
    QCursor::setPos(glob);
    grabMouse();
    setMouseTracking(true);
    //qDebug()<<"Activated";
    grabber->activate(glob);
}

void McsVirtualJoystickWidget::deactivate()
{
   setMouseTracking(false);
   releaseMouse();
   grabber->deactivate();
   //qDebug()<<"Deactivated";
}

void McsVirtualJoystickWidget::hideEvent(QHideEvent */*event*/)
{
    deactivate();
}

void McsVirtualJoystickWidget::showEvent(QShowEvent */*event*/)
{
    //activate();
}

void McsVirtualJoystickWidget::keyPressEvent(QKeyEvent*)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
}

void McsVirtualJoystickWidget::keyReleaseEvent(QKeyEvent* e)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
}

void McsVirtualJoystickWidget::mouseMoveEvent(QMouseEvent* e)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
    else
    {
        grabber->mouseMove(e);
        QPoint p=e->globalPos(); //mapToGlobal(e->pos());
        grabber->cursorGotoXY(p.x(), p.y());
    }
}

void McsVirtualJoystickWidget::mousePressEvent( QMouseEvent* e)
{
    if (!grabber)
        return;

    grabber->mousePress(e);
}

void McsVirtualJoystickWidget::mouseReleaseEvent( QMouseEvent* e)
{
    if (!grabber)
        return;
    grabber->mouseRelease(e);
}

void McsVirtualJoystickWidget::wheelEvent(QWheelEvent *e)
{
    if (!grabber)
        return;
    grabber->mouseWheel(e);
}

McsVirtualJoystickDialog::McsVirtualJoystickDialog(QWidget* parent):
    QDialog(parent, Qt::FramelessWindowHint)
{
    //grabber=theGrabber;
    setCursor(Qt::CrossCursor);

    setAttribute(Qt::WA_TranslucentBackground);
    //setWindowOpacity(0.5);
}

void McsVirtualJoystickDialog::setGrabber(IAdsMouseGrabber* g)
{
    grabber=g;
}

void McsVirtualJoystickDialog::activate()
{
    if (!grabber)
        return;

    show();

    QPoint glob = mapToGlobal(QPoint(width()/2,height()/2));
    QCursor::setPos(glob);
    grabMouse();
    setMouseTracking(true);
    //qDebug()<<"Activated";
    grabber->activate(glob);
}

void McsVirtualJoystickDialog::deactivate()
{
   setMouseTracking(false);
   releaseMouse();
   grabber->deactivate();
   //qDebug()<<"Deactivated";
}

void McsVirtualJoystickDialog::hideEvent(QHideEvent */*event*/)
{
    deactivate();
}

void McsVirtualJoystickDialog::showEvent(QShowEvent */*event*/)
{
    //activate();
}

void McsVirtualJoystickDialog::keyPressEvent(QKeyEvent*)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
}

void McsVirtualJoystickDialog::keyReleaseEvent(QKeyEvent* e)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
}

void McsVirtualJoystickDialog::mouseMoveEvent(QMouseEvent* e)
{
    if (QApplication::keyboardModifiers()!=Qt::ControlModifier)
    {
        deactivate();
        hide();
    }
    else
    {
        grabber->mouseMove(e);
        QPoint p=e->globalPos(); //mapToGlobal(e->pos());
        grabber->cursorGotoXY(p.x(), p.y());
    }
}

void McsVirtualJoystickDialog::mousePressEvent( QMouseEvent* e)
{
    if (!grabber)
        return;

    grabber->mousePress(e);
}

void McsVirtualJoystickDialog::mouseReleaseEvent( QMouseEvent* e)
{
    if (!grabber)
        return;
    grabber->mouseRelease(e);
}

void McsVirtualJoystickDialog::wheelEvent(QWheelEvent *e)
{
    if (!grabber)
        return;
    grabber->mouseWheel(e);
}
