using AlienAttack.MonoGame.Things.Muzzles;

namespace AlienAttack.MonoGame.Things.Weapons;

public class ConfigurableWeapon(WeaponDef def, WeaponState state) : Weapon
{
    public override int FireThreshold => state.FireThreshold;

    public override void Fire(Sprite owner, SpriteUpdateContext context)
    {
        if (owner is not Player player) return;

        foreach (var shot in def.Pattern.Shots)
        {
            // muzzle local -> world
            var muzzleLocal = player.GetMuzzleLocal(shot.Muzzle); // make a fast lookup
            var muzzleWorld = MuzzleMath.GetMuzzleWorld(player, muzzleLocal);

            // speed upgrade
            float vx = shot.Velocity.X * state.SpeedMultiplier;
            float vy = shot.Velocity.Y * state.SpeedMultiplier;

            // center bullet on muzzle + pattern offset
            var pos = MuzzleMath.CenterBulletOn(muzzleWorld, def.Bullet.Width, def.Bullet.Height) + shot.Offset;

            var bullet = def.Bullet.Create(pos.X, pos.Y, vx, vy, owner);

            // optional: apply damage bonus generically
            //bullet.Damage += state.DamageBonus;

            context.SpawnSprite(bullet);
        }

        //context.AudioManager.PlayPlayerFire(); // or use _def.FireSfxKey -> audio routing
        context.AudioManager.PlayPlayerFire(def.Bullet.Kind);
    }
}