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

namespace AlienAttack.MonoGame.Things.Enemies;

public class Enemy02Green : EnemyShip
{
    public const int Width = 64;
    public const int Height = 64;

    protected int FireThreshold => 20;
    protected int CurrentFireThreshold { get; set; } = 20;
    protected override int Health { get; set; } = 5;

    public Enemy02Green(int x, int y) : base(x, y)
    {
        BoundBox = new(0, 0, Width, Height);
        YVelocity = 2;
        XVelocity = 0;
    }

    public override void Draw(SpriteDrawArgs args)
    {
        int frame = XVelocity > 0 ? 2 : XVelocity < 0 ? 3 : 1;
        SpriteEffects spriteEffects = XVelocity > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None;
        Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy02Green_Frame_{frame}_png_processed");
        //SpriteEffects spriteEffects = SpriteEffects.None;

        args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, Origin, Scale, spriteEffects, 1);

        base.Draw(args);
    }

    protected override void TryMove(SpriteUpdateContext context)
    {
        if (XPosition + BoundBox.Width >= context.ViewTransform.ScreenWidth)
        {
            XPosition = context.ViewTransform.ScreenWidth - BoundBox.Width;
            XVelocity *= -1;
        }
        else if (XPosition <= 0)
        {
            XPosition = 0;
            XVelocity *= -1;
        }
    }

    protected override void TryFire(SpriteUpdateContext context)
    {
        if (CurrentFireThreshold > 0)
        {
            CurrentFireThreshold--;
        }

        if (CurrentFireThreshold == 0 && context.Random.Next(0, 30) == 1)
        {
            float originX = XPosition + (BoundBox.Width / 2) - (7 / 2);

            //context.SpawnSprite(new MinigunBulletSmall(originX - 9, YPosition + BoundBox.Height - 12, 0, 2 + YVelocity, this));
            //context.SpawnSprite(new MinigunBulletSmall(originX + 14, YPosition + BoundBox.Height - 12, 0, 2 + YVelocity, this));

            context.SpawnSprite(new MinigunBulletSmall(originX - 9, YPosition + BoundBox.Height - 12, -1, 2 + YVelocity, this));
            context.SpawnSprite(new MinigunBulletSmall(originX + 3, YPosition + BoundBox.Height - 12, 0, 2 + YVelocity, this));
            context.SpawnSprite(new MinigunBulletSmall(originX + 14, YPosition + BoundBox.Height - 12, 1, 2 + YVelocity, this));

            CurrentFireThreshold = FireThreshold;

            context.AudioManager.PlayEnemyFire();
        }
    }
}