package fr.koka99cab.ibelievechickenfly.mixin;

import fr.koka99cab.ibelievechickenfly.chickenfly.ModChickenVariants;
import fr.koka99cab.ibelievechickenfly.registry.ModItems;
import java.util.function.BiConsumer;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.animal.chicken.Chicken;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.storage.loot.LootTable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(Chicken.class)
public abstract class ChickenMixin {
	private static final int GOLDEN_EGG_LAY_CHANCE = 256;

	@Redirect(
		method = "aiStep",
		at = @At(
			value = "INVOKE",
			target = "Lnet/minecraft/world/entity/animal/chicken/Chicken;dropFromGiftLootTable(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/resources/ResourceKey;Ljava/util/function/BiConsumer;)Z"
		)
	)
	private boolean ibelievechickenfly$dropSpecialEgg(Chicken chicken, ServerLevel serverLevel, ResourceKey<LootTable> lootTable, BiConsumer<ServerLevel, ItemStack> dropper) {
		if (ModChickenVariants.isBlue(chicken)) {
			dropper.accept(serverLevel, new ItemStack(Items.DIAMOND));
			return true;
		}

		if (chicken.getRandom().nextInt(GOLDEN_EGG_LAY_CHANCE) == 0) {
			dropper.accept(serverLevel, new ItemStack(ModItems.GOLDEN_EGG));
			return true;
		}

		return chicken.dropFromGiftLootTable(serverLevel, lootTable, dropper);
	}
}
