package com.acethewildfire.acesbs.mixin;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.util.CreeperTransformAccess;
import net.minecraft.entity.mob.CreeperEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(CreeperEntity.class)
public abstract class CreeperInteractMixin {

    @Inject(method = "interactMob", at = @At("HEAD"), cancellable = true)
    private void onRightClick(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
        ItemStack itemStack = player.getStackInHand(hand);

        // === Here is the item check ===
        if (itemStack.isOf(ModItems.STABLE_ENTROPY)) {
            CreeperEntity creeper = (CreeperEntity) (Object) this;
            World world = creeper.getWorld();

            if (!world.isClient) {

                CreeperTransformAccess access = (CreeperTransformAccess) creeper;

                access.aces_bravo_sierra_template_1_21$setTransforming(true);
                access.aces_bravo_sierra_template_1_21$setTransformTimer(0);

                creeper.setAiDisabled(true);
                creeper.setInvulnerable(true);

                world.playSound(null, creeper.getBlockPos(), SoundEvents.ENTITY_EVOKER_CAST_SPELL,
                        creeper.getSoundCategory(), 1.0f, 1.0f);


                // Consume item unless in creative mode
                if (!player.isCreative()) {
                    itemStack.decrement(1);
                }
            }

            // Stop vanilla behavior and return success
            cir.setReturnValue(ActionResult.SUCCESS);
        }
    }
}
