using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using Mox.Shared;

namespace Mox.SharpDX
{
	public class Sound : SoundBase
	{
		public Sound(string _sSoundId, string _sPath)
		{
			m_sSoundId = _sSoundId;
			m_MediaPlayer = new MediaPlayer();
			m_MediaPlayer.Open(new Uri("bridge\\" + _sPath, UriKind.Relative));
		}
		
		[DllImport("winmm.dll")]
		private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

		public override void Play(bool _bLooped = false)
		{
			m_MediaPlayer.Position = TimeSpan.Zero;
			
			m_MediaPlayer.MediaEnded -= Replay;
			if (_bLooped)
			{
				m_MediaPlayer.MediaEnded += Replay;
			}

			m_MediaPlayer.Play();
		}

		private void Replay(object _sender, EventArgs _e)
		{
			m_MediaPlayer.MediaEnded -= Replay;
			Play(true);
		}

		public override void Stop()
		{
			m_MediaPlayer.Stop();
		}

		private readonly string m_sSoundId;
		private MediaPlayer m_MediaPlayer;
	}
}
