using System;
using System.Windows.Forms;

using QuartzTypeLib;

namespace KelimeEzber.General
{
	/// <summary>
	/// Summary description for Mp3Play.
	/// </summary>
	public class Mp3Play
	{
		private string sFileName="";
		private string Word="";

		private const int WM_APP = 0x8000;
		private const int WM_GRAPHNOTIFY = WM_APP + 1;
		private const int EC_COMPLETE = 0x01;
		private const int WS_CHILD = 0x40000000;
		private const int WS_CLIPCHILDREN = 0x2000000;

		private FilgraphManager m_objFilterGraph = null;
		private IBasicAudio m_objBasicAudio = null;
		private IVideoWindow m_objVideoWindow = null;
		private IMediaEvent m_objMediaEvent = null;
		private IMediaEventEx m_objMediaEventEx = null;
		private IMediaPosition m_objMediaPosition = null;
		private IMediaControl m_objMediaControl = null;

		enum MediaStatus { None, Stopped, Paused, Running };

		private MediaStatus m_CurrentStatus = MediaStatus.None;

		public string sPlayWord
		{
			get { return this.Word;}
			set 
			{ 
				if (this.Word != value)
				{
					string dirAudioFilePath="";
					int iKonum=0;
					iKonum = value.IndexOfAny(new char[]{'\r', '\'', ',', '!'});
					if (iKonum > 0)
					{
						this.Word = value.Substring(0, iKonum);
					}else this.Word = value;
					if (Word.Length == 0) return;

					dirAudioFilePath = System.Configuration.ConfigurationSettings.AppSettings["audio"] + "\\" +
								this.Word.Substring(0,1);

					System.IO.DirectoryInfo objDir = new System.IO.DirectoryInfo(dirAudioFilePath);

					foreach (System.IO.FileInfo objFile in objDir.GetFiles(this.Word + ".*")) 
					{
						this.sFileName = objFile.FullName;
						PlayFile();
						break;
					}
				}
			}
		}

		public Mp3Play()
		{
			//
			// TODO: Add constructor logic here
			//
			CleanUp();
		}

		public void PlayFile()
		{
			CleanUp();

			m_objFilterGraph = new FilgraphManager();
			m_objFilterGraph.RenderFile(this.sFileName);

			m_objBasicAudio = m_objFilterGraph as IBasicAudio;
                
			m_objVideoWindow = null;

			m_objMediaEvent = m_objFilterGraph as IMediaEvent;

			m_objMediaEventEx = m_objFilterGraph as IMediaEventEx;

			m_objMediaPosition = m_objFilterGraph as IMediaPosition;

			m_objMediaControl = m_objFilterGraph as IMediaControl;

			m_objMediaControl.Run();
			m_CurrentStatus = MediaStatus.Running;

		}


		public void StopFile()
		{
			m_objMediaPosition.CurrentPosition = 0;
			m_CurrentStatus = MediaStatus.Stopped;
		}

		public void PauseFile()
		{
			m_objMediaControl.Pause();
			m_CurrentStatus = MediaStatus.Paused;
		}

		private void CleanUp()
		{
			if (m_objMediaControl != null)
				m_objMediaControl.Stop();

			m_CurrentStatus = MediaStatus.Stopped;

			if (m_objMediaEventEx != null)
				m_objMediaEventEx.SetNotifyWindow(0, 0, 0);

			if (m_objVideoWindow != null)
			{
				m_objVideoWindow.Visible = 0;
				m_objVideoWindow.Owner = 0;
			}

			if (m_objMediaControl != null) m_objMediaControl = null;
			if (m_objMediaPosition != null) m_objMediaPosition = null;
			if (m_objMediaEventEx != null) m_objMediaEventEx = null;
			if (m_objMediaEvent != null) m_objMediaEvent = null;
			if (m_objVideoWindow != null) m_objVideoWindow = null;
			if (m_objBasicAudio != null) m_objBasicAudio = null;
			if (m_objFilterGraph != null) m_objFilterGraph = null;
		}

	}
}
