/*
   Libre Media Server - A Media Server Sotfware for stage and performing
   Copyright (C) 2012-2013  Santiago Noreña libremediaserver@gmail.com

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIBREMEDIASERVER_H
#define LIBREMEDIASERVER_H

#include <QMainWindow>
#include <QApplication>
#include <QObject>
#include <QDesktopWidget>
#include <QtDebug>
#include <QtNetwork>
#include <QLocalServer>
#include <QLocalSocket>
#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QTextStream>
#include <QWebView>
#include <QUrl>

#include "msex.h"
#include "CITPDefines.h"
#include "MSEXDefines.h"
#include "ui_libremediaserver.h"

#define VERSION "LibreMediaServer Version 0.04-4-UbuntuPrecise"
#define COPYRIGHT "(C) 2012-2013 Santi Nore libremediaserver@gmail.com"
#define LICENSE "GPL 3 License. See LICENSE.txt and credits.txt for details"

class msex;
class QMenu;
class QProcess;

class libreMediaServer : public QMainWindow
{
    Q_OBJECT

public:

    libreMediaServer (QStringList args, QWidget *parent = 0);
    virtual ~libreMediaServer();

    Ui::LibreMediaServer ui;

protected:

    QString m_pathmedia; // Path to Medias
    msex *m_msex; // CITP/MSEx
    QProcess *m_ola; // OLA daemon process
    QProcess *m_pd_video; // Pure Data process for video
    QProcess *m_pd_audio; // Pure Data process for audio

    // Video Sockets
    QTcpSocket *m_pd_write_video;
    // Video Unix Local Sockets
    QLocalServer *m_server_vid;
    QLocalSocket *m_read_vid;
    // Audio TCP Sockets
    QTcpSocket *m_pd_write_audio;
    QTcpServer *m_pd_read_audio;
    QTcpSocket *m_tcpsocket_audio;
    QTimer *m_preview; // Timer for the preview screen
    int m_startvideo; // Counter starts video engine. Debugging purpose
    int m_startaudio; // Counter starts audio engine. Debugging purpose
    bool m_gui;

private:

    bool sendPacket(const char *buffer, int bufferLen);
    void pdstart();
    void errorsending();
    bool sendPacket_audio(const char *buffer, int bufferLen);
    void pdstart_audio();
    void errorsending_audio();
    void open_start();
    void save_finish();
    void open(QFile *file);
    void save(QFile *file);
//   void MessageHandler(QtMsgType type, const char *msg);

public slots:

    void sendFrame();

private slots:

    void olastart(); // Init the OLA daemon
    void olasetup();
    // Video
    void newPeer();
    void newmessage();
    void newconexion();
    void on_window_stateChanged(int state);
    void on_winpositionx_valueChanged();
    void on_winpositiony_valueChanged();
    void on_winsizex_valueChanged();
    void on_winsizey_valueChanged();
    void on_readDMX_stateChanged(int state);
    void on_layer1Add_valueChanged();
    void on_layer1Check_stateChanged (int state);
    void on_layer2Add_valueChanged();
    void on_layer2Check_stateChanged (int state);
    void on_layer3Add_valueChanged();
    void on_layer3Check_stateChanged (int state);
    void on_layer4Add_valueChanged();
    void on_layer4Check_stateChanged (int state);
    void on_layer5Add_valueChanged();
    void on_layer5Check_stateChanged (int state);
    void on_layer6Add_valueChanged();
    void on_layer6Check_stateChanged (int state);
    void on_layer7Add_valueChanged();
    void on_layer7Check_stateChanged (int state);
    void on_layer8Add_valueChanged();
    void on_layer8Check_stateChanged (int state);
    void on_fpsRate_valueChanged();
    void pdrestart();
    void stdout();
    void on_video_stateChanged(int state);
    void on_previewMaster_stateChanged(int state);
    void on_previewLayer_stateChanged(int state);

    //Audio
    void newPeer_audio();
    void newmessage_audio();
    void newconexion_audio();
    void on_readDMX_audio_stateChanged(int state);
    void on_layer1Add_audio_valueChanged();
    void on_layer1Check_audio_stateChanged (int state);
    void on_layer2Add_audio_valueChanged();
    void on_layer2Check_audio_stateChanged (int state);
    void on_layer3Add_audio_valueChanged();
    void on_layer3Check_audio_stateChanged (int state);
    void on_layer4Add_audio_valueChanged();
    void on_layer4Check_audio_stateChanged (int state);
    void on_layer5Add_audio_valueChanged();
    void on_layer5Check_audio_stateChanged (int state);
    void on_layer6Add_audio_valueChanged();
    void on_layer6Check_audio_stateChanged (int state);
    void on_layer7Add_audio_valueChanged();
    void on_layer7Check_audio_stateChanged (int state);
    void on_layer8Add_audio_valueChanged();
    void on_layer8Check_audio_stateChanged (int state);
    void pdrestart_audio();
    void stdout_audio();
    void on_audio_stateChanged(int state);
    // Preview
    void previewMaster();
    // Menu File
    void openFile();
    void saveFile();
    void ChangeMediaPath();// Change the path to medias
    // Menu CITP/MSEx
    void initMSEX();  // Init the CITP/MSEx protocol
//    void setIPAdd();
    void makeThumbs();
};

#endif // LIBREMEDIASERVER_H
