using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;


namespace Blindfold.Managers
{
    public class SoundManager
    {
        protected Dictionary<string, SoundEffectInfo> soundEffectDictonary;
        protected AudioListener listener;
        private Main game;

        #region PROPERTIES
        
        #endregion

        public SoundManager(Main game)
        {
            this.game = game;
            listener = new AudioListener();
            soundEffectDictonary = new Dictionary<string, SoundEffectInfo>();
        }

        public bool add(SoundEffectInfo effectInfo)
        {
            if (!soundEffectDictonary.ContainsKey(effectInfo.NAME))
            {
                soundEffectDictonary.Add(effectInfo.NAME, effectInfo);
                return true;
            }

            return false;
        }

        public SoundEffectInfo getEffectInfo(string name)
        {
            return soundEffectDictonary[name];
        }

        public SoundEffectInstance getEffectInstance(string name)
        {
            SoundEffectInfo effectInfo = soundEffectDictonary[name];
            SoundEffectInstance soundEffectInstance = effectInfo.SOUNDEFFECT.CreateInstance();
            soundEffectInstance.Volume = effectInfo.VOLUME;
            soundEffectInstance.Pitch = effectInfo.PITCH;
            soundEffectInstance.Pan = effectInfo.PAN;
            soundEffectInstance.IsLooped = effectInfo.LOOP;
            return soundEffectInstance;
        }

        public void playSound(string name, Vector3 position)
        {
            SoundEffectInstance sei = getEffectInstance(name);
            UpdateListener();
            AudioEmitter emitter = new AudioEmitter();
            emitter.Position = position;
            sei.Apply3D(listener, emitter);
            sei.Play();
        }

        public void playSound(SoundEffectInstance instance, Vector3 position)
        {
            SoundEffectInstance sei = instance;
            UpdateListener();
            AudioEmitter emitter = new AudioEmitter();
            emitter.Position = position;
            emitter.Forward = game.PLAYER.POSITION - position;
            emitter.Velocity = new Vector3(10, 10, 10);
            sei.Apply3D(listener, emitter);
            sei.Play();
        }

        public void UpdateListener()
        {
            listener.Position = game.PLAYER.POSITION;
            listener.Forward = new Vector3(-game.PLAYER.LOOK.Y,0,game.PLAYER.LOOK.X);
            listener.Velocity = new Vector3(10,10,10);
        }

        public bool remove(string name)
        {
            //find effect info so we can nullify for garbage collection
            SoundEffectInfo effectInfo = soundEffectDictonary[name];
            //remove the effect info from dictionary and store return value
            bool wasRemoved = soundEffectDictonary.Remove(name);
            //nullify for garbage collection
            effectInfo.dispose();
            return wasRemoved;
        }

        public void clear()
        {
            soundEffectDictonary.Clear();
        }

        public int size()
        {
            return soundEffectDictonary.Count;
        }
    }

    
}
