using System.Collections.Generic;
using Decimation.Content.Buffs.Buffs;
using Decimation.Content.Items.Misc.Souls;
using Decimation.Content.Tiles;
using Decimation.Lib.Items;
using Decimation.Lib.Util;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;

namespace Decimation.Content.Items.Armors.ScarabArmor
{
    [AutoloadEquip(EquipType.Head)]
    internal class ScarabHelmet : DecimationItem
    {
        protected override string ItemName => "Solar Scarab Helmet";

        protected override string ItemTooltip => "25 % increased melee critical hit chances" +
                                                 "\nEnemis are more likely to target you";

        protected override void Init()
        {
            item.width = 26;
            item.height = 26;
            item.rare = Rarity.Red.GetRarityValue();

            this.item.maxStack = 1;
            this.item.defense = 30;
        }

        public override void UpdateEquip(Player player)
        {
            player.meleeCrit += 25;
            player.aggro += 250;
            Lighting.AddLight((int) (player.position.X + player.width / 2f) / 16,
                (int) (player.position.Y + player.height / 2f) / 16, 1.05f, 0.95f, 0.55f);
        }

        public override bool IsArmorSet(Item head, Item body, Item legs)
        {
            if (head.type == item.type && body.type == ModContent.ItemType<ScarabBody>() &&
                legs.type == ModContent.ItemType<ScarabLeggings>())
                return true;
            return false;
        }

        public override void UpdateArmorSet(Player player)
        {
            DecimationPlayer modPlayer = player.GetModPlayer<DecimationPlayer>();

            player.AddBuff(ModContent.BuffType<ScarabEndurance>(), 1);
            player.arrowDamage *= 1.05f;
            player.bulletDamage *= 1.05f;
            player.magicDamage *= 1.05f;
            player.meleeDamage *= 1.05f;
            player.minionDamage *= 1.05f;
            player.rangedDamage *= 1.05f;
            player.rocketDamage *= 1.05f;
            player.thrownDamage *= 1.05f;
            player.buffImmune[BuffID.OnFire] = true;
            player.buffImmune[BuffID.CursedInferno] = true;
            player.lavaImmune = true;

            player.setSolar = true;
            player.setBonus =
                "Summons scarabs to protect you, add 5% damages to each attacks, gives immunity to On Fire!, Cursed Inferno and Lava.";
            modPlayer.solarCounter++;
            int num11 = 240;
            if (modPlayer.solarCounter >= num11)
            {
                if (player.solarShields > 0 && player.solarShields < 3)
                    for (int num12 = 0; num12 < 22; num12++)
                        if (player.buffType[num12] >= 170 && player.buffType[num12] <= 171)
                            player.DelBuff(num12);
                if (player.solarShields < 3)
                {
                    player.AddBuff(170 + player.solarShields, 5, false);
                    for (int num13 = 0; num13 < 16; num13++)
                    {
                        Dust dust = Main.dust[
                            Dust.NewDust(player.position, player.width, player.height, 6, 0f, 0f, 100)];
                        dust.noGravity = true;
                        dust.scale = 1.7f;
                        dust.fadeIn = 0.5f;
                        Dust dust2 = dust;
                        dust2.velocity *= 5f;
                        dust.shader = GameShaders.Armor.GetSecondaryShader(player.ArmorSetDye(), player);
                    }

                    modPlayer.solarCounter = 0;
                }
                else
                {
                    modPlayer.solarCounter = num11;
                }
            }

            for (int num14 = player.solarShields; num14 < 3; num14++) player.solarShieldPos[num14] = Vector2.Zero;
            for (int num15 = 0; num15 < player.solarShields; num15++)
            {
                player.solarShieldPos[num15] += player.solarShieldVel[num15];
                Vector2 value = (player.miscCounter / 100f * 6.28318548f + num15 * (6.28318548f / player.solarShields))
                                .ToRotationVector2() * 6f;
                value.X = player.direction * 20;
                player.solarShieldVel[num15] = (value - player.solarShieldPos[num15]) * 0.2f;
            }

            if (player.dashDelay >= 0)
            {
                player.solarDashing = false;
                player.solarDashConsumedFlare = false;
            }

            bool flag = player.solarDashing && player.dashDelay < 0;
            if ((player.solarShields > 0) | flag) player.dash = 3;
        }


        protected override List<ModRecipe> GetRecipes()
        {
            ModRecipe recipe = GetNewModRecipe(this, 1, new List<int> {ModContent.TileType<TitanForge>()});

            recipe.AddIngredient(ItemID.SolarFlareHelmet);
            recipe.AddIngredient(ItemID.BeetleHelmet);
            recipe.AddIngredient(ItemID.LunarOre, 15);
            recipe.AddIngredient(ItemID.SoulofMight, 5);
            recipe.AddIngredient(ItemID.SoulofFright, 5);
            recipe.AddIngredient(ModContent.ItemType<SoulofSpite>(), 5);
            recipe.AddIngredient(ItemID.LavaBucket);

            return new List<ModRecipe> {recipe};
        }
    }
}