using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace LD20_Game
{
    static class SoundManager
    {
        private static Dictionary<string, int> delay = new Dictionary<string, int>();
        private static Dictionary<string, DateTime> lastPlayed = new Dictionary<string, DateTime>();

        private static Dictionary<string, SoundEffect> sounds = new Dictionary<string, SoundEffect>();
        public static Dictionary<string, SoundEffect> Sounds
        {
            get
            {
                return sounds;
            }
        }

        public static void Play(string soundName)
        {
            TimeSpan notPlayedIn = DateTime.Now - lastPlayed[soundName];

            if (notPlayedIn.TotalMilliseconds > delay[soundName])
            {
                sounds[soundName].Play();
                lastPlayed[soundName] = DateTime.Now;
            }
        }

        static Random rand = new Random();
        public static void PlayRandomWarning()
        {
            int warning = rand.Next(1, 4);
            Play("alarm" + warning.ToString());
        }

        public static void Add(string name, int gapBetweenPlays, SoundEffect sound)
        {
            delay.Add(name, gapBetweenPlays);
            lastPlayed.Add(name, DateTime.Now - TimeSpan.FromMilliseconds(gapBetweenPlays));
            Sounds.Add(name, sound);
        }

        public static void Add(string name, SoundEffect sound)
        {
            Add(name, 0, sound);
        }
    }
}
