package top.r3944realms.lib39.mixin.carryon;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.spongepowered.asm.mixin.Debug;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import top.r3944realms.lib39.content.item.DollItem;
import top.r3944realms.lib39.util.GameProfileHelper;
import tschipp.carryon.client.render.CarriedObjectRender;
import tschipp.carryon.common.carry.CarryOnDataManager;

/**
 * The type Mixin carried object render.
 */
@Pseudo
@Mixin(value = CarriedObjectRender.class, remap = false)
public class MixinCarriedObjectRender {
    @ModifyVariable(
            method = "drawFirstPersonBlock",
            at = @At(
                    value = "LOAD",
                    target = "Ltschipp/carryon/client/render/CarryRenderHelper;renderBakedModel(Lnet/minecraft/world/item/ItemStack;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;ILnet/minecraft/client/resources/model/BakedModel;)V"
            )
    )
    private static ItemStack warpDollItem$1(ItemStack stack, @Local(ordinal = 0, argsOnly = true) Player player) {
        if (stack.getItem() instanceof DollItem) {
            CompoundTag compound = CarryOnDataManager.getCarryData(player).getNbt().getCompound("tile").getCompound(GameProfileHelper.TAG_OWN_PROFILE);
            stack.getOrCreateTag().put(GameProfileHelper.TAG_OWN_PROFILE, compound);
        }
        return stack;
    }
    @ModifyVariable(
            method = "drawThirdPerson",
            at = @At(
                    value = "LOAD",
                    target = "Ltschipp/carryon/client/render/CarryRenderHelper;renderBakedModel(Lnet/minecraft/world/item/ItemStack;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;ILnet/minecraft/client/resources/model/BakedModel;)V"
            )
    )
    private static ItemStack warpDollItem$2(ItemStack stack, @Local(ordinal = 0) Player player) {
        if (stack.getItem() instanceof DollItem) {
            CompoundTag compound = CarryOnDataManager.getCarryData(player).getNbt().getCompound("tile").getCompound(GameProfileHelper.TAG_OWN_PROFILE);
            stack.getOrCreateTag().put(GameProfileHelper.TAG_OWN_PROFILE, compound);
        }
        return stack;
    }

}
