using Microsoft.Xna.Framework;
using MonoGame.FourEngine;
using MonoGame.FourEngine.Entities;
using MonoGame.FourEngine.Resources;
using System;

namespace LeMangeurDEnfants.Core.Entities
{
    public abstract class BaseObject : BasicEntity
    {
        public abstract string FileName { get; }
        public abstract Point Size { get; }
        public virtual Point HalfSize { get; }

        protected Scene Scene { get; private set; }

        protected override SpriteDefinition SpriteDefinition
            => new SpriteDefinition
            {
                FileName = FileName,
                Size = Size
            };

        public override Point Position
        {
            get => base.Position + HalfSize;
            set => base.Position = value - HalfSize;
        }

        public BaseObject()
        {
            HalfSize = new Point(Size.X / 2, Size.Y / 2);
        }

        public virtual bool Collide(Point dot)
        {
            var rect = new Rectangle(Position, new Point(Width, Height));
            return rect.Contains(dot);
        }

        public override void OnAttach(Scene scene)
        {
            base.OnAttach(scene);
            Scene = scene;
        }

        public override void OnUpdate(UpdateEventArgs args)
        {
            base.OnUpdate(args);
            SpriteComponent.Layer = Position.Y + Size.Y;
        }
    }
}