package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import com.acethewildfire.acesbs.effect.ModEffects;
import com.acethewildfire.acesbs.sounds.ModSounds;
import com.acethewildfire.acesbs.sounds.PlayerAttachedSound;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
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.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;

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

    // Store the currently playing sound (client only)
    private static PlayerAttachedSound activeSound;

    @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 void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
        super.onStoppedUsing(stack, world, user, remainingUseTicks);

        if (activeSound != null) {
            MinecraftClient.getInstance().getSoundManager().stop(activeSound);
            activeSound = null;
        }

        stack.set(ModDataComponentTypes.SMOKING, false);
    }

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

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

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

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

        if (world.isClient) {

            if (activeSound == null) {
                activeSound = new PlayerAttachedSound(
                        ((PlayerEntity) user),
                        ModSounds.SMOKING
                );

                MinecraftClient.getInstance().getSoundManager().play(activeSound);
            }
            // 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.3;

            for (int i = 0; i < 40; i++) {
                double vx = look.x * speed + (world.random.nextDouble() - 0.5) * 0.3;
                double vy = look.y * speed + (world.random.nextDouble() - 0.5) * 0.1;
                double vz = look.z * speed + (world.random.nextDouble() - 0.5) * 0.3;

                world.addParticle(
                        ParticleTypes.CAMPFIRE_COSY_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) {
        user.addStatusEffect(new StatusEffectInstance(
                ModEffects.CIGARETTE,
                6000,
                0
        ));
        stack.set(ModDataComponentTypes.SMOKING, false);
        if (user instanceof PlayerEntity player) {
            player.getItemCooldownManager().set(this, 2400);
            stack.decrement(1);
        }
        return stack;
    }
}
