#include "accelerometerwidget.h"
#include <QString>
#include <QFile>
#include <QTextStream>
#include <QGridLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QFile>
#include <phonon/MediaObject>

accelerometerWidget::accelerometerWidget(QWidget *parent)
    : QWidget(parent)
{
    setFont(QFont("Nokia Sans", 50, QFont::Bold));

        QGridLayout *grid = new QGridLayout;

        label1 = new QLabel("X");
        label1->setAlignment(Qt::AlignCenter);
        grid->addWidget(label1, 0, 0);

        label2 = new QLabel("Y");
        label2->setAlignment(Qt::AlignCenter);
        grid->addWidget(label2, 0, 1);

        label3 = new QLabel("Z");
        label3->setAlignment(Qt::AlignCenter);
        grid->addWidget(label3, 0, 2);

        xlabel = new QLabel("NaN");
        xlabel->setAlignment(Qt::AlignCenter);
        grid->addWidget(xlabel, 1, 0);

        ylabel = new QLabel("NaN");
        ylabel->setAlignment(Qt::AlignCenter);
        grid->addWidget(ylabel, 1, 1);

        zlabel = new QLabel("NaN");
        zlabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
        grid->addWidget(zlabel, 1, 2);

        currentxtx = new QLabel("NaN");
        currentxtx->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
        grid->addWidget(currentxtx, 2, 0);

        currentxty = new QLabel("NaN");
        currentxty->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
        grid->addWidget(currentxty, 2, 1);

        QPushButton *bouton = new QPushButton("Exit",this);
        grid->addWidget(bouton,2,2);

        connect(bouton,SIGNAL(clicked()),this,SLOT(close()));

        setLayout(grid);

        myThread = new accelerometerClass(this);
        connect(myThread, SIGNAL(deviceOrientation(QString, QString, QString)),
                this, SLOT(showData(QString, QString, QString)));
        myThread->start(QThread::NormalPriority);

        fileAlarm = new QFile(":/audio/CarAlarm.wav");
        if(!fileAlarm)
            qWarning()<<"Object fileAlarm is empty";
        fileCar = new QFile(":/audio/bip_car.wav");
        if(!fileCar)
            qWarning()<<"Object fileAlarm is empty";
        car = new Phonon::MediaObject(this);
        bipCar = new Phonon::MediaObject(this);

        car->setCurrentSource(fileAlarm);
        bipCar->setCurrentSource(fileCar);
        //connect(car, SIGNAL(finished()),this,SLOT(play()));

}

//void accelerometerWidget::currentValue(QString x, QString y)
//{

//}

void accelerometerWidget::valueCurrent()
{
    current=false;
}

void accelerometerWidget::showData(QString x, QString y, QString z)
{

 if(!current)
    {
        //currentValue(x,y);
        currentx=x;
        currenty=y;
        current=true;
    }
 currentxtx->setText(currentx);
 currentxty->setText(currenty);

 if(currentx.toInt() + 250 < x.toInt() || currentx.toInt() - 250 > x.toInt() )
 {
     xlabel->setText("ALARME!");
     //SLOT(voitureAlarm());
     //connect(car, SIGNAL(finished()),this,SLOT(play()));
     car->play();
 }else
    xlabel->setText(x);

 if(currenty.toInt() + 250 < y.toInt() || currenty.toInt() - 250 > y.toInt() )
 {
     ylabel->setText("ALARME!");
     //if(car->isFinished())
      SLOT(voitureAlarm());
     //bipCar->play();
 }else
    ylabel->setText(y);

    zlabel->setText(z);
}

void accelerometerWidget::voitureAlarm()
{

    //car->play();
    //connect(car, SIGNAL(finished()),this,SLOT(play()));
       // QSound::play("audio/CarAlarm.wav");
    //if (!QSound::isAvailable()) {
            // Bail out.  Programs in which sound is not critical
            // could just silently (hehe) ignore the lack of a server.
            //
      /*      QMessageBox::warning(this,"No Sound",
                    "<p><b>Sorry, you are not running the Network Audio System.</b>"
                    "<p>If you have the `au' command, run it in the background before this program. "
                    "The latest release of the Network Audio System can be obtained from:"
                    "<pre>\n"
                    " &nbsp;\n"
                    "   ftp.ncd.com:/pub/ncd/technology/src/nas\n"
                    "   ftp.x.org:/contrib/audio/nas\n"
                    "</pre>"
                    "<p>Release 1.2 of NAS is also included with the X11R6"
                    "contrib distribution."
                    "<p>After installing NAS, you will then need to reconfigure Qt with NAS sound support");}
*/
}

void accelerometerWidget::voitureBip()
{
    //QSound::play("Cbip_car.wav");
    bipCar->play();
    //connect(bipCar, SIGNAL(finished()),this,SLOT(play()));
}
