#region Using directives

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
#endregion

namespace PunkMan.Game
{
    /// <summary>
    /// Sounde Effects Engine
    /// </summary>
    public class SoundFX
    {
        /// <summary>
        /// Sound Effect type
        /// </summary>
        public enum SFX
        {
            /// <summary>
            /// Rum drinking sound fx
            /// </summary>
            Rum = 0,
            /// <summary>
            /// Absinth drinking sound fx
            /// </summary>
            Absinth = 1,
            /// <summary>
            /// Player killed sound effect
            /// </summary>
            Killed = 2, 
            /// <summary>
            /// Monster killed sound effect
            /// </summary>
            Killing = 3,
            /// <summary>
            /// Shitty making sound effect ;)
            /// </summary>
            Blinker = 4,  
            /// <summary>
            /// None sound effect
            /// </summary>
            None = 5
        }

        /// <summary>
        /// DirectSound device
        /// </summary>
        private Device dev;

        /// <summary>
        /// SFX buffers
        /// </summary>
        private SecondaryBuffer[] fxs;

        /// <summary>
        /// The Constructor
        /// </summary>
        /// <param name="owner">owner game object</param>
        /// <param name="dir">SFX repository path</param>
        /// <param name="sfxFiles">array of sfx file names</param>
        public SoundFX(Game owner, string dir, string[] sfxFiles)
        {
            dev = new Device();
            dev.SetCooperativeLevel(owner, CooperativeLevel.Priority);
            fxs = new SecondaryBuffer[(int)SFX.None];
            for (int i = 0; i < (int)SFX.None; i++)
            {
                try
                {
                    fxs[i] = new SecondaryBuffer(dir + @"\" + sfxFiles[i], dev);
                }
                catch (Exception)
                {
                    fxs[i] = null;
                }
            }

        }

        /// <summary>
        /// Set/Get Sound Effects volume
        /// </summary>
        /// <value>0 = Max, -10000 = Silence</value>
        public int Volume
        {
            get { return fxs[0].Volume; }
            set { for (int i = 0; i < fxs.Length; i++) if (fxs[i] != null) fxs[i].Volume = value; }
        }

        /// <summary>
        /// Play wav file
        /// </summary>
        /// <param name="path">the wav-file path</param>
        public void Play(string path)
        {
            SecondaryBuffer buff = new SecondaryBuffer(path, dev);
            buff.Play(0, BufferPlayFlags.Default);
        }

        /// <summary>
        /// Play sound effect
        /// </summary>
        /// <param name="effect">the SFX type</param>
        public void Play(SFX effect)
        {
            if (effect == SFX.None) return;
            if (fxs[(int)effect] == null) return;
            fxs[(int)effect].Volume = Volume;
            fxs[(int)effect].Play(0, BufferPlayFlags.Default);
        }

    }
}
