using Decimation.Content.Buffs.Debuffs;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace Decimation.Content.Projectiles
{
    public class GreenSlime : DecimationProjectile
    {
        private static readonly int SlimedBuff = ModContent.BuffType<Slimed>();
        
        public override string Texture => "Terraria/Projectile_" + ProjectileID.SlimeGun;

        protected override void Init()
        {
            projectile.CloneDefaults(ProjectileID.SlimeGun);
            projectile.aiStyle = -1;
        }

        public override void AI()
        {
            projectile.scale -= 0.015f;
            if (projectile.scale <= 0f)
            {
                projectile.velocity *= 5f;
                projectile.oldVelocity = projectile.velocity;
                projectile.Kill();
            }

            if (projectile.ai[0] > 3f)
            {
                if (projectile.owner == Main.myPlayer)
                {
                    Rectangle projRectangle = new Rectangle((int) projectile.position.X, (int) projectile.position.Y,
                        projectile.width, projectile.height);
                    for (int n = 0; n < 200; n++)
                        if (Main.npc[n].active && !Main.npc[n].dontTakeDamage && Main.npc[n].lifeMax > 1)
                        {
                            Rectangle npcRectangle = new Rectangle((int) Main.npc[n].position.X,
                                (int) Main.npc[n].position.Y, Main.npc[n].width, Main.npc[n].height);
                            if (projRectangle.Intersects(npcRectangle))
                            {
                                Main.npc[n].AddBuff(SlimedBuff, 1500);
                                projectile.Kill();
                            }
                        }

                    for (int p = 0; p < 255; p++)
                        if (p != projectile.owner && Main.player[p].active && !Main.player[p].dead)
                        {
                            Rectangle playerRectangle = new Rectangle((int) Main.player[p].position.X,
                                (int) Main.player[p].position.Y, Main.player[p].width, Main.player[p].height);
                            if (projRectangle.Intersects(playerRectangle))
                            {
                                Main.player[p].AddBuff(SlimedBuff, 1500, false);
                                projectile.Kill();
                            }
                        }
                }

                projectile.ai[0] += Main.rand.Next(0, 5);
                if (projectile.ai[0] > 30f) projectile.velocity.Y += 0.1f;

                int dustAlpha = 175;
                Color dustColor = new Color(0, 255, 60, 100);
                for (int i = 0; i < 6; i++)
                {
                    Vector2 val57 = projectile.velocity * i / 6f;
                    int num622 = 6;
                    int dust = Dust.NewDust(projectile.position + Vector2.One * 6f, projectile.width - num622 * 2,
                        projectile.height - num622 * 2, 4, 0f, 0f, dustAlpha, dustColor, 1.2f);
                    Dust dust96;
                    Dust dust2;
                    if (Main.rand.Next(2) == 0)
                    {
                        dust96 = Main.dust[dust];
                        dust2 = dust96;
                        dust2.alpha += 25;
                    }

                    if (Main.rand.Next(2) == 0)
                    {
                        dust96 = Main.dust[dust];
                        dust2 = dust96;
                        dust2.alpha += 25;
                    }

                    if (Main.rand.Next(2) == 0)
                    {
                        dust96 = Main.dust[dust];
                        dust2 = dust96;
                        dust2.alpha += 25;
                    }

                    Main.dust[dust].noGravity = true;
                    dust96 = Main.dust[dust];
                    dust2 = dust96;
                    dust2.velocity *= 0.3f;
                    dust96 = Main.dust[dust];
                    dust2 = dust96;
                    dust2.velocity += projectile.velocity * 0.5f;
                    Main.dust[dust].position = projectile.Center;
                    Main.dust[dust].position.X -= val57.X;
                    Main.dust[dust].position.Y -= val57.Y;
                    dust96 = Main.dust[dust];
                    dust2 = dust96;
                    dust2.velocity *= 0.2f;
                }

                if (Main.rand.Next(4) == 0)
                {
                    int num624 = 6;
                    int num625 = Dust.NewDust(projectile.position + Vector2.One * 6f, projectile.width - num624 * 2,
                        projectile.height - num624 * 2, 4, 0f, 0f, dustAlpha, dustColor, 1.2f);
                    Dust dust97 = Main.dust[num625];
                    Dust dust2 = dust97;
                    dust2.velocity *= 0.5f;
                    dust97 = Main.dust[num625];
                    dust2 = dust97;
                    dust2.velocity += projectile.velocity * 0.5f;
                }
            }
            else
            {
                projectile.ai[0] += 1f;
            }
        }
    }
}