package com.r3944realms.leashedplayer.client.renderer.item.properties.conditional;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.r3944realms.leashedplayer.content.items.type.ILeashRopeArrow;
import com.r3944realms.leashedplayer.extend.CrossbowItem$ChargeTypeExtend;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.item.properties.select.SelectItemModelProperty;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.ChargedProjectiles;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@OnlyIn(Dist.CLIENT)
public record ChargeExtend() implements SelectItemModelProperty<CrossbowItem$ChargeTypeExtend> {
    public static  Codec<CrossbowItem$ChargeTypeExtend> CODEC = CrossbowItem$ChargeTypeExtend.CODEC;
    public static Type<ChargeExtend, CrossbowItem$ChargeTypeExtend> TYPE =  Type.create(
            MapCodec.unit(new ChargeExtend()), CODEC
    );
    @Override
    public @NotNull CrossbowItem$ChargeTypeExtend get(@NotNull ItemStack stack, @Nullable ClientLevel level, @Nullable LivingEntity entity, int seed, @NotNull ItemDisplayContext displayContext) {
        ChargedProjectiles chargedprojectiles = stack.get(DataComponents.CHARGED_PROJECTILES);
        if(chargedprojectiles == null || chargedprojectiles.isEmpty()) {
            return CrossbowItem$ChargeTypeExtend.NONE;
        } else {
            return ILeashRopeArrow.isLeashRopeArrow(stack, entity) ? CrossbowItem$ChargeTypeExtend.LEASH_ROPE_ARROW :
                    chargedprojectiles.contains(Items.FIREWORK_ROCKET) ? CrossbowItem$ChargeTypeExtend.ROCKET : CrossbowItem$ChargeTypeExtend.ARROW;
        }
    }

    @Override
    public @NotNull Codec<CrossbowItem$ChargeTypeExtend> valueCodec() {
        return CODEC;
    }

    @Override
    public @NotNull Type<? extends SelectItemModelProperty<CrossbowItem$ChargeTypeExtend>, CrossbowItem$ChargeTypeExtend> type() {
        return TYPE;
    }
}
