package com.acethewildfire.acesbs.util;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.entity.ModEntities;
import com.acethewildfire.acesbs.entity.custom.VulgarBones;
import com.acethewildfire.acesbs.sounds.ModSounds;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EquipmentSlot;
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.server.MinecraftServer;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class ConversionManager {

    private static final List<ConversionEntry> conversions = new ArrayList<>();

    public static void init() {
        ServerTickEvents.END_SERVER_TICK.register(ConversionManager::tick);
    }

    public static void startConversion(SkeletonEntity skeleton, int ticks) {

        conversions.add(new ConversionEntry(
                skeleton.getUuid(),
                skeleton.getWorld().getRegistryKey(),
                ticks
        ));
    }

    private static void tick(MinecraftServer server) {

        Iterator<ConversionEntry> iterator = conversions.iterator();

        while (iterator.hasNext()) {

            AcesBS.LOGGER.info("Ticking");

            ConversionEntry entry = iterator.next();

            ServerWorld world = server.getWorld(entry.worldKey);

            if (world == null) {
                iterator.remove();
                continue;
            }

            Entity entity = world.getEntity(entry.entityId);

            if (!(entity instanceof SkeletonEntity skeleton)) {
                iterator.remove();
                continue;
            }

            entry.ticksRemaining--;

            if (entry.ticksRemaining <= 0) {

                VulgarBones v_bones = ModEntities.VULGAR_BONES.create(world);

                if (v_bones == null) {
                    iterator.remove();
                    continue;
                }

                world.playSound(null, skeleton.getBlockPos(),
                        ModSounds.VULGAR_BONES_SPAWN,
                        skeleton.getSoundCategory(), 1f, 1f);

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

                v_bones.refreshPositionAndAngles(
                        skeleton.getX(),
                        skeleton.getY(),
                        skeleton.getZ(),
                        skeleton.getYaw(),
                        skeleton.getPitch()
                );

                v_bones.equipStack(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));

                v_bones.prevYaw = skeleton.prevYaw;
                v_bones.prevPitch = skeleton.prevPitch;

                v_bones.setBodyYaw(skeleton.getBodyYaw());
                v_bones.setHeadYaw(skeleton.getHeadYaw());

                world.spawnEntity(v_bones);

                skeleton.discard();
                iterator.remove();
            }
        }
    }
}
