/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.item.custom;

import com.remains_cave.item.client.DoomHoodItemRenderer;
import com.remains_cave.item.client.DoomHoodRenderer;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.core.Holder;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ArmorMaterial;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.animatable.GeoItem;
import software.bernie.geckolib.animatable.client.GeoRenderProvider;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.*;
import software.bernie.geckolib.constant.DataTickets;
import software.bernie.geckolib.renderer.GeoItemRenderer;
import software.bernie.geckolib.util.GeckoLibUtil;

import java.util.function.Consumer;

public class DoomHoodItem extends ArmorItem implements GeoItem {
    private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);

    public DoomHoodItem(Holder<ArmorMaterial> material, Type type, Properties properties) {
        super(material, type, properties);
    }

    @Override
    public void createGeoRenderer(Consumer<GeoRenderProvider> consumer) {
        consumer.accept(new GeoRenderProvider() {
            private DoomHoodItemRenderer itemRenderer;
            private DoomHoodRenderer armorRenderer;

            @Override
            public GeoItemRenderer<?> getGeoItemRenderer() {
                if (itemRenderer == null) {
                    itemRenderer = new DoomHoodItemRenderer();
                }
                return itemRenderer;
            }

            @Override
            public <T extends LivingEntity> DoomHoodRenderer getGeoArmorRenderer(
                    T livingEntity, ItemStack itemStack, EquipmentSlot equipmentSlot,
                    HumanoidModel<T> original) {
                if (armorRenderer == null) {
                    armorRenderer = new DoomHoodRenderer();
                }
                return armorRenderer;
            }
        });
    }

    @Override
    public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
        controllers.add(new AnimationController<>(this, "cape_controller", 10, state -> {
            // 获取穿戴该盔甲的实体（返回 Entity）
            Entity entity = state.getData(DataTickets.ENTITY);
            if (!(entity instanceof LivingEntity livingEntity)) return PlayState.STOP;

            // 计算实体是否在移动（客户端）
            double dx = livingEntity.getX() - livingEntity.xo;
            double dz = livingEntity.getZ() - livingEntity.zo;
            boolean isMoving = dx * dx + dz * dz > 0.01;

            if (isMoving) {
                state.getController().setAnimation(
                        RawAnimation.begin().thenLoop("animation.doom_hood.cape_swing")
                );
            } else {
                state.getController().setAnimation(
                        RawAnimation.begin().thenLoop("animation.doom_hood.cape_idle")
                );
            }
            return PlayState.CONTINUE;
        }));
    }
    @Override
    public @NotNull Holder<SoundEvent> getEquipSound() {
        return SoundEvents.ARMOR_EQUIP_LEATHER;
    }
    @Override
    public AnimatableInstanceCache getAnimatableInstanceCache() {
        return cache;
    }
}