using AlienAttack.MonoGame.Things.Bullets;
using AlienAttack.MonoGame.Things.Items;
using AlienAttack.MonoGame.Things.Weapons;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace AlienAttack.MonoGame.Things.Enemies;

internal class GreenEnemy : EnemyShip
{
    //Enemy01_Green_Frame_1_png_processed

    protected ICollection<IWeapon> ActiveWeapons = [];
    protected int FireThreshold => 20;
    protected int CurrentFireThreshold { get; set; } = 20;
    protected int Health { get; set; } = 5;

    public GreenEnemy(int x, int y) : base(x, y)
    {
        BoundBox = new Rectangle(0, 0, 64, 64);
        YVelocity = 1;
        ActiveWeapons.Add(new Minigun());
        //ActiveWeapons.Add(new FastMinigun());
    }

    public override void Draw(SpriteDrawArgs args)
    {
        Texture2D texture = args.Content.Load<Texture2D>(@$"Sprites\Enemy01_Green_Frame_1_png_processed");
        SpriteEffects spriteEffects = SpriteEffects.None;

        args.SpriteBatch.Draw(texture, Position, null, DrawColor, 0, new Vector2(0, 0), 1, spriteEffects, 1);

        base.Draw(args);
    }

    public override void Update(SpriteUpdateContext context)
    {
        //YPosition += 1;

        if (Health <= 0)
        {
            IsDead = true;
            SpawnExplosion(context);

            switch (context.Random.Next(0, 5))
            {
                case 0:
                    context.SpawnSprite(new Health((int)XPosition, (int)YPosition));
                    break;
                case 1:
                    context.SpawnSprite(new Shields((int)XPosition, (int)YPosition));
                    break;
                case 2:
                    context.SpawnSprite(new Ammo((int)XPosition, (int)YPosition));
                    break;
                case 3:
                    context.SpawnSprite(new Energy((int)XPosition, (int)YPosition));
                    break;
                case 4:
                    context.SpawnSprite(new Rockets((int)XPosition, (int)YPosition));
                    break;
            }

            return;
        }

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

        if (CurrentFireThreshold > 0)
        {
            CurrentFireThreshold--;
        }

        if (CurrentFireThreshold == 0 && context.Random.Next(0, 100) == 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));

            CurrentFireThreshold = FireThreshold;

            context.AudioManager.PlayEnemyFire();
        }

        //CheckMove(context);
        CheckFire(context);

        base.Update(context);
    }

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

        //int number = context.Random.Next(1, 7);

        //SoundEffect soundEffect = context.Content.Load<SoundEffect>(@$"Sfx\Explosions\EXPLDsgn_Explosion Impact_0{number}_SFRMS_SCIWPNS");
        //soundEffect.Play(0.95f, (float)(context.Random.NextDouble() * 0.1 - 0.05), 0);

        context.AudioManager.PlayExplosion();
    }

    private void CheckFire(SpriteUpdateContext context)
    {
        //foreach (IWeapon weapon in ActiveWeapons)
        //{
        //    weapon.UpdateFireThreshold();
        //}

        //foreach (IWeapon weapon in ActiveWeapons)
        //{
        //    weapon.TryFire(this, context);
        //}
    }

    public override void OnCollision(SpriteCollisionContext context)
    {
        if (context.Sprite is Bullet bullet && bullet.Owner is Player)
        {
            Health -= bullet.Damage;
        }

        if (context.Sprite is Player)
        {
            Health = 0;
        }
    }
}