using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AlienAttack.MonoGame.Things.Enemies.Turrets;

public abstract class Turret : MoveableSprite
{
    public const int MountWidth = 63;
    public const int MountHeight = 63;

    public const int TurretWidth = 38;
    public const int TurretHeight = 33;

    public const int GunWidth = 8;
    public const int GunHeight = 38;

    protected Vector2 MountOrigin = new(MountWidth / 2, MountHeight /2);
    protected Vector2 TurretOrigin = new(TurretWidth / 2, TurretHeight / 2);
    protected Vector2 GunOrigin = new(GunWidth / 2, GunHeight / 2);

    protected abstract string TurretColor { get; }

    public Turret(int x, int y) : base(x, y)
    {
        BoundBox = new(10, 10, 44, 44); // TODO
        YVelocity = 1;
        XVelocity = 0;
    }

    public override void Draw(SpriteDrawArgs args)
    {
        DrawMount(args);
        DrawTurret(args);
        DrawGun(args);
    }

    private void DrawMount(SpriteDrawArgs args)
    {
        Texture2D texture = args.Content.Load<Texture2D>("Sprites/GunTurretMount");
        args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, MountOrigin, 1f, SpriteEffects.None, 1);
    }

    private void DrawTurret(SpriteDrawArgs args)
    {
        Texture2D texture = args.Content.Load<Texture2D>($"Sprites/GunTurret_{TurretColor}");
        args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, TurretOrigin, 1f, SpriteEffects.None, 1);
    }

    private void DrawGun(SpriteDrawArgs args)
    {
        Texture2D texture = args.Content.Load<Texture2D>("Sprites/GunTurret_ExampleGun");
        args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, GunOrigin, 1f, SpriteEffects.None, 1);
    }

    public override sealed void Update(SpriteUpdateContext context)
    {
        base.Update(context);

        CollisionBox = new Rectangle((int)XPosition + BoundBox.X - (int)MountOrigin.X, (int)YPosition + BoundBox.Y - (int)MountOrigin.Y, BoundBox.Width, BoundBox.Height);

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

        Rotation += 0.01f;

        if (Rotation > 360f)
        {
            Rotation = 0;
        }
    }
}