using MonoGame.FourEngine.Components;
using MonoGame.FourEngine.Resources;

namespace MonoGame.FourEngine.Entities
{
    public class MusicEntity : Entity
    {
        protected MusicComponent Component { get; set; }

        public float Pitch
        {
            get => Component.Pitch;
            set => Component.Pitch = value;
        }

        public float Pan
        {
            get => Component.Pan;
            set => Component.Pan = value;
        }

        public float Volume
        {
            get => Component.Volume;
            set => Component.Volume = value;
        }

        public MusicEntity()
        {
        }

        public void Play(string fileName)
            => Play(new MusicDefinition { FileName = fileName });

        public void Play(MusicDefinition def)
        {
            Component?.Dispose();
            Components.Clear();
            CreateComponent(def);
            Component.Play();
        }

        protected virtual void CreateComponent(MusicDefinition def)
        {
            Components.Add(Component = new MusicComponent(def));
        }
        
        public override void Dispose()
        {
            base.Dispose();
            Component?.Dispose();
        }
    }
}