package com.straice.smoothdoors.mixin.client;

import com.straice.smoothdoors.client.anim.SddAnimator;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.block.BlockRenderManager;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockRenderView;
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.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;

@Mixin(BlockRenderManager.class)
public class BlockRenderManagerMixin {

    @Inject(
            method = "renderBlock(Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/world/BlockRenderView;Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;ZLjava/util/List;)V",
            at = @At("HEAD"),
            cancellable = true,
            require = 0
    )
    private void sdd$renderBlock(BlockState state, BlockPos pos, BlockRenderView world,
                                 MatrixStack matrices, VertexConsumer vertexConsumer,
                                 boolean cull, List<Object> parts,
                                 CallbackInfo ci) {
        if (SddAnimator.shouldHideInChunk(pos, state)) {
            ci.cancel();
        }
    }

    @Inject(
            method = "renderBlock(Lnet/minecraft/block/BlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/world/BlockRenderView;Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;Z)Z",
            at = @At("HEAD"),
            cancellable = true,
            require = 0
    )
    private void sdd$renderBlock(BlockState state, BlockPos pos, BlockRenderView world,
                                 MatrixStack matrices, VertexConsumer vertexConsumer,
                                 boolean cull, CallbackInfoReturnable<Boolean> cir) {
        if (SddAnimator.shouldHideInChunk(pos, state)) {
            cir.setReturnValue(false);
            cir.cancel();
        }
    }
}
