/*****************************************************************************
 *
 *  jobmanager.h -- JobManager is an asynchronous job handler.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the JobManager module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#ifndef JOBMANAGER_H
#define JOBMANAGER_H

#ifdef WIN32
#ifdef _DEBUG
#define PROCESSEXEC		"dvdx4procg.exe"
#else
#define PROCESSEXEC		"dvdx4proc.exe"
#endif
#else
#ifdef _DEBUG
#define PROCESSEXEC		"dvdx4procg"
#else
#define PROCESSEXEC		"dvdx4proc"
#endif
#endif

#include "../encoder/transcoder.h"
#include "job.h"
#include "process.h"
#include <QString>
#include <QStringList>
#include <QTextEdit>
#include <QTimer>
#include <QList>
#include <QMap>
#include <QSignalMapper>
#include <QObject>

class JobManager : public QObject
{
	Q_OBJECT

public:
	JobManager(QTextEdit *stdIn, QTextEdit *stdOut, QTextEdit *stdErr, QTextEdit *defLog, const int verbose,
				const int latencyStart = 0, const int latencyFinished = 0, const int latencyKill = 0);
	//~JobManager();

	void abortProcesses();
	void addJob(Job *);
	virtual Job *addJob(QString *cmd, QStringList *args, const QString &output,
			const QString &title, const QString &description,
			Transcoder *transcoder, const qint64 estimatedOutputFileSize);
	virtual Job *addJob(QString *cmd, QStringList *args, const QString &output,
		const QString &title, const QString &description,
		Transcoder *transcoder, const qint64 estimatedOutputFileSize, const double mediaDuration);
	void setProcessDone(Process *process);
	void setLatency(const int, const int, const int);
	void setVerbosity(const int);

	QList<Job *> getJobs() { return jobs; };
	bool isIdle();

signals:
	void processStarted(Task *);
	void processTicked(Q_PID *, Process *, Task::State *, Job *);
	
	void processFinished(Process *, int, QProcess::ExitStatus);
	void processStdOutReady(Process *);
	void processStdErrReady(Process *);

private slots:
	void processJobs();
	void readProcessStdOutput();
	void readProcessStdError();
	void handleProcessFinished(int, QProcess::ExitStatus);

protected:
	QString newJobId();

	void printLog(const QString &, const int=VERBOSITY_NORMAL);
	void printIn(const QString &, const int=VERBOSITY_NORMAL);
	void printOut(const QString &, const int=VERBOSITY_NORMAL);
	void printErr(const QString &, const int=VERBOSITY_NORMAL);

	int verbosity;

	void clearLog();
	void clearIn();
	void clearOut();
	void clearErr();

protected:
	QList<Process *> processes;
	QList<Job *> jobs;

	QTextEdit *log;				// log application errors/warnings
	QTextEdit *standardInput;	// process stdIn
	QTextEdit *standardOutput;	// process stdOut
	QTextEdit *standardError;	// process stdErr

	// Process latency
	int LatencyStart, LatencyFinished, LatencyKill;
	enum Constants { DefaultLatencyStart = 3000, DefaultLatencyFinished = 3000, DefaultLatencyKill= 5000 };
};

#endif
