/*
 * Copyright (C) 2006-2007	Andre Beckedorf
 * 					 		<evilJazz _AT_ katastrophos _DOT_ net>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef MEDIA_H_INCLUDED
#define MEDIA_H_INCLUDED

#include <qfileinfo.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qfile.h>
#include <qdir.h>
#include <qurl.h>
#include <qdatetime.h>
#include <qdatastream.h>
#include <qstringlist.h>
#include <qobject.h>
#include <qmap.h>
#include <qvaluelist.h>
#include <qintcache.h>

#include "sqlite_source/sqlite3.h"

class MediaDatabase;
class Media;
class MediaIdentifier;

class MediaLocation : public QUrl
{
public:
	MediaLocation();
	MediaLocation(const QString& url);

	QString toString() const {	return reinterpret_cast<const QUrl *>(this)->toString(false, false); };
};

class MediaVideo
{
public:
	MediaVideo(Media *parent);
	
	QString codec;
	int bitrate;
	short width;
	short height;
	float FPS;
	float aspectRatio;
	
	bool load();
	bool save();
private:
	Media *owner;
};
typedef QIntCache<MediaVideo> MediaVideoCache;

class MediaAudio
{
public:
	MediaAudio(Media *parent);
	
	QString codec;
	int format;
	int bitrate;
	int sampleRate;
	int channels;

	bool load();
	bool save();
private:
	Media *owner;	
};
typedef QIntCache<MediaAudio> MediaAudioCache;

typedef QValueList<QString> MetadataEntry;
typedef QMap<QCString, MetadataEntry> MetadataMap;

class MediaMetadataExt
{
public:
	MediaMetadataExt(Media *parent);

	MetadataMap map;	

	bool load();
	bool save();
private:
	Media *owner;	
};
typedef QIntCache<MediaMetadataExt> MediaMetadataExtCache;

class MediaMetadata
{
public:
	MediaMetadata(Media *parent);
	~MediaMetadata();

	QString trackNo;
	QString title;
	QString artist;
	QString album;
	QString playTime;
	QString genre;
	unsigned long length;
	int year;
	int compilation;

	bool load();
	bool save();
	void updatePlaytime();
private:
	Media *owner;	
};
typedef QIntCache<MediaMetadata> MediaMetadataCache;

class MediaFileInfo
{
public:
	enum MediaType {
		MEDIATYPE_UNKNOWN = 0,
		MEDIATYPE_AUDIO = 1,
		MEDIATYPE_VIDEO = 2
	};

	MediaFileInfo(Media *parent);
	~MediaFileInfo();

	MediaType mediaType;		// media type of file
	uint locationHash;
	MediaLocation location;
	uint fileSize;
	QDateTime lastModified;	// last modified time (local file only)

	bool load();
	bool save();
private:
	Media *owner;	
};
typedef QIntCache<MediaFileInfo> MediaFileInfoCache;

class Media : public QObject
{
	Q_OBJECT

private:
	// Common display information
	// NOTE: had to move it here, otherwise it won't get inlined below...
	inline MediaMetadata *mediaMetadata(bool autoNew = true) const {	return (medint_ ? medint_ : mediaMetadataEx(autoNew));	}
	inline MediaFileInfo *mediaFileInfo(bool autoNew = true) const {	return (medinfint_ ? medinfint_ : mediaFileInfoEx(autoNew));	}
	
public:
	Media(QObject *parent = 0, const char *name = 0);
	~Media();
	
	void initializeFromLocation(const MediaLocation &location);
	
	bool loadByID(int mediaid);
	bool loadByLocation(const MediaLocation &location);
	bool loadByHash(uint hash);
	
	void saveToDB();
	
	MediaDatabase *owningMediaDatabase() const { return reinterpret_cast<MediaDatabase*>(parent()); }
	
	bool isAudio() const { return mediaFileInfo()->mediaType == MediaFileInfo::MEDIATYPE_AUDIO; }
	bool isVideo() const { return mediaFileInfo()->mediaType == MediaFileInfo::MEDIATYPE_VIDEO; }
	bool isSupported() const { return mediaFileInfo()->mediaType != MediaFileInfo::MEDIATYPE_UNKNOWN; }

	// get method
	unsigned long length() const { return mediaMetadata()->length; }
	
	MediaLocation location() const { return mediaFileInfo()->location; }
	QDateTime lastModified() const { return mediaFileInfo()->lastModified; }
	uint fileSize() const { return mediaFileInfo()->fileSize; }
	
	MediaAudio *mediaAudio(bool autoNew = true) const;
	MediaVideo *mediaVideo(bool autoNew = true) const;
		
	short width()	{ return mediaVideo()->width; }
	short height()	{ return mediaVideo()->height; }
	int audioRate() { return mediaAudio()->sampleRate; }

	// Metadata
	QString metadata(const QCString& key, int pos = -1) const;
	QStringList metadataStringList() const;

	inline unsigned long mediaID() const { return mediaid_; };

	inline QString trackNo()  const { return mediaMetadata()->trackNo; };
	inline QString title()	   const { return mediaMetadata()->title; };
	inline QString artist()   const { return mediaMetadata()->artist; };
	inline QString album()    const { return mediaMetadata()->album; };
	inline QString playTime() const { return mediaMetadata()->playTime; };
	
public slots:
	void dumpInfo() const;
	
private:
	friend class PlayList;
	friend class MediaDatabase;
	friend class MediaAudio;
	friend class MediaVideo;
	friend class MediaMetadataExt;
	friend class MediaMetadata;
	friend class MediaFileInfo;
	friend class MediaIdentifier;
	
	MediaMetadata *medint_;
	MediaFileInfo *medinfint_;
	unsigned long mediaid_;

	bool loadFromDBVM(sqlite3_stmt *vm);
	
	void identify(const MediaLocation &location);
	
	MediaMetadataExt *mediaMetadataExt(bool autoNew = true) const;
	MediaMetadata *mediaMetadataEx(bool autoNew = true) const;
	MediaFileInfo *mediaFileInfoEx(bool autoNew = true) const;
	
	void updateDisplayInformation(const MediaLocation &location);
};

#endif // MEDIA_H_INCLUDED
