package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.item.ModArmorMaterials;
import com.google.common.collect.ImmutableMap;
import net.fabricmc.fabric.api.item.v1.EnchantingContext;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.EquippableComponent;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.Enchantments;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.equipment.ArmorMaterial;
import net.minecraft.item.equipment.EquipmentType;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

public class ModArmorItem extends Item {
    private static final Map<ArmorMaterial, List<StatusEffectInstance>> MATERIAL_TO_EFFECT_MAP =
            (new ImmutableMap.Builder<ArmorMaterial, List<StatusEffectInstance>>())
                    .put(ModArmorMaterials.PRISMA_STEEL_ARMOR_MATERIAL,
                            List.of(new StatusEffectInstance(StatusEffects.SPEED, 400, 1, false, false),
                                    new StatusEffectInstance(StatusEffects.JUMP_BOOST, 400, 0, false, false)))
                    .put(ModArmorMaterials.WILDFIRE_ARMOR_MATERIAL,
                            List.of(new StatusEffectInstance(StatusEffects.FIRE_RESISTANCE, 400, 0, false, false)))
//                    For more materials just add more puts
//                    .put(ModArmorMaterials.PRISMA_STEEL_ARMOR_MATERIAL,
//                            List.of(new StatusEffectInstance(StatusEffects.SPEED, 400, 2, false, false),
//                                    new StatusEffectInstance(StatusEffects.JUMP_BOOST, 400, 1, false, false)))
                    .build();

    public ModArmorItem(Settings settings) {
        super(settings);
    }

    @Override
    public void inventoryTick(ItemStack stack, ServerWorld world, Entity entity, @Nullable EquipmentSlot slot) {
        super.inventoryTick(stack, world, entity, slot);

        if(!world.isClient()) {
            if(entity instanceof PlayerEntity player) {
                evaluateArmorEffects(player);
            }
        }

        super.inventoryTick(stack, world, entity, slot);
    }

    private void evaluateArmorEffects(PlayerEntity player) {
        for (Map.Entry<ArmorMaterial, List<StatusEffectInstance>> entry : MATERIAL_TO_EFFECT_MAP.entrySet()) {
            ArmorMaterial mapArmorMaterial = entry.getKey();
            List<StatusEffectInstance> mapStatusEffects = entry.getValue();

            if(hasCorrectArmorOn(mapArmorMaterial, player)) {
                addStatusEffectForMaterial(player, mapArmorMaterial, mapStatusEffects);
            }
        }
    }

    private void addStatusEffectForMaterial(PlayerEntity player, ArmorMaterial mapArmorMaterial, List<StatusEffectInstance> mapStatusEffect) {
        boolean hasPlayerEffect = mapStatusEffect.stream().allMatch(statusEffectInstance -> player.hasStatusEffect(statusEffectInstance.getEffectType()));

        if(!hasPlayerEffect) {
            for (StatusEffectInstance instance : mapStatusEffect) {
                player.addStatusEffect(new StatusEffectInstance(instance.getEffectType(),
                        instance.getDuration(), instance.getAmplifier(), instance.isAmbient(), instance.shouldShowParticles()));
            }
        }
    }

    private boolean hasCorrectArmorOn(ArmorMaterial material, PlayerEntity player) {
        EquippableComponent equippableComponentBoots =     player.getEquippedStack(EquipmentSlot.FEET).getItem().getComponents().get(DataComponentTypes.EQUIPPABLE);
        EquippableComponent equippableComponentLeggings =  player.getEquippedStack(EquipmentSlot.LEGS).getItem().getComponents().get(DataComponentTypes.EQUIPPABLE);
        EquippableComponent equippableComponentBreastplate =  player.getEquippedStack(EquipmentSlot.CHEST).getItem().getComponents().get(DataComponentTypes.EQUIPPABLE);
        EquippableComponent equippableComponentHelmet = player.getEquippedStack(EquipmentSlot.HEAD).getItem().getComponents().get(DataComponentTypes.EQUIPPABLE);

        ArmorMaterial prismaSteelArmorMaterial = ModArmorMaterials.PRISMA_STEEL_ARMOR_MATERIAL;
        ArmorMaterial wildfireArmorMaterial = ModArmorMaterials.WILDFIRE_ARMOR_MATERIAL;

        if (material == prismaSteelArmorMaterial){
            // Check whole armor
            return (equippableComponentBoots != null && equippableComponentBoots.assetId().get().equals(material.assetId())) && (equippableComponentLeggings != null && equippableComponentLeggings.assetId().get().equals(material.assetId())) &&
                    (equippableComponentBreastplate != null && equippableComponentBreastplate.assetId().get().equals(material.assetId())) && (equippableComponentHelmet != null && equippableComponentHelmet.assetId().get().equals(material.assetId()));
        } else if (material == wildfireArmorMaterial) {
            // Just check boots
            return (equippableComponentBoots != null && equippableComponentBoots.assetId().get().equals(material.assetId()));
        }

        return false;
    }
}