package com.acethewildfire.acesbs.entity;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.entity.custom.CombustibleLemonEntity;
import com.acethewildfire.acesbs.entity.custom.FrenEntity;
import com.acethewildfire.acesbs.entity.custom.VulgarBones;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.util.ConversionManager;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.entity.mob.SkeletonEntity;
import net.minecraft.entity.mob.ZombieEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;

public class ModEntities {

    private static final RegistryKey<EntityType<?>> FREN_KEY =
            RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of(AcesBS.MOD_ID, "fren"));
    private static final RegistryKey<EntityType<?>> C_LEMON_KEY =
            RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of(AcesBS.MOD_ID, "combustible_lemon"));
    private static final RegistryKey<EntityType<?>> VULGAR_BONES_KEY =
            RegistryKey.of(RegistryKeys.ENTITY_TYPE, Identifier.of(AcesBS.MOD_ID, "vulgar_bones"));

    public static final EntityType<FrenEntity> FREN = Registry.register(Registries.ENTITY_TYPE,
            Identifier.of(AcesBS.MOD_ID, "fren"),
            EntityType.Builder.create(FrenEntity::new, SpawnGroup.CREATURE)
                    .dimensions(0.6F, 1.7F).build(FREN_KEY));

    public static final EntityType<CombustibleLemonEntity> C_LEMON = Registry.register(Registries.ENTITY_TYPE,
            Identifier.of(AcesBS.MOD_ID, "combustible_lemon"),
            EntityType.Builder.<CombustibleLemonEntity>create(CombustibleLemonEntity::new, SpawnGroup.MISC)
                    .dimensions(0.5F, 0.5F).build(C_LEMON_KEY));

    public static final EntityType<VulgarBones> VULGAR_BONES = Registry.register(Registries.ENTITY_TYPE,
            Identifier.of(AcesBS.MOD_ID, "vulgar_bones"),
            EntityType.Builder.create(VulgarBones::new, SpawnGroup.MONSTER)
                    .dimensions(0.6F, 1.99F).build(VULGAR_BONES_KEY));


    public static void registerModEntities() {

        UseEntityCallback.EVENT.register((player, world, hand, entity, hitResult) -> {

            if (world.isClient()) return ActionResult.PASS;

            if (!(entity instanceof SkeletonEntity skeleton)) return ActionResult.PASS;

            if ((entity instanceof VulgarBones vb)) return ActionResult.PASS;

            ItemStack stack = player.getStackInHand(hand);

            if (!stack.isOf(ModItems.STABLE_ENTROPY)) return ActionResult.PASS;

            skeleton.setAiDisabled(true);
            skeleton.setInvulnerable(true);

            world.playSound(null, skeleton.getBlockPos(), SoundEvents.ENTITY_EVOKER_CAST_SPELL,
                    skeleton.getSoundCategory(), 1.0f, 1.0f);

            ((ServerWorld) world).spawnParticles(
                    ParticleTypes.ENCHANT,
                    skeleton.getX(),
                    skeleton.getY() + 1.0,
                    skeleton.getZ(),
                    5,     // count
                    0.2,   // dx
                    0.2,   // dy
                    0.2,   // dz
                    0.0    // speed
            );

            ConversionManager.startConversion(skeleton, 20);

            if (!player.getAbilities().creativeMode) {
                stack.decrement(1);
            }

            return ActionResult.SUCCESS;
        });

        AcesBS.LOGGER.info("Registering Mod Entities for " + AcesBS.MOD_ID);
    }
}
