using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Items;

namespace AlienAttack.MonoGame.Things.Enemies;

public abstract class Enemy(int x, int y) : MoveableSprite(x, y)
{
    protected abstract int Health { get; set; }

    public override void Update(SpriteUpdateContext context)
    {
        CheckDeath(context);

        base.Update(context);
    }

    private void CheckDeath(SpriteUpdateContext context)
    {
        if (YPosition > context.ViewTransform.ScreenHeight)
        {
            IsDead = true;
            return;
        }

        if (Health <= 0)
        {
            IsDead = true;
            context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));

            switch (context.Random.Next(0, 5))
            {
                case 0:
                    context.SpawnSprite(new Health((int)XPosition, (int)YPosition));
                    break;
                case 1:
                    context.SpawnSprite(new Shields((int)XPosition, (int)YPosition));
                    break;
                case 2:
                    context.SpawnSprite(new Ammo((int)XPosition, (int)YPosition));
                    break;
                case 3:
                    context.SpawnSprite(new Energy((int)XPosition, (int)YPosition));
                    break;
                case 4:
                    context.SpawnSprite(new Rockets((int)XPosition, (int)YPosition));
                    break;
            }

            return;
        }
    }

    public override void OnCollision(SpriteCollisionContext context)
    {
        if (context.Sprite is Bullet bullet && bullet.Owner is Player)
        {
            Health -= bullet.Damage;
        }
    }
}