#include <QtGui/QMessageBox>
#include "mandoWindow.hh"
#include "mandoWizard.hh"
#include <X11/extensions/XTest.h>

using namespace mimas;
using namespace std;

MandoWindow::MandoWindow( QWidget *parent, Qt::WFlags f ):
  QWidget( parent, f ), mandoWizard(NULL), timer(0)
{
  ui.setupUi( this );
  connect( ui.quitButton, SIGNAL(clicked()),
           this, SLOT(close()) );
  connect( ui.aboutButton, SIGNAL(clicked()),
           this, SLOT(about()) );
  connect( ui.configureButton, SIGNAL(clicked()),
           this, SLOT(configure()) );
  connect( ui.activateCheckBox, SIGNAL(toggled(bool)),
           this, SLOT(activate(bool)) );
}

void MandoWindow::about()
{
  QMessageBox::about
    ( this, "About mando",
      "<p>Copyright &copy; 2006. <b>mando</b> is free software; you can "
      "redistribute it and/or  modify it under the terms of the <em>GNU "
      "General Public License</em> as published by the Free Software "
      "Foundation; either version 2 of the License, or (at your option) any "
      "later version.</p>"
      "<p><b>mando</b> is distributed in the hope that it will be useful, but "
      "<em>without any warranty</em>; without even the implied warranty of "
      "<em>merchantibility</em> or <em>fitness for a particular "
      "purpose</em>. See the GNU General Public License for more details.</p>"
      "<p>You should have received a copy of the GNU General Public License "
      "along with <b>mando</b>; if not, contact one of the authors of this "
      "software.</p>"
      "<ul><li>Juan Roldan <a href=\"mailto:juan.roldanREMOVETHIS@gmail.com\">juan.roldanREMOVETHIS@gmail.com</a></li>"
      "<li>Ushakiran Soutapalli <a href=\"mailto:ushakiron_sREMOVETHIS@yahoo.co.in\">ushakiron_sREMOVETHIS@yahoo.co.in</a></li>"
      "<li>Julien Faucher <a href=\"faucherjREMOVETHIS@gmail.com\">faucherjREMOVETHIS@gmail.com</a></li>"
      "<li>Jan Wedekind\n <a href=\"mailto:janREMOVETHIS@wedesoft.de\">janREMOVETHIS@wedesoft.de</a></li></ul>" );
}

void MandoWindow::configure()
{
  input.reset();
  pointerRecognition.reset();
  ui.activateCheckBox->setEnabled( false );
  activate(false);
  MandoWizard mandoWizard( this );
}

void MandoWindow::activate( bool on )
{
  if ( on ) {
    if ( timer == 0 )
      timer = startTimer( 0 );
  } else
    if ( timer != 0 ) {
      killTimer( timer );
      timer = 0;
    };
}

void MandoWindow::startDrag(void)
{
  Display *display = XOpenDisplay(NULL);
  XTestFakeButtonEvent( display, 1, True, 0 );
  XCloseDisplay( display );
}

void MandoWindow::stopDrag(void)
{
  Display *display = XOpenDisplay(NULL);
  XTestFakeButtonEvent( display, 1, False, 0 );
  XCloseDisplay( display );
  // ui.singleClickRadio->setChecked( true );
}

void MandoWindow::click(void)
{
  Display *display = XOpenDisplay(NULL);
  XTestFakeButtonEvent( display, 1, True, 0 );
  XTestFakeButtonEvent( display, 1, False, 250 );
  XCloseDisplay( display );
}

void MandoWindow::mouseMove( int x, int y )
{
  Display *display = XOpenDisplay(NULL);
  int screen = DefaultScreen( display );
  XTestFakeMotionEvent( display, screen, x, y, 0 );
  XCloseDisplay( display );
}

void MandoWindow::timerEvent( QTimerEvent *e )
{
  if ( e->timerId() == timer ) {
    try {
      assert( pointerRecognition );
      Vector camPos( 3 ), pos( 3 );
      image< rgba< unsigned char > > frame( grabColourFrame() );
    } catch ( exception & ) {
      assert( timer != 0 );
      killTimer( timer );
      ui.activateCheckBox->setChecked( false );
    };
  } else
    QWidget::timerEvent( e );
}

image< rgba< unsigned char > > MandoWindow::grabColourFrame(void)
  throw (mimasexception)
{
  assert( input );
  image< rgba< unsigned char > > retVal;
  (*input) >> retVal;
  return retVal;
}