/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2013  Georg Rudoy
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **********************************************************************/

#pragma once

#include <functional>
#include <QObject>

#ifdef ENABLE_MPRIS
#include <qdbuscontext.h>
#endif

#include <phonon/mediasource.h>
#include <phonon/path.h>
#include <interfaces/media/iradiostation.h>
#include "mediainfo.h"
#include "sortingcriteria.h"

class QModelIndex;
class QStandardItem;
class QAbstractItemModel;
class QStandardItemModel;

namespace Phonon
{
	class MediaObject;
	class AudioOutput;
}

typedef QPair<QString, QString> StringPair_t;

namespace LeechCraft
{
namespace LMP
{
	struct MediaInfo;

	class Player : public QObject
#ifdef ENABLE_MPRIS
				 , public QDBusContext
#endif
	{
		Q_OBJECT

		QStandardItemModel *PlaylistModel_;
		Phonon::MediaObject *Source_;
		Phonon::AudioOutput *Output_;
		Phonon::Path Path_;

		QList<Phonon::MediaSource> CurrentQueue_;
		QHash<Phonon::MediaSource, QStandardItem*> Items_;
		QHash<QString, QList<QStandardItem*>> AlbumRoots_;

		Phonon::MediaSource CurrentStopSource_;

		Media::IRadioStation_ptr CurrentStation_;
		QStandardItem *RadioItem_;
		QHash<QUrl, MediaInfo> Url2Info_;

		MediaInfo LastPhononMediaInfo_;
	public:
		enum class PlayMode
		{
			Sequential,
			Shuffle,
			ShuffleAlbums,
			ShuffleArtists,
			RepeatTrack,
			RepeatAlbum,
			RepeatWhole
		};
	private:
		PlayMode PlayMode_;

		struct Sorter
		{
			QList<SortingCriteria> Criteria_;

			Sorter ();
			bool operator() (const MediaInfo&, const MediaInfo&) const;
		} Sorter_;
	public:
		enum Role
		{
			IsCurrent = Qt::UserRole + 1,
			IsStop,
			IsAlbum,
			Source,
			Info,
			AlbumArt,
			AlbumLength
		};

		Player (QObject* = 0);

		QAbstractItemModel* GetPlaylistModel () const;
		Phonon::MediaObject* GetSourceObject () const;
		Phonon::AudioOutput* GetAudioOutput () const;

		PlayMode GetPlayMode () const;
		void SetPlayMode (PlayMode);

		QList<SortingCriteria> GetSortingCriteria () const;
		void SetSortingCriteria (const QList<SortingCriteria>&);

		void PrepareURLInfo (const QUrl&, const MediaInfo&);
		void Enqueue (const QStringList&, bool = true);
		void Enqueue (const QList<Phonon::MediaSource>&, bool = true);
		void ReplaceQueue (const QList<Phonon::MediaSource>&, bool = true);
		QList<Phonon::MediaSource> GetQueue () const;
		QList<Phonon::MediaSource> GetIndexSources (const QModelIndex&) const;

		void Dequeue (const QModelIndex&);
		void Dequeue (const QList<Phonon::MediaSource>&);

		void SetStopAfter (const QModelIndex&);

		void SetRadioStation (Media::IRadioStation_ptr);

		MediaInfo GetCurrentMediaInfo () const;
		QString GetCurrentAAPath () const;
	private:
		MediaInfo GetMediaInfo (const Phonon::MediaSource&) const;
		MediaInfo GetPhononMediaInfo () const;
		void AddToPlaylistModel (QList<Phonon::MediaSource>, bool);

		bool HandleCurrentStop (const Phonon::MediaSource&);

		void UnsetRadio ();

		template<typename T>
		Phonon::MediaSource GetRandomBy (QList<Phonon::MediaSource>::const_iterator,
				std::function<T (Phonon::MediaSource)>) const;
		Phonon::MediaSource GetNextSource (const Phonon::MediaSource&) const;
	public slots:
		void play (const QModelIndex&);
		void previousTrack ();
		void nextTrack ();
		void togglePause ();
		void setPause ();
		void stop ();
		void clear ();
		void shufflePlaylist ();
	private slots:
		void handleSorted ();
		void continueAfterSorted (const QList<QPair<Phonon::MediaSource, MediaInfo>>&);

		void restorePlaylist ();
		void handleStationError (const QString&);
		void handleRadioStream (const QUrl&, const Media::AudioInfo&);
		void handleGotRadioPlaylist (const QString&, const QString&);
		void postPlaylistCleanup (const QString&);
		void handleUpdateSourceQueue ();
		void handlePlaybackFinished ();
		void handleStateChanged (Phonon::State);
		void handleCurrentSourceChanged (const Phonon::MediaSource&);
		void handleMetadata ();
		void refillPlaylist ();
		void setTransitionTime ();
	signals:
		void songChanged (const MediaInfo&);
		void indexChanged (const QModelIndex&);
		void insertedAlbum (const QModelIndex&);

		void playModeChanged (Player::PlayMode);
		void bufferStatusChanged (int);

		void playerAvailable (bool);
	};
}
}
