using AlienAttack.MonoGame.Things.Items;
using AlienAttack.MonoGame.Things.Muzzles;
using Microsoft.Xna.Framework;
using System;

namespace AlienAttack.MonoGame.Things.Bullets;

public class Bullet(float x, float y, float xVel, float yVel, Sprite owner) : Sprite(x, y)
{
    protected float XVelocity = xVel;
    protected float YVelocity = yVel;
    public Sprite Owner { get; protected set; } = owner;
    public int Damage { get; protected set; } = 0;

    public override void Draw(SpriteDrawArgs args)
    {
        base.Draw(args);
    }

    public override void Update(SpriteUpdateContext context)
    {
        XPosition += XVelocity;
        YPosition += YVelocity;

        if (XPosition + BoundBox.Width < 0
            || XPosition > context.ViewTransform.ScreenWidth
            || YPosition + BoundBox.Height < 0
            || YPosition > context.ViewTransform.ScreenHeight)
        {
            IsDead = true;
        }

        base.Update(context);
    }

    public override void OnCollision(SpriteCollisionContext context)
    {
        Sprite sprite = context.Sprite;

        if (sprite is Bullet || sprite == Owner || sprite is Item || sprite is Explosion)
            return;

        IsDead = true;

        float xVel = 0;
        float yVel = 0;

        if (context.Sprite is MoveableSprite moveableSprite)
        {
            xVel = moveableSprite.XVelocity;
            yVel = moveableSprite.YVelocity;
        }

        context.SpawnSprite(new MiniExplosion((int)XPosition, (int)YPosition, xVel, yVel));

        context.AudioManager.PlayImpact();
    }
}