using System;
using System.Linq;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;
using Terraria.GameContent.ItemDropRules;
using Continuity.Content.Items;
using Continuity.Content.Items.Materials;

namespace Continuity.Content
{
    public class PlanteraDrops : GlobalNPC
    {
        public override void ModifyNPCLoot(NPC npc, NPCLoot npcLoot)
        {
            if (npc.type == NPCID.Plantera) 
            {
                LeadingConditionRule notExpertRule = new LeadingConditionRule(new Conditions.NotExpert());
                notExpertRule.OnSuccess(ItemDropRule.Common(ModContent.ItemType<PalishadeTissue>(), 1, 10, 35));
                npcLoot.Add(notExpertRule);
            }
        }
    }

    [JITWhenModsEnabled("CalamityMod")]
    public class PlanteraBossBag : GlobalItem
    {
        public override void ModifyItemLoot(Item item, ItemLoot itemLoot)
        {
            short[] toRemove = {
                ItemID.TheAxe,
                ItemID.PygmyStaff,
                ItemID.Seedling,
                ItemID.ThornHook,
            };
            foreach (var i in toRemove)
            {
                itemLoot.RemoveWhere(rule => rule is CommonDropNotScalingWithLuck drop
                    && drop.itemId == i);
            }

            if (item.type == ItemID.PlanteraBossBag)
            {
                itemLoot.Add(ItemDropRule.Common(ModContent.ItemType<PalishadeTissue>(), 1, 25, 35));

                itemLoot.RemoveWhere(rule => rule is CommonDrop drop
                    && drop.itemId == ItemID.TempleKey);

                foreach (var rule in itemLoot.Get())
                {
                    itemLoot.RemoveWhere(rule => rule is OneFromRulesRule);

                    if (ModLoader.HasMod("CalamityMod"))
                    {
                        itemLoot.RemoveWhere(rule => rule is CalamityMod.DropHelper.AllOptionsAtOnceWithPityDropRule);

                        if (ModLoader.TryGetMod("CalamityMod", out Mod calamityMod) && calamityMod.TryFind<ModItem>("BlossomFlux", out ModItem BlossomFlux)) {
                            itemLoot.RemoveWhere(rule => rule is CommonDrop drop
                                && drop.itemId == BlossomFlux.Type);
                        }
                    }
                }
            }
        }
    }
}
