package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.blocks.gui.EaselScreen;
import com.dairymoose.modernlife.blocks.gui.EaselSizeScreen;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeClient;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.core.ModernLifeConfig;
import com.dairymoose.modernlife.tileentities.EaselBlockEntity;
import com.dairymoose.modernlife.util.ModernLifeUtil;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/EaselBlock.class */
public class EaselBlock extends Block implements EntityBlock {
    protected static final VoxelShape SHAPE_NORTH = Block.box(3.5d, 0.0d, 3.2d, 14.0d, 23.0d, 12.8d);
    protected static final VoxelShape SHAPE_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH);
    protected static final VoxelShape SHAPE_SOUTH = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_EAST);
    protected static final VoxelShape SHAPE_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH);
    public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
    public static final BooleanProperty EMPTY = BooleanProperty.create("empty");

    public EaselBlock() {
        super(Properties.of(Material.WOOD).sound(SoundType.WOOD).strength(2.5f, 0.0f));
        registerDefaultState((BlockState) ((BlockState) this.stateDefinition.any().setValue(FACING, Direction.NORTH)).setValue(EMPTY, Boolean.TRUE));
    }

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

    public BlockState mirror(BlockState state, Mirror mirror) {
        return state.rotate(mirror.getRotation(state.getValue(FACING)));
    }

    public int getFlammability(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
        if (this.material.isFlammable()) {
            return 20;
        }
        return 0;
    }

    public int getFireSpreadSpeed(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
        if (this.material.isFlammable()) {
            return 5;
        }
        return 0;
    }

    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack itemStack, @Nullable BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag) {
        list.add(new TextComponent("Place a canvas on it to begin painting"));
    }

    public boolean canSurvive(BlockState p_196260_1_, LevelReader p_196260_2_, BlockPos p_196260_3_) {
        return (p_196260_2_.isEmptyBlock(p_196260_3_.below()) || p_196260_2_.getBlockState(p_196260_3_.below()).is(CustomBlocks.BLOCK_EASEL.get())) ? false : true;
    }

    public void onRemove(BlockState p_196243_1_, Level p_196243_2_, BlockPos p_196243_3_, BlockState p_196243_4_, boolean p_196243_5_) {
        if (!p_196243_1_.is(p_196243_4_.getBlock())) {
            Container blockEntity = (Container) p_196243_2_.getBlockEntity(p_196243_3_);
            if (blockEntity != null) {
                Containers.dropContents(p_196243_2_, p_196243_3_, blockEntity);
                p_196243_2_.updateNeighbourForOutputSignal(p_196243_3_, this);
            }
            super.onRemove(p_196243_1_, p_196243_2_, p_196243_3_, p_196243_4_, p_196243_5_);
        }
    }

    public InteractionResult use(BlockState p_225533_1_, Level p_225533_2_, BlockPos p_225533_3_, Player p_225533_4_, InteractionHand p_225533_5_, BlockHitResult p_225533_6_) {
        if (p_225533_4_.getMainHandItem().getItem() == CustomBlocks.ITEM_CANVAS.get()) {
            if (((Boolean) p_225533_1_.getValue(EMPTY)).booleanValue()) {
                if (!p_225533_2_.isClientSide) {
                    BlockEntity tileEntity = p_225533_2_.getBlockEntity(p_225533_3_);
                    if (tileEntity instanceof EaselBlockEntity) {
                        EaselBlockEntity easelBlockEntity = (EaselBlockEntity) tileEntity;
                        if ((p_225533_2_ instanceof ServerLevel) && easelBlockEntity != null) {
                            easelBlockEntity.setItem(0, p_225533_4_.getMainHandItem().copy());
                            p_225533_4_.getMainHandItem().setCount(0);
                            BlockState newState = (BlockState) p_225533_1_.setValue(EMPTY, false);
                            p_225533_2_.setBlockAndUpdate(p_225533_3_, newState);
                            ((ServerLevel) p_225533_2_).sendBlockUpdated(p_225533_3_, p_225533_1_, newState, 2);
                        }
                    }
                }
                return InteractionResult.CONSUME;
            }
        } else if (p_225533_4_.getMainHandItem().getItem() == Items.AIR && p_225533_4_.isSteppingCarefully()) {
            if (!((Boolean) p_225533_1_.getValue(EMPTY)).booleanValue()) {
                if (!p_225533_2_.isClientSide) {
                    BlockEntity tileEntity2 = p_225533_2_.getChunkAt(p_225533_3_).getBlockEntity(p_225533_3_);
                    ModernLifeCommon.LOGGER.debug("tileEntity = " + tileEntity2);
                    if (tileEntity2 instanceof EaselBlockEntity) {
                        EaselBlockEntity easelBlockEntity2 = (EaselBlockEntity) tileEntity2;
                        if ((p_225533_2_ instanceof ServerLevel) && easelBlockEntity2 != null) {
                            ItemStack canvasItem = easelBlockEntity2.getItem(0).copy();
                            easelBlockEntity2.setItem(0, ItemStack.EMPTY);
                            p_225533_2_.setBlockAndUpdate(p_225533_3_, (BlockState) p_225533_1_.setValue(EMPTY, true));
                            if (canvasItem != null) {
                                p_225533_4_.setItemInHand(InteractionHand.MAIN_HAND, canvasItem);
                            }
                        }
                    }
                }
                return InteractionResult.CONSUME;
            }
        } else if (!((Boolean) p_225533_1_.getValue(EMPTY)).booleanValue()) {
            if (p_225533_2_.isClientSide) {
                DistExecutor.runWhenOn(Dist.CLIENT, () -> {
                    return new Runnable() { // from class: com.dairymoose.modernlife.blocks.EaselBlock.1

                        @Override // java.lang.Runnable
                        public void run() {
                            EaselScreen screen = new EaselScreen(p_225533_2_, p_225533_3_);
                            BlockEntity tileEntity3 = p_225533_2_.getBlockEntity(p_225533_3_);
                            ModernLifeCommon.LOGGER.debug("tileEntity = " + tileEntity3);
                            if (tileEntity3 instanceof EaselBlockEntity) {
                                EaselBlockEntity easelBlockEntity3 = (EaselBlockEntity) tileEntity3;
                                ItemStack item = easelBlockEntity3.getItem(0);
                                ModernLifeCommon.LOGGER.debug("item = " + item);
                                if (item.getItem() == CustomBlocks.ITEM_CANVAS.get()) {
                                    ModernLifeCommon.LOGGER.debug("canvas = " + tileEntity3);
                                    if (item.hasTag() && item.getTag().contains("UniqueId")) {
                                        long uniqueId = item.getTag().getLong("UniqueId");
                                        byte[] imageData = ModernLifeClient.getCanvasData(uniqueId);
                                        if (imageData != null && imageData.length > 0) {
                                            int texW = ModernLifeClient.getCanvasWidth(uniqueId);
                                            int texH = ModernLifeClient.getCanvasHeight(uniqueId);
                                            screen.setTextureSize(texW, texH);
                                            screen.setInitialData(imageData);
                                            ModernLifeCommon.LOGGER.debug("initialData = " + imageData.length);
                                        }
                                    } else if (((Boolean) ModernLifeConfig.CLIENT.showEaselResolutionScreen.get()).booleanValue()) {
                                        EaselSizeScreen sizeScreen = new EaselSizeScreen(p_225533_2_, p_225533_3_);
                                        Minecraft.getInstance().setScreen(sizeScreen);
                                        return;
                                    } else {
                                        ModernLifeCommon.LOGGER.debug("empty canvas size = " + ModernLifeConfig.CLIENT.emptyCanvasEaselSize.get());
                                        screen.setTextureSize(((Integer) ModernLifeConfig.CLIENT.emptyCanvasEaselSize.get()).intValue(), ((Integer) ModernLifeConfig.CLIENT.emptyCanvasEaselSize.get()).intValue());
                                    }
                                }
                            }
                            screen.setBlockPos(p_225533_3_);
                            Minecraft.getInstance().setScreen(screen);
                        }
                    };
                });
            }
            return InteractionResult.CONSUME;
        }
        return InteractionResult.PASS;
    }

    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        EaselBlockEntity toReturn = (EaselBlockEntity) EaselBlockEntity.EASEL.create(pos, state);
        return toReturn;
    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(new Property[]{FACING, EMPTY});
    }

    /* renamed from: com.dairymoose.modernlife.blocks.EaselBlock$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/EaselBlock$2.class */
    static /* synthetic */ class C00202 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];

        static {
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public VoxelShape getShape(BlockState bs, BlockGetter reader, BlockPos pos, CollisionContext sel) {
        switch (C00202.$SwitchMap$net$minecraft$core$Direction[bs.getValue(FACING).ordinal()]) {
            case 1:
                return SHAPE_NORTH;
            case 2:
                return SHAPE_SOUTH;
            case 3:
                return SHAPE_EAST;
            case 4:
                return SHAPE_WEST;
            default:
                return SHAPE_NORTH;
        }
    }

    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return (BlockState) defaultBlockState().setValue(FACING, context.getHorizontalDirection());
    }
}
