/*****************************************************************************
 *
 *  mainwindow.h -- Top window which handles menus and user flow control.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Gui4 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 MAINWINDOW_H
#define MAINWINDOW_H

#include "mainwidget.h"
#include "playerwidget.h"
#include "encoderwidget.h"
#include "../encoder/mediaeditor.h"
#include "advancedmonitorwidget.h"
#include "consoledialog.h"
#include "jobmanagerdialog.h"
#include "../job/jobmanager.h"
#include "../main/console.h"
#include <QMainWindow>
#include <QString>
#include <QSize>
#include <QLabel>
#include <QMenu>
#include <QAction>
#include <QActionGroup>

class MainWindow : public QMainWindow
{
	Q_OBJECT

public:
	struct Preferences
	{
		EncoderWidget::Preferences encoderPreferences;
		PlayerWidget::Preferences playerPreferences;

		Preferences() { reset(); }
		
		void reset()
		{
			encoderPreferences.reset();
			playerPreferences.reset();
		}
	};

public:
	MainWindow();
	void initialize();

	bool requestEncoderTestMode() { return encoderWidget->isRunninTestMode(); };
	int requestEncoderAudioCorrection() { return encoderWidget->readAudioCorrection(); };

signals:
	void encodingComplete(Process *);
	void movieLoaded(int);

public slots:
	void adjustGuiSize();

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

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

	void applyVerbosity();
	void updateVerbosity();
	bool isApplicationReady();
	void checkApplicationReady();
	void closeEvent(QCloseEvent *);
	void showEvent(QShowEvent *);
	void hideEvent(QHideEvent *);
	void createActions();
	void createMenus();
	void createStatusBar();
	void createControls();
	void deleteFromRecentMovie(const QString &);
	void updateRecentPathActions();
	void readSettingsForWindow();
	void readSettingsForFiles();
	void readSettingsForMonitor();
	void readSettingsForOptions();
	void readSettingsForEngines();
	void readSettingsForPreferences();
	void setPathsForExternals();
	void writeSettings();

	QString newFileNameInFolder(const QString &, const QString &);
	QString newJobId();
	void startJob(QString *, QStringList *, const QString &, const QString &, 
		const QString &, const QString &, Encoder *, const int);

	void setCurrentDvdPath(const QString &);
	void setCurrentFilePath(const QString &);
	void updateTargetName(int);

	QString strippedName(const QString &fullFileName);

protected slots:
	void onMovieLoaded(int);
	void processBridgeMessage(const QString &, const bool);
	void processBridgeMessage(const QString &, const int);
	void processBridgeMessage(const QString &, void *);
	void readJobProcessStdOut(Process *);
	void readJobProcessStdErr(Process *);
	void finishJobProcess(Process *, int, QProcess::ExitStatus);
	void startProcessConsole(Task *);
	void updateEncoderInput();
	void updateJobMenu();
	void enableVideoOpenActions(bool);
	void openDvd();
	void openFile();
	void openRecentMovie();
	void clearRecent();
	void setTarget();
	void openTargetOnDesktop();
	void toggleJobManager();
	void updateJobManagerMenu();
	void jobEncodeVcd();
	void jobEncodeSvcd();
	void jobEncodeXvid();
	void jobSplitMovie();
	void saveJob();
	void saveJobAs();
	void showFFmpegStatus();
	void showFFmpegCapability();
	void showMEncoderStatus();
	void showMEncoderCapability();
	void showPlayerStatus();
	void showPlayerCapability();
	void showMediaInfoStatus();
	void showMediaInfo();
	void showFFprobeStatus();
	void showFFprobe();
	void changeZoom();
	void toggleConsoleButton();
	void toggleAdvancedMonitor();
	void updateAdvancedMonitorMenu();
	void resetAdvancedMonitor();
	void clearAdvancedMonitor();
	void changeVerbosity();
	void showPreferences();
	void clearAllSettings();
	void about();
	void onlineHelp();
	void onlineFaq();
	void checkUpdate();
	void credits();

public:
	QAction *aboutAct;
	QMenu *advancedMenu;
	QMenu *encoderEnginesMenu;
	QMenu *helpMenu;
	QMenu *jobMenu;
	QAction *operationsSeperatorAct;
	QAction *splitMovieAct;

protected:
	Console *m_console;

	QByteArray state;
	QString appName;

	QString currentDvd;
	QString currentFile;
	QStringList recentFiles;
	QString currentTargetDir;
	QString currentTargetName;
	
	QString ffmpegBinPath;
	QString mencoderBinPath;
	QString mplayerBinPath;
	QString mediainfoBinPath;
	QString ffprobeBinPath;

	QLabel *statusLabel;

	MainWidget *mainWidget;
	PlayerWidget *playerWidget;
	EncoderWidget *encoderWidget;
	JobManager *jobManager;
	JobManagerDialog *jobManagerDialog;
	JobManagerWidget *jobManagerWidget;
	AdvancedMonitorWidget *advancedMonitor;
	QTextEdit *logChannel;
	QTextEdit *inputChannel;
	QTextEdit *outputChannel;
	QTextEdit *errorChannel;

	bool showAllEncoderOutputOptions;
	bool saveSettingsOnClose;
	bool showConsoleButton;
	int verbosity;

	QMenu *fileMenu;
	QMenu *encoderMenu;
	QMenu *encoderFFmpegMenu;
	QMenu *encoderMEncoderMenu;
	QMenu *playerMenu;
	QMenu *playerBackendMenu;
	QMenu *zoomMenu;
	QMenu *verbosityMenu;
	QAction *openDvdAct;
	QAction *openFileAct;
	QAction *clearRecentAct;
	QAction *setTargetAct;
	QAction *openTargetOnDesktopAct;
	QAction *exitAct;
	QAction *toggleJobManagerAct;
	QAction *encodeVcdAct;
	QAction *encodeSvcdAct;
	QAction *encodeXvidAct;
	QAction *encoderEnginesAct;
	QAction *encoderFFmpegAct;
	QAction *encoderMEncoderAct;
	QAction *showFFmpegCapabilityAct;
	QAction *showFFmpegStatusAct;
	QAction *showMEncoderCapabilityAct;
	QAction *showMEncoderStatusAct;
	QAction *playerBackendAct;
	QAction *mediaInfoAct;
	QAction *ffprobeAct;
	QAction *showMPlayerCapabilityAct;
	QAction *showMPlayerStatusAct;
	QAction *showMediaInfoStatusAct;
	QAction *showMediaInfoAct;
	QAction *showFFprobeStatusAct;
	QAction *showFFprobeAct;
	QAction *zoomMenuAct;
	QActionGroup *zoomActGroup;
	QAction *zoomAutoAct;
	QAction *zoomMaxAct;
	QAction *zoom21Act;
	QAction *zoom11Act;
	QAction *zoom34Act;
	QAction *zoom12Act;
	QAction *zoom14Act;
	QAction *toggleConsoleButtonAct;
	QAction *toggleAdvancedMonitorAct;
	QAction *resetAdvancedMonitorAct;
	QAction *clearAdvancedMonitorAct;
	QActionGroup *verbosityActGroup;
	QAction *verbosityQuietAct;
	QAction *verbosityNormalAct;
	QAction *verbosityVerboseAct;
	QAction *verbosityMenuAct;
	QAction *showPreferencesAct;
	QAction *clearAllSettingsAct;
	QAction *onlineHelpAct;
	QAction *onlineFaqAct;
	QAction *checkUpdateAct;
	QAction *creditsAct;

	enum Constants { MaxRecentMovies = 5 };

	QAction *recentMoviesActs[MaxRecentMovies];
	Preferences preferences;
};

#endif
