package com.dairymoose.modernlife.renderer.tileentity;

import com.dairymoose.modernlife.blocks.gui.chess.BishopPiece;
import com.dairymoose.modernlife.blocks.gui.chess.KingPiece;
import com.dairymoose.modernlife.blocks.gui.chess.KnightPiece;
import com.dairymoose.modernlife.blocks.gui.chess.PawnPiece;
import com.dairymoose.modernlife.blocks.gui.chess.Piece;
import com.dairymoose.modernlife.blocks.gui.chess.QueenPiece;
import com.dairymoose.modernlife.blocks.gui.chess.RookPiece;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.network.play.client.ServerboundChessStatePacket;
import com.dairymoose.modernlife.tileentities.ChessBoardBlockEntity;
import com.mojang.blaze3d.vertex.PoseStack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@OnlyIn(Dist.CLIENT)
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer.class */
public class ChessBoardBlockEntityRenderer implements BlockEntityRenderer<ChessBoardBlockEntity> {
    private static final Logger LOGGER = LogManager.getLogger();
    private static final ItemStack IS_ROOK = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.1
        C01431() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_ROOK.get();
        }
    });
    private static final ItemStack IS_BISHOP = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.2
        C01472() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_BISHOP.get();
        }
    });
    private static final ItemStack IS_PAWN = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.3
        C01483() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_PAWN.get();
        }
    });
    private static final ItemStack IS_KNIGHT = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.4
        C01494() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KNIGHT.get();
        }
    });
    private static final ItemStack IS_KING = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.5
        C01505() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KING.get();
        }
    });
    private static final ItemStack IS_QUEEN = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.6
        C01516() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_QUEEN.get();
        }
    });
    private static final ItemStack IS_ROOK_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.7
        C01527() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_ROOK_W.get();
        }
    });
    private static final ItemStack IS_BISHOP_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.8
        C01538() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_BISHOP_W.get();
        }
    });
    private static final ItemStack IS_PAWN_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.9
        C01549() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_PAWN_W.get();
        }
    });
    private static final ItemStack IS_KNIGHT_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.10
        C014410() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KNIGHT_W.get();
        }
    });
    private static final ItemStack IS_KING_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.11
        C014511() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KING_W.get();
        }
    });
    private static final ItemStack IS_QUEEN_W = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer.12
        C014612() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_QUEEN_W.get();
        }
    });
    public static Map<BlockPos, GameState> stateMap = new HashMap();
    private static final float WHITE_OVERLAY_PACK_VALUE = 0.5f;

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$1.class */
    class C01431 implements ItemLike {
        C01431() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_ROOK.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$2.class */
    class C01472 implements ItemLike {
        C01472() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_BISHOP.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$3 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$3.class */
    class C01483 implements ItemLike {
        C01483() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_PAWN.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$4 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$4.class */
    class C01494 implements ItemLike {
        C01494() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KNIGHT.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$5 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$5.class */
    class C01505 implements ItemLike {
        C01505() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KING.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$6 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$6.class */
    class C01516 implements ItemLike {
        C01516() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_QUEEN.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$7 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$7.class */
    class C01527 implements ItemLike {
        C01527() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_ROOK_W.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$8 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$8.class */
    class C01538 implements ItemLike {
        C01538() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_BISHOP_W.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$9 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$9.class */
    class C01549 implements ItemLike {
        C01549() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_PAWN_W.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$10 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$10.class */
    class C014410 implements ItemLike {
        C014410() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KNIGHT_W.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$11 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$11.class */
    class C014511 implements ItemLike {
        C014511() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_KING_W.get();
        }
    }

    /* renamed from: com.dairymoose.modernlife.renderer.tileentity.ChessBoardBlockEntityRenderer$12 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$12.class */
    class C014612 implements ItemLike {
        C014612() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CHESS_QUEEN_W.get();
        }
    }

    public ChessBoardBlockEntityRenderer(BlockEntityRendererProvider.Context ctx) {
    }

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/renderer/tileentity/ChessBoardBlockEntityRenderer$GameState.class */
    public class GameState {
        private List<Piece> whitePieces;
        private List<Piece> blackPieces;
        private int lastHash = 0;
        private boolean isWhiteTurn = true;

        public GameState() {
        }
    }

    public void renderPiece(ItemStack toRender, int x, int y, PoseStack matrixStack, int lightmap, int overlay, MultiBufferSource iRenderTypeBuffer) {
        matrixStack.pushPose();
        matrixStack.translate(0.225d + (x * 0.0782d), 0.12d, 0.225d + (y * 0.0782d));
        matrixStack.scale(0.12f, 0.12f, 0.12f);
        Minecraft.getInstance().getItemRenderer().renderStatic(toRender, ItemTransforms.TransformType.FIXED, lightmap, overlay, matrixStack, iRenderTypeBuffer, 0);
        matrixStack.popPose();
    }

    private void defaultPiecesOneSide(int startY, int yIncrement, PoseStack matrixStack, int lightmap, int overlay, MultiBufferSource iRenderTypeBuffer, boolean isWhite) {
        renderPiece(isWhite ? IS_ROOK_W : IS_ROOK, 0, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_KNIGHT_W : IS_KNIGHT, 1, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_BISHOP_W : IS_BISHOP, 2, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_KING_W : IS_KING, 3, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_QUEEN_W : IS_QUEEN, 4, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_BISHOP_W : IS_BISHOP, 5, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_KNIGHT_W : IS_KNIGHT, 6, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        renderPiece(isWhite ? IS_ROOK_W : IS_ROOK, 7, startY, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        for (int i = 0; i < 8; i++) {
            renderPiece(isWhite ? IS_PAWN_W : IS_PAWN, i, startY + yIncrement, matrixStack, lightmap, overlay, iRenderTypeBuffer);
        }
    }

    public void defaultBoardState(PoseStack matrixStack, int lightmap, int overlay, MultiBufferSource iRenderTypeBuffer) {
        defaultPiecesOneSide(0, 1, matrixStack, lightmap, overlay, iRenderTypeBuffer, false);
        defaultPiecesOneSide(7, -1, matrixStack, lightmap, overlay, iRenderTypeBuffer, true);
    }

    public ItemStack getItemStackForPiece(Piece p, boolean isWhite) {
        ItemStack pieceType = null;
        if (p instanceof RookPiece) {
            pieceType = isWhite ? IS_ROOK_W : IS_ROOK;
        } else if (p instanceof BishopPiece) {
            pieceType = isWhite ? IS_BISHOP_W : IS_BISHOP;
        } else if (p instanceof PawnPiece) {
            pieceType = isWhite ? IS_PAWN_W : IS_PAWN;
        } else if (p instanceof KnightPiece) {
            pieceType = isWhite ? IS_KNIGHT_W : IS_KNIGHT;
        } else if (p instanceof KingPiece) {
            pieceType = isWhite ? IS_KING_W : IS_KING;
        } else if (p instanceof QueenPiece) {
            pieceType = isWhite ? IS_QUEEN_W : IS_QUEEN;
        }
        return pieceType;
    }

    public void nbtBoardState(PoseStack matrixStack, int lightmap, int overlay, MultiBufferSource iRenderTypeBuffer, GameState gameState) {
        boolean mirrorBoard = gameState.isWhiteTurn;
        for (Piece p : gameState.blackPieces) {
            ItemStack pieceType = getItemStackForPiece(p, false);
            if (pieceType != null) {
                int x = p.f9x;
                int y = p.f10y;
                if (mirrorBoard) {
                    x = 7 - x;
                    y = 7 - y;
                }
                renderPiece(pieceType, x, y, matrixStack, lightmap, overlay, iRenderTypeBuffer);
            }
        }
        for (Piece p2 : gameState.whitePieces) {
            ItemStack pieceType2 = getItemStackForPiece(p2, true);
            if (pieceType2 != null) {
                int x2 = p2.f9x;
                int y2 = p2.f10y;
                if (mirrorBoard) {
                    x2 = 7 - x2;
                    y2 = 7 - y2;
                }
                renderPiece(pieceType2, x2, y2, matrixStack, lightmap, overlay, iRenderTypeBuffer);
            }
        }
    }

    public void render(ChessBoardBlockEntity tileEntity, float v, PoseStack matrixStack, MultiBufferSource iRenderTypeBuffer, int lightmap, int overlay) {
        if (!tileEntity.isEmpty()) {
            ItemStack itemStack = tileEntity.getItem(0);
            if (itemStack.getItem() == CustomBlocks.ITEM_CHESS_BOARD.get()) {
                CompoundTag nbt = itemStack.getTag();
                if (nbt != null && nbt.contains("BoardState")) {
                    GameState gameState = stateMap.get(tileEntity.getBlockPos());
                    if (gameState == null) {
                        gameState = new GameState();
                        stateMap.put(tileEntity.getBlockPos(), gameState);
                    }
                    int newHash = nbt.hashCode();
                    if (newHash != gameState.lastHash) {
                        ModernLifeCommon.LOGGER.debug("check nbt: " + newHash + "/" + gameState.lastHash);
                        ModernLifeCommon.LOGGER.debug("nbt=" + nbt);
                        gameState.lastHash = newHash;
                        ServerboundChessStatePacket packet = ServerboundChessStatePacket.fromNbt(nbt);
                        gameState.whitePieces = new ArrayList(packet.whitePieces);
                        gameState.blackPieces = new ArrayList(packet.blackPieces);
                        gameState.isWhiteTurn = packet.isWhiteTurn;
                    }
                    nbtBoardState(matrixStack, lightmap, overlay, iRenderTypeBuffer, gameState);
                    return;
                }
                defaultBoardState(matrixStack, lightmap, overlay, iRenderTypeBuffer);
            }
        }
    }
}
