using Microsoft.Xna.Framework;
using MonoGame.FourEngine;
using MonoGame.FourEngine.Components;
using MonoGame.FourEngine.Resources;

namespace LeMangeurDEnfants.Core.Entities
{
    public class Background : Entity
    {        
        protected static readonly Point Size = new Point(320, 320);
        public const int LimitY = 60;

        #region Sprite Definitions
        protected static SpriteDefinition Layer1Definition
            => new SpriteDefinition
            {
                FileName = Resources.Sprites.Background.Layer1,
                Size = Size
            };
        protected static SpriteDefinition Layer2Definition
            => new SpriteDefinition
            {
                FileName = Resources.Sprites.Background.Layer2,
                Size = Size
            };
        #endregion

        #region Sprite Components
        public readonly SpriteComponent Layer1Component;
        public readonly SpriteComponent Layer2Component;
        #endregion
        
        public Point Position
        {
            get => _position;
            set => OnPositionChanged(_position = value);
        }
        private Point _position;
                
        public Background()
        {
            Components.Add(Layer1Component = new SpriteComponent(Layer1Definition));
            Components.Add(Layer2Component = new SpriteComponent(Layer2Definition));
            Position = Point.Zero;
            Layer1Component.Layer = int.MinValue;
            Layer2Component.Layer = int.MinValue;
        }

        protected virtual void OnPositionChanged(Point position)
        {
            Layer1Component.Position = new Point(position.X, -position.Y);
            Layer2Component.Position = new Point(position.X, position.Y - LimitY);
        }
    }
}