package ru.dbotthepony.mc.otm.mixin.compat.ad_astra;

import earth.terrarium.ad_astra.common.util.OxygenUtils;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
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.CallbackInfoReturnable;
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
import ru.dbotthepony.mc.otm.config.ServerCompatConfig;

@Mixin(OxygenUtils.class)
public class OxygenUtilsMixin {
	@Inject(
		method = "entityHasOxygen(Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/LivingEntity;)Z",
		at = @At("HEAD"),
		cancellable = true,
		remap = false
	)
	private static void entityHasOxygen(Level level, LivingEntity entity, CallbackInfoReturnable<Boolean> hook) {
		if (entity instanceof Player && ServerCompatConfig.AdAstra.INSTANCE.getANDROIDS_DO_NOT_NEED_OXYGEN()) {
			entity.getCapability(MatteryCapability.MATTERY_PLAYER).ifPresent(it -> {
				if (it.isAndroid()) {
					hook.setReturnValue(true);
				}
			});
		}
	}
}
