using Microsoft.Xna.Framework;
using MonoGame.FourEngine;
using MonoGame.FourEngine.Components;
using MonoGame.FourEngine.Interfaces;
using MonoGame.FourEngine.Raycast;
using Raycast.Win32.Scenes;

namespace Raycast.Win32.Entities
{
    public class Door : RaycastObject, IRaycastObject, IHaveSizeAndPosition
    {
        private RenderedScene _scene;
        
        private SfxComponent _sfx;

        public Door()
        {
            Components.Add(_sfx = new SfxComponent(Resources.Sfx.DoorOpen));
        }

        public void Open()
        {
            if (_scene == null) return;

            _sfx.Play();
            var mapPoint = Position.ToPoint();
            _scene.View.Map.Layers[0].Data[mapPoint.X, mapPoint.Y]++;
            _scene.View.Component.OpeningDoor = mapPoint;
            _scene.RemoveEntity(this);
        }

        public override void OnAttach(Scene scene)
        {
            _scene = scene as RenderedScene;
        }

        public override void OnDetach(Scene scene)
        {
            _scene = null;
        }

        #region IHaveSizeAndPosition
        Point IHaveSizeAndPosition.Position
        {
            get => Position.ToPoint();
            set => Position = value.ToVector2();
        }
        int IHaveSizeAndPosition.Width { get; set; }
        int IHaveSizeAndPosition.Height { get; set; }
        #endregion

        #region IHaveReadOnlySizeAndPosition
        Point IHaveReadOnlySizeAndPosition.Position => ((IHaveSizeAndPosition)this).Position;
        int IHaveReadOnlySizeAndPosition.Width => ((IHaveSizeAndPosition)this).Width;
        int IHaveReadOnlySizeAndPosition.Height => ((IHaveSizeAndPosition)this).Height;
        #endregion
    }
}
