package com.acethewildfire.acesbs.item.custom;

import com.acethewildfire.acesbs.AcesBS;
import com.acethewildfire.acesbs.component.ModDataComponentTypes;
import com.acethewildfire.acesbs.effect.ModEffects;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.sounds.ModSounds;
import com.acethewildfire.acesbs.sounds.PlayerAttachedSound;
import com.acethewildfire.acesbs.util.ModStats;
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.item.consume.UseAction;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import static com.acethewildfire.acesbs.util.ModStats.grantAdvancement;

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

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

    @Override
    public ActionResult use(World world, PlayerEntity user, Hand hand) {
        if (!user.getItemCooldownManager().isCoolingDown(user.getStackInHand(hand))) {
            user.setCurrentHand(hand);
            user.getStackInHand(hand).set(ModDataComponentTypes.SMOKING, true);
            return ActionResult.CONSUME;
        }
        return ActionResult.FAIL;
    }

    @Override
    public boolean 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;
//        }

        if (user instanceof PlayerEntity player) {
            if (!world.isClient()) {
                player.incrementStat(ModStats.CIGARETTES_USED);
                player.incrementStat(Stats.USED.getOrCreateStat(ModItems.CIGARETTE_FUNNY));
                if (player instanceof ServerPlayerEntity s_player){
                    int uses = s_player.getStatHandler().getStat(Stats.CUSTOM.getOrCreateStat(ModStats.CIGARETTES_USED));

                    if (uses >= 500) {
                        grantAdvancement(s_player, AcesBS.MOD_ID + "/memes/500_cigarettes");
                    }
                }
            }
        }

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

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


    @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);

        // 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
                );
            }
        }

        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.addParticleClient(
                        ParticleTypes.CAMPFIRE_COSY_SMOKE,
                        x, y, z,
                        vx, vy, vz
                );
            }
        }
    }

    @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(stack, 2400);
            stack.decrement(1);

            if (!world.isClient()) {
                player.incrementStat(ModStats.CIGARETTES_USED);
            }
        }
        return stack;
    }
}
