package su.a71.new_soviet.blocks;

import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.ShapeContext;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldView;
import su.a71.new_soviet.util.Shapes;

public class ChessBlockKnight extends HorizontalFacingBlock {

    public ChessBlockKnight(Settings settings) {
        super(settings.notSolid().nonOpaque().noBlockBreakParticles().pistonBehavior(PistonBehavior.DESTROY).strength(0.1f, 2f));
        setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
    }

    @Override
    protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
        builder.add(Properties.HORIZONTAL_FACING);
    }

    @Override
    public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
        Direction dir = state.get(FACING);
        return switch (dir) {
            case NORTH -> VoxelShapes.union(Block.createCuboidShape(4,0,4,12,1,12), Block.createCuboidShape(5,1,5,11,4,11), Block.createCuboidShape(6,0,6,10,13,10), Block.createCuboidShape(5.5,9,1.5,10.5,13,9.5), Block.createCuboidShape(7.5,8,6,8.5,17,11));
            case SOUTH -> VoxelShapes.union(Block.createCuboidShape(4,0,4,12,1,12), Block.createCuboidShape(5,1,5,11,4,11), Block.createCuboidShape(6,0,6,10,13,10), Block.createCuboidShape(5.5,9,6.5,10.5,13,14.5), Block.createCuboidShape(7.5,8,5,8.5,17,10));
            case EAST -> VoxelShapes.union(Block.createCuboidShape(4,0,4,12,1,12), Block.createCuboidShape(5,1,5,11,4,11), Block.createCuboidShape(6,0,6,10,13,10), Block.createCuboidShape(6.5,9,5.5,14.5,13,10.5), Block.createCuboidShape(5,8,7.5,10,17,8.5));
            case WEST -> VoxelShapes.union(Block.createCuboidShape(4,0,4,12,1,12), Block.createCuboidShape(5,1,5,11,4,11), Block.createCuboidShape(6,0,6,10,13,10), Block.createCuboidShape(1.5,9,5.5,9.5,13,10.5), Block.createCuboidShape(6,8,7.5,11,17,8.5));
            default -> VoxelShapes.fullCube();
        };
    }

    @Override
    public BlockState getPlacementState(ItemPlacementContext ctx) {
        return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite());
    }

    protected boolean canPlantOnTop(BlockState floor, BlockView world, BlockPos pos) {
        return !floor.getCollisionShape(world, pos).getFace(Direction.UP).isEmpty() || floor.isSideSolidFullSquare(world, pos, Direction.UP);
    }

    public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
        BlockPos blockPos = pos.down();
        return this.canPlantOnTop(world.getBlockState(blockPos), world, blockPos);
    }
}