package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import com.google.common.collect.Maps;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.item.DyeColor;
import java.util.Map;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class BannerBlock extends AbstractBannerBlock {
    public static final IntegerProperty ROTATION;
    private static final Map<DyeColor, Block> BY_COLOR;
    private static final VoxelShape SHAPE;
    
    public BannerBlock(final DyeColor bje, final Properties c) {
        super(bje, c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)BannerBlock.ROTATION, 0));
        BannerBlock.BY_COLOR.put(bje, this);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        return bqd.getBlockState(fu.below()).getMaterial().isSolid();
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return BannerBlock.SHAPE;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)BannerBlock.ROTATION, Mth.floor((180.0f + bin.getRotation()) * 16.0f / 360.0f + 0.5) & 0xF);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz == Direction.DOWN && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)BannerBlock.ROTATION, cap.rotate(cfj.<Integer>getValue((Property<Integer>)BannerBlock.ROTATION), 16));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)BannerBlock.ROTATION, bzj.mirror(cfj.<Integer>getValue((Property<Integer>)BannerBlock.ROTATION), 16));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(BannerBlock.ROTATION);
    }
    
    public static Block byColor(final DyeColor bje) {
        return BannerBlock.BY_COLOR.getOrDefault(bje, Blocks.WHITE_BANNER);
    }
    
    static {
        ROTATION = BlockStateProperties.ROTATION_16;
        BY_COLOR = Maps.newHashMap();
        SHAPE = Block.box(4.0, 0.0, 4.0, 12.0, 16.0, 12.0);
    }
}
