/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content.item;

import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.core.animation.AnimationController;
import software.bernie.geckolib.core.object.PlayState;
import software.bernie.geckolib.util.GeckoLibUtil;
import top.r3944realms.eroticdungeongame.client.renderer.item.IronBallItemRenderer;
import top.r3944realms.eroticdungeongame.content.entity.IronBallEntity;
import top.r3944realms.eroticdungeongame.core.register.EDGItems;

import java.util.function.Consumer;

public class IronBallItem extends Item implements GeoItem {
    private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
    public IronBallItem(Properties properties) {
        super(properties);
    }

    @Override
    public @NotNull InteractionResult useOn(@NotNull UseOnContext context) {
        if(!context.getLevel().isClientSide() && context.getHand() == InteractionHand.MAIN_HAND && context.getItemInHand().is(EDGItems.IRON_BALL.get())) {
            IronBallEntity entity = IronBallEntity.createFromItem(context.getLevel(), context.getItemInHand(), context.getClickedPos().offset(context.getClickedFace().getNormal()));
            Player player = context.getPlayer();
            if(player != null)
                player.awardStat(Stats.ITEM_USED.get(EDGItems.IRON_BALL.get()));
            context.getLevel().addFreshEntity(entity);
            context.getItemInHand().shrink(1);
        }

        return InteractionResult.PASS;
    }

    @Override
    public void registerControllers(AnimatableManager.@NotNull ControllerRegistrar controllerRegistrar) {
        controllerRegistrar.add(
                new AnimationController<>(this, state -> PlayState.STOP)
        );
    }

    @Override
    public AnimatableInstanceCache getAnimatableInstanceCache() {
        return cache;
    }

    @Override
    public void initializeClient(@NotNull Consumer<IClientItemExtensions> consumer) {
        consumer.accept(
                new IClientItemExtensions() {
                    IronBallItemRenderer renderer = null;
                    @Override
                    public BlockEntityWithoutLevelRenderer getCustomRenderer() {
                        if(renderer == null) {
                            renderer = new IronBallItemRenderer();
                        }
                        return renderer;
                    }
                }
        );
    }
}
