package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.particle.ModParticles;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.equipment.ArmorMaterial;
import net.minecraft.item.equipment.EquipmentType;
import net.minecraft.network.packet.s2c.play.ParticleS2CPacket;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class WildfireArmorItem extends Item {

    int tick = 0;

    public WildfireArmorItem(Settings settings) {
        super(settings);
    }

    @Override
    public void inventoryTick(ItemStack stack, ServerWorld world, Entity entity, @Nullable EquipmentSlot slot) {
        if(!world.isClient()) {
            if(entity instanceof PlayerEntity player) {
                ItemStack bootsStack = player.getEquippedStack(EquipmentSlot.FEET);
                if (!bootsStack.isEmpty() && bootsStack.getItem() == ModItems.WILDFIRE_BOOTS){
                    double x = player.getX();
                    double y = player.getY(); // feet level
                    double z = player.getZ();

                    ParticleS2CPacket packet;


                    if (Objects.equals(player.getName().getLiteralString(), "Acelib25")){
                        packet = switch (tick % 4){
                            case 0 -> new ParticleS2CPacket(
                                    ParticleTypes.FLAME,
                                    true, false,
                                    (float) x, (float) y + 0.2f, (float) z,
                                    0.2f, 0f, 0.2f,
                                    0.01f,
                                    1
                            );
                            case 1 -> new ParticleS2CPacket(
                                    ParticleTypes.SOUL_FIRE_FLAME,
                                    true, false,
                                    (float) x, (float) y + 0.2f, (float) z,
                                    0.2f, 0f, 0.2f,
                                    0.01f,
                                    1
                            );
                            case 2 -> new ParticleS2CPacket(
                                    ModParticles.PURPLE_FLAME,
                                    true, false,
                                    (float) x, (float) y + 0.2f, (float) z,
                                    0.2f, 0f, 0.2f,
                                    0.01f,
                                    1
                            );
                            case 3 -> new ParticleS2CPacket(
                                    ModParticles.GREEN_FLAME,
                                    true, false,
                                    (float) x, (float) y + 0.2f, (float) z,
                                    0.2f, 0f, 0.2f,
                                    0.01f,
                                    1
                            );
                            default -> throw new IllegalStateException("Unexpected value: " + tick % 4);
                        };
                    } else {
                        packet = new ParticleS2CPacket(
                                ParticleTypes.FLAME,
                                true,
                                false,
                                x, y + 0.2, z,
                                0.2f, 0f, 0.2f,
                                0.01f,
                                1
                        );
                    }


                    tick++;

                    if (tick >= 200){
                        tick = 0;
                    }

                    BlockPos pos = new BlockPos((int) x, (int) y, (int) z);

                    // Send to all players in the world near that position
                    for (PlayerEntity player_i : world.getPlayers()) {
                        if (player.getBlockPos().isWithinDistance(pos, 128)) {
                            if (player_i instanceof ServerPlayerEntity serverPlayer) {
                                serverPlayer.networkHandler.sendPacket(packet);
                            }
                        }

                    }
                }
            }
        }

        super.inventoryTick(stack, world, entity, slot);
    }
}
