package net.mcreator.customoregen.item;

import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.biome.Biome;

import java.util.*;

public class OreBiomeFinderItem extends Item {

	// Tags personnalisés du mod
	private static final TagKey<Biome> COLD_BIOMES_TAG = TagKey.create(Registries.BIOME,
			ResourceLocation.fromNamespaceAndPath("custom_ore_gen", "cold_biomes"));
	private static final TagKey<Biome> HOT_BIOMES_TAG = TagKey.create(Registries.BIOME,
			ResourceLocation.fromNamespaceAndPath("custom_ore_gen", "hot_biomes"));
	private static final TagKey<Biome> MOUNTAIN_BIOMES_TAG = TagKey.create(Registries.BIOME,
			ResourceLocation.fromNamespaceAndPath("custom_ore_gen", "mountain_biomes"));
	private static final TagKey<Biome> TEMPERED_BIOMES_TAG = TagKey.create(Registries.BIOME,
			ResourceLocation.fromNamespaceAndPath("custom_ore_gen", "tempered_biomes"));
	private static final TagKey<Biome> RARE_BIOMES_TAG = TagKey.create(Registries.BIOME,
			ResourceLocation.fromNamespaceAndPath("custom_ore_gen", "rare_biomes"));

	// Minerais par catégorie
	private static final List<String> COLD_ORES = Arrays.asList(
			"Lapis (stone) [Y: 0 à 32]",
			"Lapis (deepslate) [Y: -64 à 0]",
			"Diamant (deepslate) [Y: -64 à 0]");

	private static final List<String> HOT_ORES = Arrays.asList(
			"Or Pur (stone) [Y: 0 à 256]",
			"Or Pur (deepslate) [Y: -64 à 0]",
			"Redstone (stone) [Y: -10 à 20]",
			"Redstone (deepslate) [Y: -80 à -30]",
			"Cuivre (haut) [Y: 15 à 256]",
			"Cuivre (bas) [Y: -64 à 0]");

	private static final List<String> MOUNTAIN_ORES = Arrays.asList(
			"Emeraude (haute altitude) [Y: 0 à 64]");

	private static final List<String> TEMPERED_ORES = Arrays.asList(
			"Charbon Concentre [Y: 0 à 70]",
			"Fer (stone) [Y: 0 à 100]",
			"Fer (deepslate) [Y: -64 à 0]");

	private static final List<String> RARE_ORES = Arrays.asList(
			"Emeraude (basse altitude) [Y: -64 à 0]");

	private static final List<String> EVERYWHERE_ORES = Arrays.asList(
			"Diamant Shard (deepslate) [Y: -64 à -40]",
			"Bloc Diamant Shard [Y: 0 à 15]");

	public OreBiomeFinderItem() {
		super(new Item.Properties().stacksTo(1));
	}

	@Override
	public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
		ItemStack stack = player.getItemInHand(hand);

		if (!level.isClientSide) {
			BlockPos pos = player.blockPosition();
			var biomeHolder = level.getBiome(pos);
			ResourceKey<Biome> biomeKey = biomeHolder.unwrapKey()
					.orElse(ResourceKey.create(Registries.BIOME, ResourceLocation.fromNamespaceAndPath("minecraft", "plains")));
			ResourceLocation biomeId = biomeKey.location();
			String biomeName = biomeId.getPath();

			player.displayClientMessage(Component.literal("=== Minerais dans: " + biomeName + " ==="), false);
			player.displayClientMessage(Component.literal("Biome ID: " + biomeId), false);

			// Vérifier les tags personnalisés du mod
			Set<String> foundTags = new HashSet<>();
			List<String> foundOres = new ArrayList<>();

			// Vérifier chaque tag de biome et ajouter les minerais correspondants
			if (isBiomeInTag(level, pos, COLD_BIOMES_TAG)) {
				foundTags.add("cold_biomes");
				foundOres.addAll(COLD_ORES);
			}
			if (isBiomeInTag(level, pos, HOT_BIOMES_TAG)) {
				foundTags.add("hot_biomes");
				foundOres.addAll(HOT_ORES);
			}
			if (isBiomeInTag(level, pos, MOUNTAIN_BIOMES_TAG)) {
				foundTags.add("mountain_biomes");
				foundOres.addAll(MOUNTAIN_ORES);
			}
			if (isBiomeInTag(level, pos, TEMPERED_BIOMES_TAG)) {
				foundTags.add("tempered_biomes");
				foundOres.addAll(TEMPERED_ORES);
			}
			if (isBiomeInTag(level, pos, RARE_BIOMES_TAG)) {
				foundTags.add("rare_biomes");
				foundOres.addAll(RARE_ORES);
			}

			// Ajouter les minerais présents partout
			foundOres.addAll(EVERYWHERE_ORES);

			// Afficher les tags trouvés
			if (!foundTags.isEmpty()) {
				player.displayClientMessage(Component.literal("Tags du mod: " + String.join(", ", foundTags)), false);
			} else {
				player.displayClientMessage(Component.literal("Tags du mod: aucun"), false);
			}

			// Afficher les minerais (sans doublons)
			if (!foundOres.isEmpty()) {
				Set<String> uniqueOres = new LinkedHashSet<>(foundOres);
				player.displayClientMessage(Component.literal("Minerais trouvables:"), false);
				for (String ore : uniqueOres) {
					player.displayClientMessage(Component.literal("  * " + ore), false);
				}
			} else {
				player.displayClientMessage(Component.literal("Aucun minerai specifique"), false);
			}
		}

		return InteractionResultHolder.sidedSuccess(stack, level.isClientSide);
	}

	private static boolean isBiomeInTag(Level level, BlockPos pos, TagKey<Biome> tag) {
		return level.getBiome(pos).is(tag);
	}
}
