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

namespace AlienAttack.MonoGame.Things.Enemies.Mines;

public abstract class Mine : MoveableSprite
{
    public const int Width = 65;
    public const int Height = 64;

    protected float Rotation = 0;

    protected abstract string CoverColor { get; }

    public Mine(int x, int y) : base(x, y)
    {
        Origin = new(30, 33);
        BoundBox = new(10, 14, 40, 38);
        YVelocity = 1;
        XVelocity = 0;
    }

    public override void Draw(SpriteDrawArgs args)
    {
        DrawRotor(args);
        DrawCover(args);
        //DrawCollisionBox(args);
    }

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

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

    private void DrawCollisionBox(SpriteDrawArgs args)
    {
        //var pixel = DebugPixel; // static cached

        Texture2D pixel = new Texture2D(args.SpriteBatch.GraphicsDevice, 1, 1);
        pixel.SetData(new[] { Color.White });

        //Rectangle r = GetWorldCollisionBox();

        Color c = Color.LimeGreen; // debug color

        // Top
        args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, CollisionBox.Width, 1), c);
        // Bottom
        args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Bottom - 1, CollisionBox.Width, 1), c);
        // Left
        args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.X, CollisionBox.Y, 1, CollisionBox.Height), c);
        // Right
        args.SpriteBatch.Draw(pixel, new Rectangle(CollisionBox.Right - 1, CollisionBox.Y, 1, CollisionBox.Height), c);
    }

    //Rectangle GetWorldCollisionBox()
    //{
    //    int x = (int)(XPosition - Origin.X + BoundBox.X);
    //    int y = (int)(YPosition - Origin.Y + BoundBox.Y);

    //    return new Rectangle(x, y, BoundBox.Width, BoundBox.Height);
    //}

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

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

        float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
        float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);

        double distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);

        if (distance < 100)
        {
            IsDead = true;
            SpawnExplosion(context);
            return;
        }

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

        Rotation += 0.01f;

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

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