#region Using directives

using System;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
using System.Collections;
using System.IO;
#endregion

namespace PunkMan.Game
{
    /// <summary>
    /// MP3 Jukebox
    /// </summary>
    public class Jukebox : IDisposable
    {
        /// <summary>
        /// Number of playing item in playlist
        /// </summary>
        protected int itemNo = 0;
        /// <summary>
        /// Playbeck audio object
        /// </summary>
        protected Audio playback = null;
        /// <summary>
        /// Jukebox playlist
        /// </summary>
        protected ArrayList playlist;
        /// <summary>
        /// Constructor
        /// </summary>
        public Jukebox()
        {
            playlist = new ArrayList();
            
        }

        /// <summary>
        /// volume of jukebox
        /// </summary>
        private int volume;
        /// <summary>
        /// get/set jukebox volume
        /// </summary>
        /// <value>0 = maximum, -10000 = silence</value>
        public int Volume
        {
            get { return volume; }
            set {
                if (playback != null) playback.Volume = value;
                volume = value; 
            }
        }

        /// <summary>
        /// Add song into playlist
        /// </summary>
        /// <param name="fileName">MP3 song file path</param>
        public void AddSong(string fileName)
        {
            playlist.Add(fileName);
        }

        /// <summary>
        /// play jukebox
        /// </summary>
        public void Play()
        {
            if (playlist.Count < 1) return;
            if (itemNo < 0) itemNo = 0;
            if (itemNo >= playlist.Count) itemNo = 0;

            if (playback != null)
            {
                if (playback.Playing) playback.Stop();

                playback.Open((string)playlist[itemNo]);
                playback.Volume = volume;
            }
            else
            {
                playback = new Audio((string)playlist[itemNo]);
                playback.Ending += new EventHandler(OnEnding);
            }
            playback.Play();
            
        }

        /// <summary>
        /// previous song
        /// </summary>
        public void Prev()
        {
            if (playback.Playing) playback.Stop();
            itemNo--;
            if (itemNo < 0) itemNo = playlist.Count - 1;
            Play();
        }

        /// <summary>
        /// next song in jukebox
        /// </summary>
        public void Next()
        {
            if (playback.Playing) playback.Stop();
            itemNo++;
            if (itemNo >= playlist.Count) itemNo = 0;
            Play();
        }

        /// <summary>
        /// Clear jukebox playlist
        /// </summary>
        public void Clear()
        {
            playlist.Clear();
        }

        /// <summary>
        /// Stop jukebox
        /// </summary>
        public void Stop()
        {
            if (playback.Playing) playback.Stop();
        }

        /// <summary>
        /// On song ending event handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void OnEnding(object sender, EventArgs args)
        {
            Next();
        }

        /// <summary>
        /// Load playlist from directory
        /// </summary>
        /// <param name="dir">playlist directory path</param>
        public void PlaylistFromDirector(string dir)
        {
            DirectoryInfo di = new DirectoryInfo(dir);
            foreach (FileInfo fi in di.GetFiles())
                AddSong(fi.FullName);
        }

        /// <summary>
        /// Dispose playback
        /// </summary>
        public void Dispose()
        {
            if (playback.Playing)
                playback.Stop();
            playback.Dispose();
        }


    }
}
