package com.acethewildfire.acesbs.enchantment.custom;

import com.mojang.serialization.MapCodec;
import net.minecraft.enchantment.EnchantmentEffectContext;
import net.minecraft.enchantment.effect.EnchantmentEntityEffect;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;

public record LightningStrikeEnchantmentEffect() implements EnchantmentEntityEffect {
    public static final MapCodec<LightningStrikeEnchantmentEffect> CODEC = MapCodec.unit(LightningStrikeEnchantmentEffect::new);

    @Override
    public void apply(ServerWorld world, int level, EnchantmentEffectContext context, Entity user, Vec3d pos) {
        if(level == 1){
            EntityType.LIGHTNING_BOLT.spawn(world, user.getBlockPos(), SpawnReason.TRIGGERED);
        }
        if(level == 2){
            EntityType.LIGHTNING_BOLT.spawn(world, new BlockPos(((int) pos.x),((int) pos.y),((int) pos.z)), SpawnReason.TRIGGERED);
            EntityType.LIGHTNING_BOLT.spawn(world, new BlockPos(((int) pos.x),((int) pos.y),((int) pos.z)), SpawnReason.TRIGGERED);
        }
    }

    @Override
    public MapCodec<? extends EnchantmentEntityEffect> getCodec() {
        return CODEC;
    }
}
