package com.r3944realms.leashedplayer.content.effects.type;

import com.r3944realms.leashedplayer.content.effects.ModEffectRegister;
import com.r3944realms.leashedplayer.content.entities.LeashRopeArrow;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Leashable;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

public class NoLeashEffect extends MobEffect {

    public NoLeashEffect(MobEffectCategory pCategory, int pColor) {
        super(pCategory, pColor);
    }

    @Override
    public boolean applyEffectTick(@NotNull LivingEntity pLivingEntity, int pAmplifier) {
        MobEffectInstance effect = pLivingEntity.getEffect(ModEffectRegister.NO_LEASH_EFFECT);
        if(effect != null && effect.getDuration() != 0) {
            if (pLivingEntity instanceof Leashable leashable) {
                if (leashable.getLeashHolder() instanceof LeashRopeArrow arrow) {
                    arrow.setOwner(null);
                    leashable.dropLeash(true, false);
                }
                leashable.dropLeash(true, true);
            }
        }
        return true;
    }

    @Override
    public boolean shouldApplyEffectTickThisTick(int pDuration, int pAmplifier) {
        return true;
    }
}
