package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.explosion.Explosion;

public class CigaretteLemon extends Item {
    public CigaretteLemon(Settings settings) {
        super(settings);
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        if (!user.getItemCooldownManager().isCoolingDown(this)) {
            user.setCurrentHand(hand);
            user.getStackInHand(hand).set(ModDataComponentTypes.SMOKING, true);
            return TypedActionResult.consume(user.getStackInHand(hand));
        }
        return TypedActionResult.fail(user.getStackInHand(hand));
    }

    @Override
    public UseAction getUseAction(ItemStack stack) {
        return UseAction.TOOT_HORN;
    }

    @Override
    public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
        super.onStoppedUsing(stack, world, user, remainingUseTicks);
        stack.set(ModDataComponentTypes.SMOKING, false);
    }

    @Override
    public SoundEvent getEatSound() {
        return SoundEvents.INTENTIONALLY_EMPTY;
    }

    @Override
    public int getMaxUseTime(ItemStack stack, LivingEntity user) {
        return 50; // basically infinite hold
    }

    @Override
    public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
        super.usageTick(world, user, stack, remainingUseTicks);

        if (world.isClient) {

            // Direction player is looking
            Vec3d look = user.getRotationVec(1.0f);

            // Spawn position (near mouth)
            double x = user.getX() + look.x * 0.3;
            double y = user.getEyeY() - 0.15;
            double z = user.getZ() + look.z * 0.3;

            // Base outward velocity
            double speed = 0.08;

            // Add small randomness so it spreads naturally
            double vx = look.x * speed + (world.random.nextDouble() - 0.5) * 0.02;
            double vy = 0.02 + (world.random.nextDouble() * 0.02); // slight upward drift
            double vz = look.z * speed + (world.random.nextDouble() - 0.5) * 0.02;

            if (remainingUseTicks % 4 == 0) {
                world.addParticle(
                        ParticleTypes.SMOKE,
                        x, y, z,
                        vx, vy, vz
                );
            }
        }

        // Serverside code
        if (!world.isClient){
            if (remainingUseTicks % 40 == 0) {
                world.playSound(
                        null,
                        user.getX(),
                        user.getY(),
                        user.getZ(),
                        SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE,
                        SoundCategory.PLAYERS,
                        1.0f,
                        1.0f
                );
            }
        }

    }

    @Override
    public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
        // Eye position of the player
        Vec3d eyePos = user.getCameraPosVec(1.0F);

        for (int i = 0; i < 6; i++) {
            double offsetX = (world.random.nextDouble() - 0.5) * 2 * 0.5f;
            double offsetY = (world.random.nextDouble() - 0.5) * 2 * 0.5f;
            double offsetZ = (world.random.nextDouble() - 0.5) * 2 * 0.5f;

            world.addParticle(
                    ParticleTypes.EXPLOSION,
                    eyePos.x + offsetX,
                    eyePos.y + offsetY,
                    eyePos.z + offsetZ,
                    1, 0, 0
            );
        }
        stack.set(ModDataComponentTypes.SMOKING, false);
        Explosion explosion = new Explosion(
                world,
                user,
                user.getX(),
                user.getY(),
                user.getZ(),
                4.0f,
                false,
                Explosion.DestructionType.KEEP
        );
        explosion.collectBlocksAndDamageEntities();
        user.damage(user.getDamageSources().explosion(user, user), 20F);
        explosion.affectWorld(true);



        if (user instanceof PlayerEntity player) {
            player.getItemCooldownManager().set(this, 2400);
            stack.decrement(1);
        }
        return stack;
    }
}
