/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageFilePlayer.h
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fri, 23 Sep 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	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 RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#ifndef RIMAGEFILEPLAYER_H__
#define RIMAGEFILEPLAYER_H__

#include "ritkSourcePlugin.h"

#include <ritkRImageFileSource.h>
#include <QMutex>
#include <QTimer>
#include "ritkRImageFilePlayerWidget.h"

namespace ritk
{

/**	@ingroup	Core
 *	@class		RImageFilePlayer
 *	@author		Jakob Wasza / Sebastian Bauer
 *	@brief		RITK player that gets its input from a file
 *
 *	@details
 *	RITK player that gets its input from a file. List modes here
 *	The player holds a pointer to the controlling widget. \n
 */
class RImageFilePlayer : public SourcePlugin
{
	Q_OBJECT

public:
	/// Constructor
	RImageFilePlayer();
	/// Destructor
	virtual ~RImageFilePlayer();

	/// Get the plugins GUI @sa PluginBase::GetPluginGUI
	QWidget *GetPluginGUI();

	/** @name	Player states */
	//@{
	enum FileInterfaceStateType 
	{
		ClosedState, 
		OpenedState, 
		PlayForwardState, 
		PlayBackwardState,
	};
	/// Set the state
	void SetState(FileInterfaceStateType State);
	/// Get the state
	FileInterfaceStateType GetState();
	//@}

	/// Interface to set a parameter with a RITKPluginConfig
	void SetParameter(QString Name, QString Value);
	


	/** @name	Input file */
	//@{
public slots:
	void SetFileName(QString FileName);
public:
	QString GetFileName();
	bool ValidFileName();
	void OpenFile();
	//@}

	/** @name	Playing modes */
	//@{
public slots:
	void Pause();
	void Stop();
	void PlayBackward();
	void PlayForward();
	void StepBackward();
	void StepForward();
	void GoToFrame(unsigned long ID);
	void ToggleLoop(bool Enabled);
	void ToggleTimestampDelivery(bool TimestampDeliveryFlag);
	/// Should be connected to the acquisition pipelines NewFrame() signal so we can tell the player widget to update its members
	void NewFrame();
	//@}
	
signals:
	/// Is emitted if a GUI updated is desired. Should be connected to the widget 
	void UpdateGUI();
	void OpenFileEvent(QString FileName);

protected:
	/// Must be provided. Returns a file reader
	SourcePointer GetSource();

	/**	@brief	Request a single frame
	 *
	 *	@details
	 *	Request a single frame. This method will eventually execute the pipeline.
	 */
	void RequestFrame();


protected slots:
	/// Is connected to the timer for playing
	void PlayEvent();
	/// Set the frame interval
	void SetFrameInterval(int Interval);

protected:
	/// The current state
	FileInterfaceStateType m_State;
	/// Mutex used to lock access to the state
	QMutex m_StateMutex;
	/// Timer for live playing
	QTimer *m_Timer;
	/// The file name of the input
	QString m_FileName;
	/// Flag that indicates whether the file is opened
	bool m_Opened;
	/// Flag that indicates whether to loop
	bool m_Loop;	
	/** @name	The actual reader */
	//@{
	typedef RImageFileSourceF2 RImageFileSourceType;
	typedef RImageFileSourceType::Pointer RImageFileSourcePointer;
	RImageFileSourcePointer m_RImageFileSource;
	//@}
	/// The current frame index
	unsigned long m_CurrentFrameID;
	/// The controlling widget
	RImageFilePlayerWidget *m_PlayerWidget;

	/// Flag that indicates whether the frames are delivered according to the timestamps
	bool m_TimestampDeliveryFlag;
	/// Frame interval in milliseconds, used when timestamp delivery flag is false
	unsigned int m_FrameInterval;

	// Flag that indicates whether the RImageFilePlayer has already been destroyed
	bool m_ValidityFlag;
	// Mutex used to lock access to the validity flag
	QMutex m_ValidityFlagMutex;	
};

} // end namespace ritk

#endif // RIMAGEFILEPLAYER_H__