//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if WIN32
using System;
using System.IO;
using System.Media;
using u = user_rtx;

namespace user_rtx {

/// <summary>
/// Sound playback from stream or file
/// </summary>
[u.win32]
public static class sound_player {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[u.lock_free][u.volatile_access]
public static bool IsMute {
   get => _isMute;
   set => _isMute = value; }

//==================================================================================================

/// <exception cref="u.EOperationFailed" />
public static bool PlayStream( Stream stream ) =>
   PlayStream( stream, false );

/// <exception cref="u.EOperationFailed" />
public static bool PlayStream( Stream stream, bool ignoreMute ) =>
   PlayStream( stream, ignoreMute, false );

/// <exception cref="u.EOperationFailed" />
public static bool PlayStream( Stream stream, bool ignoreMute, bool synchronously )
{
   if ( !ignoreMute && IsMute )
      return false;
   try {
      var soundPlayer = new SoundPlayer { Stream = stream };
      if ( synchronously )
         soundPlayer.PlaySync();
      else
         soundPlayer.Play();
   } catch ( Exception e ) {
      throw e_operation_failed.New( e );
   }
   return true;
}

//==================================================================================================

/// <exception cref="u.EOperationFailed" />
public static bool PlayFile( string filePath ) =>
   PlayFile( filePath, false );

/// <exception cref="u.EOperationFailed" />
public static bool PlayFile( string filePath, bool ignoreMute ) =>
   PlayFile( filePath, ignoreMute, false );

/// <exception cref="u.EOperationFailed" />
public static bool PlayFile( string filePath, bool ignoreMute, bool synchronously )
{
   if ( !ignoreMute && IsMute )
      return false;
   try {
      var soundPlayer = new SoundPlayer( filePath );
      if ( synchronously )
         soundPlayer.PlaySync();
      else
         soundPlayer.Play();
   } catch ( Exception e ) {
      throw e_operation_failed.New( e );
   }
   return true;
}

//==================================================================================================

/// <exception cref="u.EOperationFailed" />
public static void SyncPlayStream( Stream stream ) =>
   SyncPlayStream( stream, false );

/// <exception cref="u.EOperationFailed" />
public static void SyncPlayStream( Stream stream, bool ignoreMute ) =>
   PlayStream( stream, ignoreMute, true );

//--------------------------------------------------------------------------------------------------

/// <exception cref="u.EOperationFailed" />
public static bool SyncPlayFile( string filePath ) =>
   SyncPlayFile( filePath, false );

/// <exception cref="u.EOperationFailed" />
public static bool SyncPlayFile( string filePath, bool ignoreMute ) =>
   PlayFile( filePath, ignoreMute, true );

//==================================================================================================

static volatile bool _isMute;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // sound_player

} // user_rtx
#endif // WIN32
