﻿//-----------------------------------------------------------------------
// <copyright file="NotificationSoundPlayer" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox 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 the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Media
{
    using System;
    using System.Media;
    using System.Windows;
    using System.Windows.Resources;

    using ChatBox.DomainObjects;

    /// <summary>
    /// Class to play the notification sound on request
    /// </summary>
    public sealed class NotificationSoundPlayer : IDisposable
    {
        /// <summary>
        /// The sound player for the notifcation sound
        /// </summary>
        private SoundPlayer notificationPlayer;

        /// <summary>
        /// The sound player for the beep sound
        /// </summary>
        private SoundPlayer beepPlayer;

        /// <summary>
        /// The sound player for the error sound
        /// </summary>
        private SoundPlayer errorPlayer;

        /// <summary>
        /// Play the sound for notification
        /// </summary>
        public void PlayNotification()
        {
            if (this.notificationPlayer == null)
            {
                this.notificationPlayer = MakeSoundPlayer("Media\\notification.wav");
            }

            if (this.notificationPlayer != null)
            {
                this.notificationPlayer.Play();
            }
        }

        /// <summary>
        /// Play the sound for a simple beep
        /// </summary>
        public void PlayBeep()
        {
            if (this.beepPlayer == null)
            {
                this.beepPlayer = MakeSoundPlayer("Media\\beep.wav");
            }

            if (this.beepPlayer != null)
            {
                this.beepPlayer.Play();
            }
        }

        /// <summary>
        /// Play the sound for an error
        /// </summary>
        public void PlayError()
        {
            if (this.errorPlayer == null)
            {
                this.errorPlayer = MakeSoundPlayer("Media\\fail.wav");
            }

            if (this.errorPlayer != null)
            {
                this.errorPlayer.Play();
            }
        }

        #region IDisposable Members

        /// <summary>
        /// IDisposable member
        /// </summary>
        public void Dispose()
        {
            this.DisposeInternal();
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Internal dispose method
        /// </summary>
        private void DisposeInternal()
        {
            if (this.notificationPlayer != null)
            {
                this.notificationPlayer.Dispose();
                this.notificationPlayer = null;
            }

            if (this.beepPlayer != null)
            {
                this.beepPlayer.Dispose();
                this.beepPlayer = null;
            }

            if (this.errorPlayer != null)
            {
                this.errorPlayer.Dispose();
                this.errorPlayer = null;
            }
        }

        #endregion

        /// <summary>
        /// Create a media player
        /// </summary>
        /// <param name="soundResourcePath">the path to the solund resource</param>
        /// <returns>the player for that sound</returns>
        private static SoundPlayer MakeSoundPlayer(string soundResourcePath)
        {
            Uri resourceUri = new Uri(soundResourcePath, UriKind.Relative);
            StreamResourceInfo soundStreamInfo = Application.GetResourceStream(resourceUri);
            if (soundStreamInfo == null)
            {
                throw new ChatBoxException("could not find sound resource " + soundResourcePath);
            }

            return new SoundPlayer(soundStreamInfo.Stream);
        }
    }
}
