/*****************************************************************************
 *
 *  job.cpp -- Job handler.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Job 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.
 *
 *****************************************************************************/

#include "job.h"
#include "macros.h"
#include <QByteArray>
#include <QFile>
#include <QDebug>

Job::Job()
{
}

void Job::setTask(Task *task, const QString &jobId, const QString &jobName, const QString &jobOutput,
			QTextEdit *stdIn, QTextEdit *stdOut, QTextEdit *stdErr, QTextEdit *defLog, const int verbosityLevel,
			const int latencyStart, const int latencyFinished, const int latencyKill)
{
	this->task = task;

	id = jobId;
	name = jobName;
	output = jobOutput;

	LatencyStart = (latencyStart == 0) ? DefaultLatencyStart : latencyStart;
	LatencyFinished = (latencyFinished == 0) ? DefaultLatencyFinished : latencyFinished;
	LatencyKill = (latencyKill == 0) ? DefaultLatencyKill : latencyKill;

	// Set IO channels from parent channels
	standardInput = stdIn;
	standardOutput = stdOut;
	standardError = stdErr;
	log = defLog;
	verbosity = verbosityLevel;

	// Fill task with job infos
	task->setJobInfo(this, this->id, this->name);
}

Task* Job::getTaskToStart()
{
	if (task->getState() == Task::Waiting)
	{
		return task;
	}
	else
	{
		return 0;
	}
}

Task::State Job::getTaskState()
{
	return task->getState();
}

bool Job::isFinished()
{
	return task->isFinished();
}

void Job::updateOnTerminatedTask(Task *task, bool terminated)
{
	if (terminated)
	{
		task->setState( Task::Terminated );
	
		printLog( QObject::tr("Job: %1 terminated [%2].")
				.arg(task->getName()).arg(this->id), VERBOSITY_NORMAL );
	}
	else
	{
		task->setState( Task::Aborted );

		printLog( QObject::tr("Job: %1 aborted [%2].")
			.arg(task->getName()).arg(this->id), VERBOSITY_VERBOSE );

		QList<QVariant> *data = (QList<QVariant> *) task->getData();
		QString output = data->value(5).toString();
		if (QFile::exists(output)) QFile::remove(output);
	}

	emit finished();
}

QList<void *> Job::getConsoles()
{
	QList<void *> consoles;

	void *console = task->getConsole();

	if (console != 0) consoles.append( console );

	return consoles;
}

void Job::printLog(const QString &message, const int verbosityLevel)
{
	if ( log == 0 )
		qDebug() << "Job Log:" << message;
	else if ( verbosityLevel <= verbosity )
		log->append( message );
}

void Job::printIn(const QString &message, const int verbosityLevel)
{
	if ( standardInput == 0)
		qDebug() << "Job stdIn:" << message;
	else if ( verbosityLevel <= verbosity )
		standardInput->append( message );
}

void Job::printOut(const QString &message, const int verbosityLevel)
{
	if ( standardOutput == 0)
		qDebug() << "Job stdOut:" << message;
	else if ( verbosityLevel <= verbosity )
		standardOutput->append( message );
}

void Job::printErr(const QString &message, const int verbosityLevel)
{
	if ( standardError == 0)
		qDebug() << "Job stdErr:" << message;
	else if ( verbosityLevel <= verbosity )
		standardError->append( message );
}

void Job::clearLog()
{
	if ( log != 0)
		log->clear();
}

void Job::clearIn()
{
	if ( standardInput != 0)
		standardInput->clear();
}

void Job::clearOut()
{
	if ( standardOutput != 0)
		standardOutput->clear();
}

void Job::clearErr()
{
	if ( standardError != 0)
		standardError->clear();
}
