package com.dairymoose.modernlife.items;

import com.dairymoose.modernlife.blocks.RoadMarkerBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/items/RoadMarkerItem.class */
public class RoadMarkerItem extends Item {
    private static final Logger LOGGER = LogManager.getLogger();
    private static final Block[] roadMarkers = {(Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_YELLOW.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_DOUBLE_YELLOW.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_SHOULDER_LEFT.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_SHOULDER_RIGHT.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_CORNER_LEFT.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_CORNER_RIGHT.get()};
    private static final Block[] roadMarkersIncline = {(Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_YELLOW_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_DOUBLE_YELLOW_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_SHOULDER_LEFT_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_SHOULDER_RIGHT_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_CORNER_LEFT_INCLINE.get(), (Block) CustomBlocks.BLOCK_ROAD_MARKER_WHITE_CORNER_RIGHT_INCLINE.get()};
    public static Map<Player, Integer> selectedMarker = new HashMap();
    private static final int DEFAULT_MARKER = 0;

    public RoadMarkerItem(Properties p_i48530_4_) {
        super(p_i48530_4_);
    }

    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack p_77624_1_, @Nullable Level p_77624_2_, List<Component> text, TooltipFlag p_77624_4_) {
        text.add(new TextComponent("* Right click paved road to apply currently selected road marker"));
        text.add(new TextComponent("* Right click elsewhere to toggle between available road markers"));
    }

    public InteractionResult useOn(UseOnContext context) {
        Level world = context.getLevel();
        if (!world.isClientSide) {
            BlockState bs = world.getBlockState(context.getClickedPos());
            if (bs.is(CustomBlocks.BLOCK_PAVED_ROAD.get()) || bs.is(CustomBlocks.BLOCK_PAVED_ROAD_RAMP.get())) {
                boolean didAction = false;
                BlockPos above = context.getClickedPos().above();
                BlockState aboveBs = world.getBlockState(above);
                if (world.getBlockState(above).isAir()) {
                    Integer marker = selectedMarker.get(context.getPlayer());
                    if (marker == null) {
                        marker = 0;
                    }
                    if (bs.is(CustomBlocks.BLOCK_PAVED_ROAD.get())) {
                        world.setBlockAndUpdate(above, (BlockState) roadMarkers[marker.intValue()].defaultBlockState().setValue(BlockStateProperties.HORIZONTAL_FACING, context.getHorizontalDirection()));
                    } else {
                        world.setBlockAndUpdate(above, (BlockState) roadMarkersIncline[marker.intValue()].defaultBlockState().setValue(BlockStateProperties.HORIZONTAL_FACING, context.getHorizontalDirection()));
                    }
                    world.playSound((Player) null, above, SoundEvents.SCAFFOLDING_STEP, SoundSource.BLOCKS, 1.0f, 1.6f);
                    didAction = true;
                    ItemStack itemStack = context.getItemInHand();
                    itemStack.hurtAndBreak(1, context.getPlayer(), x -> {
                    });
                } else if (aboveBs.getBlock() instanceof RoadMarkerBlock) {
                    world.setBlockAndUpdate(above, Blocks.AIR.defaultBlockState());
                    world.playSound((Player) null, above, SoundEvents.SCAFFOLDING_STEP, SoundSource.BLOCKS, 1.0f, 1.6f);
                    didAction = true;
                }
                if (didAction) {
                }
            } else {
                toggleSelectedMarker(context.getPlayer());
            }
        }
        return InteractionResult.CONSUME;
    }

    void toggleSelectedMarker(Player player) {
        Integer marker = selectedMarker.get(player);
        if (marker == null) {
            marker = 0;
        }
        Integer marker2 = Integer.valueOf(Integer.valueOf(marker.intValue() + 1).intValue() % roadMarkers.length);
        selectedMarker.put(player, marker2);
        player.sendMessage(new TextComponent("Using marker: ").m_7220_(new TranslatableComponent(roadMarkers[marker2.intValue()].getDescriptionId())), player.getUUID());
    }

    public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand p_77659_3_) {
        ItemStack itemStack = player.getItemInHand(p_77659_3_);
        if (!world.isClientSide) {
            toggleSelectedMarker(player);
        }
        return InteractionResultHolder.consume(itemStack);
    }
}
