package ru.dbotthepony.mc.otm.mixin;

import net.minecraft.world.Difficulty;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodData;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
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.MatteryCapability;

@Mixin(FoodData.class)
public class FoodDataMixin {
	@Shadow
	private int lastFoodLevel;
	@Shadow
	private int tickTimer;
	@Shadow
	private int foodLevel;
	@Shadow
	private float exhaustionLevel;

	@Inject(
		method = "tick(Lnet/minecraft/world/entity/player/Player;)V",
		at = @At("HEAD"),
		cancellable = true
	)
	private void tick(Player player, CallbackInfo info) {
		player.getCapability(MatteryCapability.MATTERY_PLAYER).ifPresent(it -> {
			if (it.isAndroid()) {
				info.cancel();

				// полностью подменяем логику если андроид
				lastFoodLevel = foodLevel;

				if (player.level().getDifficulty() == Difficulty.PEACEFUL) {
					exhaustionLevel = 0f;
				} else {
					tickTimer = 0;
				}

				// не обновляем уровень истощения ибо он обнуляется логикой внутри MatteryPlayerCapability
				// а так же не регенерируем
				// ну и не получаем урон от "голодания"
			}
		});
	}
}
