using AlienAttack.MonoGame.Things.Bullets;
using Microsoft.Xna.Framework;

namespace AlienAttack.MonoGame.Things.Enemies;

public abstract class EnemyShip : MoveableSprite
{
    protected abstract int Health { get; set; }

    public virtual int CrashDamage => 10;

    public EnemyShip(int x, int y) : base(x, y)
    {
        Origin = new(32, 32);
    }

    public override sealed void Update(SpriteUpdateContext context)
    {
        if (Health <= 0)
        {
            IsDead = true;
            SpawnExplosion(context);
            OnKilled(context);
            return;
        }

        base.Update(context);

        TryMove(context);

        if (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
        {
            IsDead = true;
            return;
        }

        TryFire(context);
    }

    protected virtual void TryMove(SpriteUpdateContext context)
    {

    }

    protected virtual void TryFire(SpriteUpdateContext context)
    {

    }

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

        if (context.Sprite is Player)
        {
            Health = 0;
        }
    }

    private void SpawnExplosion(SpriteUpdateContext context)
    {
        context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
        context.AudioManager.PlayExplosion();
    }

    protected virtual void OnKilled(SpriteUpdateContext context)
    {

    }
}