/*****************************************************************************
 *
 *  configurator.h -- Guess encoder settings.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Encoder 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 CONFIGURATOR_H
#define CONFIGURATOR_H

// should be for libdvd.lib
#ifdef WIN32
#include "config.h"
#include "dvdnav/dvdnav.h"
#endif

#include "../video/video_struct.h"
#include "../video/video_constants.h"
#include "encoder.h"
#include <QString>
#include <QStringList>
#include <QSet>
#include <QObject>

class Configurator : public QObject
{
	Q_OBJECT

public:
	struct Profile
	{
		int level;
		QString title;
		QString description;
		int engine;
		encoder_profile_t profile;
	};

	struct ProfileInterface
	{
		QString title;
		QString panelTitle;

		encoder_profile_t *setting;
		int selected;
		QList<Profile> profiles;
	};

public:
	Configurator(Encoder *);
	//~Configurator();

	bool guessUserInterface();
	bool isValidProfile(const QString &, const QString &, const QString &, const int);
	encoder_profile_t getProfile(const QString &, const QString &, const QString &, const int);
	inline ProfileInterface *getProfileInterface() const { return profileInterface; };
	QString getProfileName(const int, const QString &, const QString &, const QString &, const QString &);
	QString getProfileTitle(const encoder_profile_t *);
	void save();

	QStringList getSupportedProfileMuxers();
	QStringList getSupportedProfileVideoCodecs(const QString &);
	QStringList getSupportedProfileAudioCodecs(const QString &);

	QStringList getApplicableMuxers();
	QStringList getApplicableVideos(const QString &);
	QStringList getApplicableAudios(const QString &);
	QStringList getApplicableSubtitles(const QString &);

	int readProfileFromSettings(const QString &);

	QString choiceText(int catalog, QVariant);

protected:
	// We need a sortable size class for QMap (sort on lines(height) first)
	class SortableSize : public QSize
	{
	public:
		SortableSize(const QSize &size) : QSize(size) {};
		SortableSize(const int &w, const int &h) : QSize(w,h) {};
		bool operator < (const SortableSize &other) const
		{
			if (height() == other.height()) return width() < other.width();
			return height() < other.height();
		};
		QSize size() const { return QSize(width(), height()); };
	};

protected:
	bool configure(const int, const QString &, const QString &, const QString &);
	QList<Configurator::Profile> getProfileList(const int, const QString &, const QString &, const QString &);

	QSize preferredRez43(const QSize &);

	void writeProfileInSettings(const QString &, const int &);

	void setVideoProfiles();
	void setAudioProfiles();

protected:
	Encoder *encoder;
	encoder_settings_t::input_t *inputSettings;

	ProfileInterface *profileInterface;
	QSet<int> supportedProfiles;

	// Profiles
	QHash<QString, encoder_video_profile_t> profileVideos;  // codec->profiles table
	QHash<QString, encoder_audio_profile_t> profileAudios;
};

#endif
