using Microsoft.Xna.Framework.Audio;

namespace MonoGame.FourEngine.Resources
{
    public class Music : IResource
    {
        /// <summary>
        /// Permanent resources are not deleted on ResourceManager.Clear
        /// </summary>
        public bool Permanent { get; protected set; }

        /// <summary>
        /// SoundEffect data
        /// </summary>
        public SoundEffect SoundEffect { get; }

        /// <summary>
        /// Create new Music instance
        /// </summary>
        /// <param name="def">Music parameters</param>
        public Music(MusicDefinition def)
        {
            SoundEffect = ReadSoundEffect(def.FileName);
        }

        private SoundEffect ReadSoundEffect(string fileName)
        {
            using (var stream = ServiceLocator.ResourceLoader.GetStream(fileName))
                return SoundEffect.FromStream(stream);
        }

        public void Dispose()
            => SoundEffect?.Dispose();
    }

    /// <summary>
    /// Defines Music parameters
    /// </summary>
    public class MusicDefinition : IResourceDefinition
    {
        /// <summary>
        /// SoundEffect file name
        /// </summary>
        public string FileName { get; set; }
    }
}