package ovh.glitchlabs.ornamentum.blocks.custom;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class FlagBlock extends HorizontalDirectionalBlock {

    public FlagBlock(Properties properties) {
        super(properties);

        this.registerDefaultState(
                this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(BlockStateProperties.ATTACH_FACE, AttachFace.FLOOR));
    }


    @Override
    protected void createBlockStateDefinition(
            StateDefinition.Builder<Block, BlockState> builder
    ) {
        builder.add(
                FACING,
                BlockStateProperties.ATTACH_FACE
        );
    }

    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {

        Direction clickedFace = context.getClickedFace();

        AttachFace face;

        if (clickedFace == Direction.UP) {
            face = AttachFace.FLOOR;
        }
        else if (clickedFace == Direction.DOWN) {
            face = AttachFace.CEILING;
        }
        else {
            face = AttachFace.WALL;
        }
        return this.defaultBlockState()
                .setValue(BlockStateProperties.ATTACH_FACE, face)
                .setValue(FACING, context.getHorizontalDirection().getOpposite());
    }

    @Override
    public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
        AttachFace face =
                state.getValue(BlockStateProperties.ATTACH_FACE);

        Direction facing =
                state.getValue(FACING);


        return switch (face) {

            case FLOOR -> switch (facing) {
                case NORTH, SOUTH -> Block.box(7, 0, 7, 9, 16, 9);
                case EAST, WEST -> Block.box(7, 0, 7, 9, 16, 9);
                default -> Block.box(7, 0, 7, 9, 16, 9);
            };

            case WALL -> switch (facing) {
                case NORTH -> Block.box(7, 5, 9, 9, 16, 16);
                case SOUTH -> Block.box(7, 5, 0, 9, 16, 7);
                case WEST -> Block.box(9, 5, 7, 16, 16, 9);
                case EAST -> Block.box(0, 5, 7, 7, 16, 9);
                default -> Block.box(7, 5, 9, 9, 16, 16);
            };

            case CEILING -> switch (facing) {
                case NORTH, SOUTH -> Block.box(0, 10, 7, 16, 16, 9);
                case EAST, WEST -> Block.box(7, 10, 0, 9, 16, 16);
                default -> Block.box(7, 10, 7, 9, 16, 9);
            };
        };
    }

    @Override
    public BlockState rotate(BlockState state, Rotation rotation) {
        return state.setValue(FACING, rotation.rotate(state.getValue(FACING))
        );
    }



    @Override
    protected com.mojang.serialization.MapCodec<? extends HorizontalDirectionalBlock> codec() {
        return simpleCodec(FlagBlock::new);
    }
}