package com.r3944realms.modernlifepatch.mixin.block.common;

import com.dairymoose.modernlife.blocks.DeckBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.stream.Stream;

@Mixin(DeckBlock.class)
public class MixinDeck {
    @Unique
    private static final VoxelShape SHAPE_S = Stream.of(
            Block.box(0, 14, 4, 16, 16, 7),
            Block.box(3, 13, 0, 6, 15, 16),
            Block.box(10, 13, 0, 13, 15, 16),
            Block.box(0, 14, 0, 16, 16, 3),
            Block.box(0, 14, 12, 16, 16, 15),
            Block.box(0, 14, 8, 16, 16, 11)
    ).reduce((v1, v2) -> Shapes.join(v1, v2, BooleanOp.OR)).get(),
    _WITH_SUPPORT = Stream.of(SHAPE_S, Block.box(6.0, 0.0, 6.0, 10.0, 14.0, 10.0)).reduce((v1, v2) -> Shapes.join(v1, v2, BooleanOp.OR)).get();
    @Inject(method = {"getShape"}, at= @At("HEAD"), cancellable = true)
    public void getShape(BlockState bs, BlockGetter reader, BlockPos pos, CollisionContext sel, CallbackInfoReturnable<VoxelShape> cir) {
        cir.setReturnValue(!bs.getValue(DeckBlock.SUPPORT) ? SHAPE_S : _WITH_SUPPORT);
    }
}
