using Microsoft.Xna.Framework;

namespace MonoGame.FourEngine.Raycast
{
    public class RaycastViewEntity : Entity
    {
        public IRaycastMap Map { get; }
        public IRaycastCamera Camera { get; }
        public Point Resolution { get; }

        private RaycastComponent _component;

        public RaycastViewEntity(IRaycastMap map, IRaycastCamera camera, Point resolution)
        {
            Map = map;
            Camera = camera;
            Resolution = resolution;
        }

        protected virtual RaycastComponent CreateComponent(IRaycastMap map, Scene scene, IRaycastCamera camera, Point resolution)
            => new RaycastComponent(map, scene, camera, resolution);

        public override void OnAttach(Scene scene)
        {
            base.OnAttach(scene);
            _component?.Dispose();
            Components.Clear();
            Components.Add(_component = CreateComponent(Map, scene, Camera, Resolution));
        }

        public override void OnDetach(Scene scene)
        {
            base.OnDetach(scene);
            _component?.Dispose();
            Components.Clear();
            _component = null;
        }
    }
}