package dev.micle.loginprotection.events.common;

import dev.micle.loginprotection.data.ProtectedPlayer;
import dev.micle.loginprotection.data.ProtectedPlayerManager;
import dev.micle.loginprotection.setup.Config;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class OnLivingSetAttackTargetEventHandler {
    @SubscribeEvent
    public void LivingSetAttackTargetEvent(LivingSetAttackTargetEvent event) {
        if (!(event.getTarget() instanceof Player target)) {
            return;
        }

        ProtectedPlayer player = ProtectedPlayerManager.getPlayer(target.getUUID());
        if (player == null) {
            return;
        }

        // Check if mob should ignore player
        if ((player.getState().equals(ProtectedPlayer.State.JOINING) && Config.Server.LOGIN_MOBS_IGNORE_PLAYER.get()) ||
                (player.getState().equals(ProtectedPlayer.State.AFK) && Config.Server.AFK_MOBS_IGNORE_PLAYER.get()) ||
                (player.getState().equals(ProtectedPlayer.State.LOGIN_GRACE) && Config.Server.LOGIN_GRACE_MOBS_IGNORE_PLAYER.get()) ||
                (player.getState().equals(ProtectedPlayer.State.AFK_GRACE) && Config.Server.AFK_GRACE_MOBS_IGNORE_PLAYER.get())) {
            ((Mob) event.getEntityLiving()).setTarget(null);
        }
    }

}
