package ru.dbotthepony.mc.otm.mixin;

import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.projectile.AbstractHurtingProjectile;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.dbotthepony.mc.otm.capability.IMatteryPlayer;
import ru.dbotthepony.mc.otm.core.IMatteryLevel;
import ru.dbotthepony.mc.otm.registry.game.MSoundEvents;

@Mixin(AbstractHurtingProjectile.class)
public class MixinAbstractHurtingProjectile {
	@Inject(
		method = "onDeflection(Lnet/minecraft/world/entity/Entity;Z)V",
		remap = false,
		at = @At("TAIL")
	)
	public void onDeflection(Entity entity, boolean byAttack, CallbackInfo info) {
		if (entity instanceof IMatteryPlayer player) {
			var cap = player.getOtmPlayer();

			AbstractHurtingProjectile proj = (AbstractHurtingProjectile)(Object)this;

			if (cap.isAndroid() && proj.getOwner() != entity) {
				entity.level().playSound(entity, proj.blockPosition(), MSoundEvents.INSTANCE.getANDROID_PROJ_PARRY(), SoundSource.PLAYERS, 1.0f, 0.95f + ((IMatteryLevel) entity.level()).getOtmRandom().nextFloat() * 0.1f);
			}
		}
	}
}
