/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2011-2012  Minh Ngo
 * 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 <QTreeView>
#include "vlcwrapper.h"

class QKeyEvent;
class QStandardItemModel;
class QMouseEvent;

namespace LeechCraft
{
namespace Laure
{
	enum Roles
	{
		IsPlayingRole = Qt::UserRole + 1
	};
	
	enum PlayListColumns
	{
		StatusColumn,
		URLColumn,
		ArtistColumn,
		TitleColumn,
		AlbumColumn,
		GenreColumn,
		DateColumn,
		QueueColumn,
		LengthColumn,
		MAX
	};
	
	/** @brief Provides a model/view implementation of a playlist view.
	 * 
	 * @author Minh Ngo <nlminhtl@gmail.com>
	 */
	class PlayListView : public QTreeView
	{
		Q_OBJECT
		
		QStandardItemModel *PlayListModel_;
		int CurrentItem_;
		int NotHiddenColumnCount_;
		std::shared_ptr<VLCWrapper> VLCWrapper_;
		QMap<PlayListColumns, QByteArray> HeaderProperties_;
	public:
		/** @brief Constructs a new PlayListView class
		 * with the given model and parent.
		 * 
		 * @param[in] model Playlist model.
		 */
		PlayListView (QStandardItemModel *model, QWidget* = 0);
		
		void Init (std::shared_ptr<VLCWrapper> wrapper);
		
		/** @brief Adds the item into the playlist.
		 * 
		 * @param[in] item Media meta info.
		 * @param[in] fileName Media file location.
		 * 
		 * @sa MediaMeta
		 */
		void AddItem (const MediaMeta& item, const QString& fileName);
		
		/** @brief Sets the playing item.
		 * 
		 * @param[in] row  Item index.
		 */
		void MarkPlayingItem (int row);
	protected:
		void keyPressEvent (QKeyEvent*);
	public slots:
		/** @brief Is called to select the item row.
		 * 
		 * @param[in] row Item index.
		 */
		void selectRow (int row);
		
		/** @brief Is called to remove selected rows.
		 */
		void removeSelectedRows ();
	private slots:
		void handleDoubleClicked (const QModelIndex&);
		void handleHideHeaders ();
		void handleHeaderMenu (const QPoint& point);
		void handleMenu (const QPoint& point);
		void handleSectionResized (int logicalIndex, int oldSize, int newSize);
	private:
		void UpdateQueueIndexes ();
		
	signals:
		/** @brief Is emitted when the item index is removed.
		 * 
		 * @param[out] index Item index.
		 */
		void itemRemoved (int index);
		
		/** @brief Notifies that the given item needs to be played.
		 * 
		 * @param[out] index The index of the item to play.
		 */
		void playItem (int index);
	};
}
}