using LeMangeurDEnfants.Core.Scenes;
using Microsoft.Xna.Framework;
using MonoGame.FourEngine;
using System;

namespace LeMangeurDEnfants.Core.Entities
{
    public class Blood : BaseObject
    {
        public override string FileName => Resources.Sprites.Blood;
        public override Point Size => new Point(8);

        private static Random Random;

        private Vector2 _positionVector;
        private Vector2 _speed;
        private bool _isMoving;
        private int _stopAt;

        static Blood()
        {
            Random = new Random(DateTime.UtcNow.Millisecond);
        }

        public Blood()
        {
            SpriteComponent.Frame = new Point(Random.Next(0, SpriteComponent.FrameCount.X), 0);
            _speed = new Vector2((Random.Next(0,10) - 5) / 10f, -1);
            _isMoving = true;
        }

        public override void OnAttach(Scene scene)
        {
            base.OnAttach(scene);
            _positionVector = Position.ToVector2();
            _stopAt = Position.Y + Random.Next(0, 8) - 2;
            if (Scene is GameScene gameScene)
            {
                gameScene.Camera.AttachTo(this);
            }
        }

        public override void OnUpdate(UpdateEventArgs args)
        {
            base.OnUpdate(args);
            SpriteComponent.Layer = 3;
            if (_isMoving)
            {
                _positionVector += _speed;
                _speed = new Vector2(_speed.X * 0.96f, _speed.Y + 0.05f);
                Position = _positionVector.ToPoint();
                if (Position.Y > _stopAt)
                    _isMoving = false;
            }
        }
    }
}