package com.dairymoose.modernlife.blocks.gui.chess;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeNetwork;
import com.dairymoose.modernlife.network.play.client.ServerboundChessStatePacket;
import com.dairymoose.modernlife.tileentities.ChessBoardBlockEntity;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ImageButton;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextColor;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
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/blocks/gui/chess/ChessScreen.class */
public class ChessScreen extends Screen {
    final int GUI_WIDTH = 230;
    final int GUI_HEIGHT = 230;
    final int ROW_WIDTH = 23;
    final int ROW_HEIGHT = 23;
    final int BORDERED_ROW_WIDTH = 24;
    final int BORDERED_ROW_HEIGHT = 24;
    public static final int BOARD_WIDTH = 8;
    public static final int BOARD_HEIGHT = 8;
    int currentTurn;
    private BlockPos chessBoardPos;
    private String whitePlayer;
    private String blackPlayer;
    private Button leftCastleButton;
    private Button rightCastleButton;
    private Button restartButton;
    CastleFlags castleFlags;
    private Button leftRook;
    private Button rightRook;
    private Button leftBishop;
    private Button rightBishop;
    private Button pawn1;
    private Button leftKnight;
    private Button rightKnight;
    private Button king;
    private Button queen;
    private Piece selectedPiece;
    private Piece movedPiece;
    public boolean playerIsWhite;
    private boolean canPlay;
    private boolean isWhiteTurn;
    private boolean isExpectedPlayer;
    CheckState cachedCheckState;
    public List<Piece> blackPieces;
    public List<Piece> whitePieces;
    public KingPiece whiteKing;
    public KingPiece blackKing;
    private int leftCastleButtonState;
    private int rightCastleButtonState;
    private int restartButtonState;
    private boolean restartingGame;
    private static final int UI_BUTTON_WIDTH = 14;
    private static final int UI_BUTTON_HEIGHT = 13;
    int drawSelectionX;
    int drawSelectionY;
    private static final Logger LOGGER = LogManager.getLogger();
    private static final ResourceLocation CHESS_UI = new ResourceLocation("modernlife", "textures/gui/gui_chess_blank.png");
    private static boolean playerCanPlayHimself = false;

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/ChessScreen$CastleFlags.class */
    public static class CastleFlags {
        public boolean whiteKingHasMoved = false;
        public boolean whiteLeftRookHasMoved = false;
        public boolean whiteRightRookHasMoved = false;
        public boolean blackKingHasMoved = false;
        public boolean blackLeftRookHasMoved = false;
        public boolean blackRightRookHasMoved = false;
    }

    public ChessScreen(BlockPos chessBoardPos) {
        super(new TextComponent("Chess").m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(4210752))));
        this.GUI_WIDTH = 230;
        this.GUI_HEIGHT = 230;
        this.ROW_WIDTH = 23;
        this.ROW_HEIGHT = 23;
        this.BORDERED_ROW_WIDTH = 24;
        this.BORDERED_ROW_HEIGHT = 24;
        this.currentTurn = 0;
        this.whitePlayer = null;
        this.blackPlayer = null;
        this.leftCastleButton = null;
        this.rightCastleButton = null;
        this.restartButton = null;
        this.castleFlags = new CastleFlags();
        this.playerIsWhite = true;
        this.canPlay = true;
        this.isWhiteTurn = true;
        this.isExpectedPlayer = false;
        this.blackPieces = new ArrayList(16);
        this.whitePieces = new ArrayList(16);
        this.leftCastleButtonState = 0;
        this.rightCastleButtonState = 0;
        this.restartButtonState = 0;
        this.restartingGame = false;
        this.drawSelectionX = -1;
        this.drawSelectionY = -1;
        this.chessBoardPos = chessBoardPos;
    }

    private void assignNamesAndSubmitTurn() {
        if (this.whitePlayer == null || this.whitePlayer.isEmpty()) {
            this.whitePlayer = Minecraft.getInstance().player.getName().getString();
        } else if (this.blackPlayer == null || this.blackPlayer.isEmpty()) {
            this.blackPlayer = Minecraft.getInstance().player.getName().getString();
        }
        this.canPlay = false;
        CheckState newCheckState = getCheckState();
        ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundChessStatePacket(1, newCheckState.ordinal(), this.whitePlayer, this.blackPlayer, !this.isWhiteTurn, this.chessBoardPos, this.whitePieces, this.blackPieces, this.movedPiece, this.castleFlags));
    }

    public boolean handleMouseEvent(double clickX, double clickY, boolean mouseDown) {
        if (mouseDown && this.selectedPiece != null) {
            int boardX = screenXtoBoardX((int) clickX);
            int boardY = screenYtoBoardY((int) clickY);
            if ((this.selectedPiece.f9x != boardX || this.selectedPiece.f10y != boardY) && this.selectedPiece.move(boardX, boardY)) {
                if (this.selectedPiece.black) {
                    if (this.selectedPiece.getId() == PieceIds.ROOK.ordinal()) {
                        if (this.selectedPiece.f9x == 0 && this.selectedPiece.f10y == 7) {
                            this.castleFlags.blackLeftRookHasMoved = true;
                        } else if (this.selectedPiece.f9x == 7 && this.selectedPiece.f10y == 7) {
                            this.castleFlags.blackRightRookHasMoved = true;
                        }
                    } else if (this.selectedPiece.getId() == PieceIds.KING.ordinal()) {
                        this.castleFlags.blackKingHasMoved = true;
                    }
                } else if (this.selectedPiece.getId() == PieceIds.ROOK.ordinal()) {
                    if (this.selectedPiece.f9x == 0 && this.selectedPiece.f10y == 7) {
                        this.castleFlags.whiteLeftRookHasMoved = true;
                    } else if (this.selectedPiece.f9x == 7 && this.selectedPiece.f10y == 7) {
                        this.castleFlags.whiteRightRookHasMoved = true;
                    }
                } else if (this.selectedPiece.getId() == PieceIds.KING.ordinal()) {
                    this.castleFlags.whiteKingHasMoved = true;
                }
                this.movedPiece = this.selectedPiece;
                this.selectedPiece = null;
                assignNamesAndSubmitTurn();
                return true;
            }
            return false;
        }
        return false;
    }

    public boolean m_7979_(double dragX, double dragY, int p_231045_5_, double p_231045_6_, double p_231045_8_) {
        if (p_231045_5_ == 0) {
            handleMouseEvent(dragX, dragY, true);
        }
        return super.m_7979_(dragX, dragY, p_231045_5_, p_231045_6_, p_231045_8_);
    }

    public boolean m_6375_(double clickX, double clickY, int p_231044_5_) {
        if (p_231044_5_ == 0) {
            m_7897_(true);
            if (handleMouseEvent(clickX, clickY, true)) {
                return false;
            }
        }
        return super.m_6375_(clickX, clickY, p_231044_5_);
    }

    public boolean m_6348_(double clickX, double clickY, int p_231044_5_) {
        m_7897_(false);
        if (p_231044_5_ == 0) {
            handleMouseEvent(clickX, clickY, false);
        }
        return super.m_6348_(clickX, clickY, p_231044_5_);
    }

    protected Button addSingularPiece(boolean black, int p_i51134_1_, int p_i51134_2_, int p_i51134_3_, int p_i51134_4_, int p_i51134_5_, int p_i51134_6_, int p_i51134_7_, ResourceLocation p_i51134_8_, Button.OnPress p_i51134_9_) {
        if (!black) {
            return new InvertedImageButton(p_i51134_1_, p_i51134_2_, p_i51134_3_, p_i51134_4_, p_i51134_5_, p_i51134_6_, p_i51134_7_, p_i51134_8_, p_i51134_9_);
        }
        return new ImageButton(p_i51134_1_, p_i51134_2_, p_i51134_3_, p_i51134_4_, p_i51134_5_, p_i51134_6_, p_i51134_7_, p_i51134_8_, p_i51134_9_);
    }

    protected Button addSingularPieceXDiff(boolean black, int p_i51134_1_, int p_i51134_2_, int p_i51134_3_, int p_i51134_4_, int p_i51134_5_, int p_i51134_6_, int p_i51134_7_, ResourceLocation p_i51134_8_, Button.OnPress p_i51134_9_) {
        if (!black) {
            return new InvertedImageButtonXDiff(p_i51134_1_, p_i51134_2_, p_i51134_3_, p_i51134_4_, p_i51134_5_, p_i51134_6_, p_i51134_7_, p_i51134_8_, p_i51134_9_);
        }
        return new ImageButtonXDiff(p_i51134_1_, p_i51134_2_, p_i51134_3_, p_i51134_4_, p_i51134_5_, p_i51134_6_, p_i51134_7_, p_i51134_8_, p_i51134_9_);
    }

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/ChessScreen$ButtonPressResult.class */
    public class ButtonPressResult implements Button.OnPress {
        Piece piece;
        boolean top;
        boolean black;

        ButtonPressResult(Piece piece, boolean top, boolean black) {
            this.piece = piece;
            this.top = top;
            this.black = black;
            if (this.black) {
                ChessScreen.this.blackPieces.add(piece);
            } else {
                ChessScreen.this.whitePieces.add(piece);
            }
        }

        public void onPress(Button button) {
            if (ChessScreen.this.canPlay && !this.top) {
                if (ChessScreen.this.selectedPiece == this.piece) {
                    ChessScreen.this.selectedPiece = null;
                    return;
                }
                ChessScreen.this.drawSelectionX = button.f_93620_;
                ChessScreen.this.drawSelectionY = button.f_93621_;
                ChessScreen.this.selectedPiece = this.piece;
            }
        }
    }

    protected int getBottomStartX() {
        return ((this.width - 230) / 2) + 20;
    }

    protected int getBottomStartY() {
        return ((this.height - 230) / 2) + 188;
    }

    protected int getTopStartX() {
        return ((this.width - 230) / 2) + 20;
    }

    protected int getTopStartY() {
        return ((this.height - 230) / 2) + 20;
    }

    public int screenXtoBoardX(int screenX) {
        if (screenX < getTopStartX()) {
            return -1;
        }
        return (screenX - getTopStartX()) / 24;
    }

    public int screenYtoBoardY(int screenY) {
        if (screenY < getTopStartY()) {
            return -1;
        }
        return (screenY - getTopStartY()) / 24;
    }

    public int boardXtoScreenX(int boardX) {
        return (boardX * 24) + getTopStartX();
    }

    public int boardYtoScreenY(int boardY) {
        return (boardY * 24) + getTopStartY();
    }

    public Piece getPieceAt(int x, int y) {
        for (Piece p : this.whitePieces) {
            if (p.f9x == x && p.f10y == y) {
                return p;
            }
        }
        for (Piece p2 : this.blackPieces) {
            if (p2.f9x == x && p2.f10y == y) {
                return p2;
            }
        }
        return null;
    }

    protected CheckState getCheckState() {
        if (this.blackKing.inCheck()) {
            int moveCount = blackAvailableMoveCount();
            if (moveCount == 0) {
                return CheckState.BLACK_CHECKMATE;
            }
            return CheckState.BLACK_CHECK;
        }
        if (this.whiteKing.inCheck()) {
            int moveCount2 = whiteAvailableMoveCount();
            if (moveCount2 == 0) {
                return CheckState.WHITE_CHECKMATE;
            }
            return CheckState.WHITE_CHECK;
        }
        return CheckState.NONE;
    }

    protected void initPieceXandY() {
        for (Piece p : this.whitePieces) {
            if (p.button != null) {
                p.f9x = screenXtoBoardX(p.button.f_93620_);
                p.f10y = screenYtoBoardY(p.button.f_93621_);
                p.screen = this;
                p.black = false;
                p.top = !this.playerIsWhite;
            }
        }
        for (Piece p2 : this.blackPieces) {
            if (p2.button != null) {
                p2.f9x = screenXtoBoardX(p2.button.f_93620_);
                p2.f10y = screenYtoBoardY(p2.button.f_93621_);
                p2.screen = this;
                p2.black = true;
                p2.top = this.playerIsWhite;
            }
        }
    }

    protected void initPieceInitialXY() {
        for (Piece p : this.whitePieces) {
            p.initialX = p.f9x;
            p.initialY = p.f10y;
        }
        for (Piece p2 : this.blackPieces) {
            p2.initialX = p2.f9x;
            p2.initialY = p2.f10y;
        }
    }

    protected int getTexOffsetX(Piece p) {
        if (p.getId() == PieceIds.QUEEN.ordinal()) {
            return 0;
        }
        return 233;
    }

    protected int getTexOffsetY(Piece p) {
        if (p.getId() == PieceIds.QUEEN.ordinal()) {
            return 233;
        }
        return 46 * p.getId();
    }

    protected void addPieceWithButton(Piece p, boolean top, boolean black, int boardX, int boardY) {
        if (p instanceof QueenPiece) {
            ButtonPressResult press = new ButtonPressResult(p, top, black);
            Button b = addRenderableWidget(addSingularPieceXDiff(black, boardXtoScreenX(boardX), boardYtoScreenY(boardY), 23, 23, getTexOffsetX(p), getTexOffsetY(p), 23, CHESS_UI, press));
            press.piece.button = b;
        } else {
            ButtonPressResult press2 = new ButtonPressResult(p, top, black);
            Button b2 = addRenderableWidget(addSingularPiece(black, boardXtoScreenX(boardX), boardYtoScreenY(boardY), 23, 23, getTexOffsetX(p), getTexOffsetY(p), 23, CHESS_UI, press2));
            press2.piece.button = b2;
        }
    }

    protected void addPieces(boolean black, boolean top) {
        int startX = getBottomStartX();
        int startY = getBottomStartY();
        if (top) {
            startX = getTopStartX();
            startY = getTopStartY();
        }
        int rowSwitch = -24;
        if (top) {
            rowSwitch = 24;
        }
        ButtonPressResult press = new ButtonPressResult(new RookPiece(), top, black);
        this.leftRook = addRenderableWidget(addSingularPiece(black, startX + 0, startY, 23, 23, 233, 0, 23, CHESS_UI, press));
        press.piece.button = this.leftRook;
        ButtonPressResult press2 = new ButtonPressResult(new RookPiece(), top, black);
        this.rightRook = addRenderableWidget(addSingularPiece(black, startX + 168, startY, 23, 23, 233, 0, 23, CHESS_UI, press2));
        press2.piece.button = this.rightRook;
        ButtonPressResult press3 = new ButtonPressResult(new BishopPiece(), top, black);
        this.leftBishop = addRenderableWidget(addSingularPiece(black, startX + 48, startY, 23, 23, 233, 46, 23, CHESS_UI, press3));
        press3.piece.button = this.leftBishop;
        ButtonPressResult press4 = new ButtonPressResult(new BishopPiece(), top, black);
        this.rightBishop = addRenderableWidget(addSingularPiece(black, startX + 120, startY, 23, 23, 233, 46, 23, CHESS_UI, press4));
        press4.piece.button = this.rightBishop;
        for (int p = 0; p < 8; p++) {
            ButtonPressResult press5 = new ButtonPressResult(new PawnPiece(), top, black);
            this.pawn1 = addRenderableWidget(addSingularPiece(black, startX + (24 * p), startY + rowSwitch, 23, 23, 233, 92, 23, CHESS_UI, press5));
            press5.piece.button = this.pawn1;
        }
        ButtonPressResult press6 = new ButtonPressResult(new KnightPiece(), top, black);
        this.leftKnight = addRenderableWidget(addSingularPiece(black, startX + 24, startY, 23, 23, 233, 138, 23, CHESS_UI, press6));
        press6.piece.button = this.leftKnight;
        ButtonPressResult press7 = new ButtonPressResult(new KnightPiece(), top, black);
        this.rightKnight = addRenderableWidget(addSingularPiece(black, startX + 144, startY, 23, 23, 233, 138, 23, CHESS_UI, press7));
        press7.piece.button = this.rightKnight;
        ButtonPressResult press8 = new ButtonPressResult(new KingPiece(), top, black);
        this.king = addRenderableWidget(addSingularPiece(black, startX + 96, startY, 23, 23, 233, 184, 23, CHESS_UI, press8));
        press8.piece.button = this.king;
        ButtonPressResult press9 = new ButtonPressResult(new QueenPiece(), top, black);
        this.queen = addRenderableWidget(addSingularPieceXDiff(black, startX + 72, startY, 23, 23, 0, 233, 23, CHESS_UI, press9));
        press9.piece.button = this.queen;
        initPieceXandY();
        initPieceInitialXY();
    }

    protected void mirrorBoard() {
        for (Piece p : this.whitePieces) {
            p.absMoveTo(7 - p.f9x, 7 - p.f10y);
        }
        for (Piece p2 : this.blackPieces) {
            p2.absMoveTo(7 - p2.f9x, 7 - p2.f10y);
        }
        if (this.movedPiece != null) {
            this.movedPiece.f9x = 7 - this.movedPiece.f9x;
            this.movedPiece.f10y = 7 - this.movedPiece.f10y;
        }
    }

    protected void addPieces() {
        boolean addedPieces = false;
        if (!this.restartingGame) {
            BlockEntity tileEntity = getMinecraft().level.getBlockEntity(this.chessBoardPos);
            if (tileEntity instanceof ChessBoardBlockEntity) {
                ChessBoardBlockEntity chessBoard = (ChessBoardBlockEntity) tileEntity;
                ItemStack itemStack = chessBoard.getItem(0);
                if (itemStack.getItem() == CustomBlocks.ITEM_CHESS_BOARD.get() && itemStack.getTag() != null && itemStack.getTag().contains("BoardState")) {
                    addedPieces = true;
                    ServerboundChessStatePacket packet = ServerboundChessStatePacket.fromNbt(itemStack.getTag());
                    this.castleFlags.blackKingHasMoved = packet.castleFlags.blackKingHasMoved;
                    this.castleFlags.blackLeftRookHasMoved = packet.castleFlags.blackLeftRookHasMoved;
                    this.castleFlags.blackRightRookHasMoved = packet.castleFlags.blackRightRookHasMoved;
                    this.castleFlags.whiteKingHasMoved = packet.castleFlags.whiteKingHasMoved;
                    this.castleFlags.whiteLeftRookHasMoved = packet.castleFlags.whiteLeftRookHasMoved;
                    this.castleFlags.whiteRightRookHasMoved = packet.castleFlags.whiteRightRookHasMoved;
                    this.whitePieces.clear();
                    this.blackPieces.clear();
                    this.currentTurn = packet.currentTurn;
                    this.whitePlayer = packet.whitePlayer;
                    this.blackPlayer = packet.blackPlayer;
                    this.isWhiteTurn = packet.isWhiteTurn;
                    this.movedPiece = packet.movedPiece;
                    assignWhitePlayer();
                    for (Piece p : packet.whitePieces) {
                        addPieceWithButton(p, !this.playerIsWhite, false, p.f9x, p.f10y);
                    }
                    for (Piece p2 : packet.blackPieces) {
                        addPieceWithButton(p2, this.playerIsWhite, true, p2.f9x, p2.f10y);
                    }
                    initPieceXandY();
                    if (this.isExpectedPlayer) {
                        mirrorBoard();
                    }
                }
            }
        } else {
            this.restartingGame = false;
        }
        if (!addedPieces) {
            assignWhitePlayer();
            if (this.playerIsWhite) {
                addPieces(false, false);
                addPieces(true, true);
            } else {
                addPieces(true, false);
                addPieces(false, true);
            }
        }
        assignKingReferences();
        CheckState checkState = getCheckState();
        this.cachedCheckState = checkState;
        if (checkState == CheckState.BLACK_CHECKMATE || checkState == CheckState.WHITE_CHECKMATE) {
            this.canPlay = false;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:44:0x005a, code lost:
    
        if (r0.equals(net.minecraft.client.Minecraft.getInstance().player.getName().getString()) == false) goto L62;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private void assignWhitePlayer() {
        /*
            Method dump skipped, instructions count: 377
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen.assignWhitePlayer():void");
    }

    private void assignKingReferences() {
        for (Piece p : this.whitePieces) {
            if (p instanceof KingPiece) {
                this.whiteKing = (KingPiece) p;
            }
        }
        for (Piece p2 : this.blackPieces) {
            if (p2 instanceof KingPiece) {
                this.blackKing = (KingPiece) p2;
            }
        }
    }

    private boolean canCastleLeftCheckInternal(KingPiece king) {
        int kingX = king.f9x;
        int kingY = king.f10y;
        try {
            if (king.inCheck()) {
                return false;
            }
            int checkX = king.f9x;
            for (int checkX2 = checkX - 1; checkX2 > 0; checkX2--) {
                Piece p = getPieceAt(checkX2, king.f10y);
                if (p != null) {
                    king.absMoveTo(kingX, kingY);
                    return false;
                }
            }
            int newKingX = king.f9x;
            for (int newKingX2 = newKingX - 1; newKingX2 > 1; newKingX2--) {
                if (!king.canMove(newKingX2, kingY)) {
                    king.absMoveTo(kingX, kingY);
                    return false;
                }
                king.move(newKingX2, kingY);
            }
            king.absMoveTo(kingX, kingY);
            king.absMoveTo(kingX, kingY);
            return true;
        } finally {
            king.absMoveTo(kingX, kingY);
        }
    }

    private boolean canCastleRightCheckInternal(KingPiece king) {
        int kingX = king.f9x;
        int kingY = king.f10y;
        try {
            if (king.inCheck()) {
                return false;
            }
            int checkX = king.f9x;
            for (int checkX2 = checkX + 1; checkX2 < 7; checkX2++) {
                Piece p = getPieceAt(checkX2, king.f10y);
                if (p != null) {
                    king.absMoveTo(kingX, kingY);
                    return false;
                }
            }
            int newKingX = king.f9x;
            for (int newKingX2 = newKingX + 1; newKingX2 < 7; newKingX2++) {
                if (!king.canMove(newKingX2, kingY)) {
                    king.absMoveTo(kingX, kingY);
                    return false;
                }
                king.move(newKingX2, kingY);
            }
            king.absMoveTo(kingX, kingY);
            return true;
        } finally {
            king.absMoveTo(kingX, kingY);
        }
    }

    private boolean canCastleLeft() {
        if (this.canPlay) {
            if (this.playerIsWhite) {
                if (!this.castleFlags.whiteKingHasMoved && !this.castleFlags.whiteLeftRookHasMoved) {
                    return canCastleLeftCheckInternal(this.whiteKing);
                }
                return false;
            }
            if (!this.castleFlags.blackKingHasMoved && !this.castleFlags.blackLeftRookHasMoved) {
                return canCastleLeftCheckInternal(this.blackKing);
            }
            return false;
        }
        return false;
    }

    private boolean canCastleRight() {
        if (this.canPlay) {
            if (this.playerIsWhite) {
                if (!this.castleFlags.whiteKingHasMoved && !this.castleFlags.whiteRightRookHasMoved) {
                    return canCastleRightCheckInternal(this.whiteKing);
                }
                return false;
            }
            if (!this.castleFlags.blackKingHasMoved && !this.castleFlags.blackRightRookHasMoved) {
                return canCastleRightCheckInternal(this.blackKing);
            }
            return false;
        }
        return false;
    }

    private boolean canCastle() {
        return canCastleLeft() || canCastleRight();
    }

    /* JADX WARN: Removed duplicated region for block: B:13:0x0058  */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0031  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private boolean canRestart() {
        /*
            r3 = this;
            r0 = r3
            java.lang.String r0 = r0.whitePlayer
            if (r0 == 0) goto L28
            r0 = r3
            java.lang.String r0 = r0.whitePlayer
            r1 = r3
            net.minecraft.client.Minecraft r1 = r1.minecraft
            net.minecraft.client.Minecraft r1 = net.minecraft.client.Minecraft.getInstance()
            net.minecraft.client.player.LocalPlayer r1 = r1.player
            net.minecraft.network.chat.Component r1 = r1.getName()
            java.lang.String r1 = r1.getString()
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L28
            r0 = 1
            goto L29
        L28:
            r0 = 0
        L29:
            r4 = r0
            r0 = r3
            java.lang.String r0 = r0.blackPlayer
            if (r0 == 0) goto L52
            r0 = r3
            java.lang.String r0 = r0.blackPlayer
            r1 = r3
            net.minecraft.client.Minecraft r1 = r1.minecraft
            net.minecraft.client.Minecraft r1 = net.minecraft.client.Minecraft.getInstance()
            net.minecraft.client.player.LocalPlayer r1 = r1.player
            net.minecraft.network.chat.Component r1 = r1.getName()
            java.lang.String r1 = r1.getString()
            boolean r0 = r0.equals(r1)
            if (r0 == 0) goto L52
            r0 = 1
            goto L53
        L52:
            r0 = 0
        L53:
            r5 = r0
            r0 = r4
            if (r0 != 0) goto L5c
            r0 = r5
            if (r0 == 0) goto L67
        L5c:
            r0 = r3
            int r0 = r0.currentTurn
            if (r0 <= 0) goto L67
            r0 = 1
            goto L68
        L67:
            r0 = 0
        L68:
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen.canRestart():boolean");
    }

    protected void init() {
        addPieces();
        int startX = ((((this.width - 230) / 2) + 230) - 3) - 42;
        int startY = ((this.height - 230) / 2) + 216;
        if (canCastleLeft()) {
            this.leftCastleButton = addRenderableWidget(new ImageButton(startX, startY, UI_BUTTON_WIDTH, 13, 47, 230, 13, CHESS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen.1
                final /* synthetic */ ChessScreen val$thisScreen;

                C01091(ChessScreen this) {
                    this = this;
                }

                public void onPress(Button arg0) {
                    Piece kingPiece;
                    Piece rookPiece;
                    if (!ChessScreen.this.canPlay) {
                        return;
                    }
                    if (ChessScreen.this.leftCastleButtonState == 0) {
                        ChessScreen.this.restartButtonState = 0;
                        ChessScreen.this.rightCastleButtonState = 0;
                        ChessScreen.this.leftCastleButtonState = 1;
                        Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                        return;
                    }
                    if (ChessScreen.this.leftCastleButtonState == 1) {
                        if (ChessScreen.this.playerIsWhite) {
                            kingPiece = this.whiteKing;
                        } else {
                            kingPiece = this.blackKing;
                        }
                        if (kingPiece != null && (kingPiece instanceof KingPiece) && (rookPiece = this.getPieceAt(0, 7)) != null && (rookPiece instanceof RookPiece)) {
                            kingPiece.absMoveTo(kingPiece.f9x - 2, kingPiece.f10y);
                            rookPiece.absMoveTo(kingPiece.f9x + 1, kingPiece.f10y);
                            if (ChessScreen.this.playerIsWhite) {
                                ChessScreen.this.castleFlags.whiteKingHasMoved = true;
                            } else {
                                ChessScreen.this.castleFlags.blackKingHasMoved = true;
                            }
                            ChessScreen.this.movedPiece = kingPiece;
                            ChessScreen.this.assignNamesAndSubmitTurn();
                        }
                    }
                }
            }));
        }
        if (canCastleRight()) {
            this.rightCastleButton = addRenderableWidget(new ImageButton(startX + 1 + UI_BUTTON_WIDTH, startY, UI_BUTTON_WIDTH, 13, 61, 230, 13, CHESS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen.2
                final /* synthetic */ ChessScreen val$thisScreen;

                C01102(ChessScreen this) {
                    this = this;
                }

                public void onPress(Button arg0) {
                    Piece kingPiece;
                    Piece rookPiece;
                    if (!ChessScreen.this.canPlay) {
                        return;
                    }
                    if (ChessScreen.this.rightCastleButtonState == 0) {
                        ChessScreen.this.restartButtonState = 0;
                        ChessScreen.this.leftCastleButtonState = 0;
                        ChessScreen.this.rightCastleButtonState = 1;
                        Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                        return;
                    }
                    if (ChessScreen.this.rightCastleButtonState == 1) {
                        if (ChessScreen.this.playerIsWhite) {
                            kingPiece = this.whiteKing;
                        } else {
                            kingPiece = this.blackKing;
                        }
                        if (kingPiece != null && (kingPiece instanceof KingPiece) && (rookPiece = this.getPieceAt(7, 7)) != null && (rookPiece instanceof RookPiece)) {
                            kingPiece.absMoveTo(kingPiece.f9x + 2, kingPiece.f10y);
                            rookPiece.absMoveTo(kingPiece.f9x - 1, kingPiece.f10y);
                            if (ChessScreen.this.playerIsWhite) {
                                ChessScreen.this.castleFlags.whiteKingHasMoved = true;
                            } else {
                                ChessScreen.this.castleFlags.blackKingHasMoved = true;
                            }
                            ChessScreen.this.movedPiece = kingPiece;
                            ChessScreen.this.assignNamesAndSubmitTurn();
                        }
                    }
                }
            }));
        }
        if (canRestart()) {
            this.restartButton = addRenderableWidget(new ImageButton(startX + 2 + 28, startY, UI_BUTTON_WIDTH, 13, 75, 230, 13, CHESS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen.3
                final /* synthetic */ ChessScreen val$thisScreen;

                C01113(ChessScreen this) {
                    this = this;
                }

                public void onPress(Button arg0) {
                    if (ChessScreen.this.canRestart()) {
                        if (ChessScreen.this.restartButtonState == 0) {
                            ChessScreen.this.leftCastleButtonState = 0;
                            ChessScreen.this.rightCastleButtonState = 0;
                            ChessScreen.this.restartButtonState = 1;
                            Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                            return;
                        }
                        if (ChessScreen.this.restartButtonState == 1) {
                            BlockEntity tileEntity = Minecraft.getInstance().level.getBlockEntity(ChessScreen.this.chessBoardPos);
                            if (tileEntity instanceof ChessBoardBlockEntity) {
                                ChessBoardBlockEntity chessBoard = (ChessBoardBlockEntity) tileEntity;
                                ItemStack itemStack = chessBoard.getItem(0);
                                if (itemStack.getItem() == CustomBlocks.ITEM_CHESS_BOARD.get() && itemStack.getTag() != null && itemStack.getTag().contains("BoardState")) {
                                    itemStack.setTag(new CompoundTag());
                                    this.whitePieces.clear();
                                    this.blackPieces.clear();
                                    this.movedPiece = null;
                                    this.clearWidgets();
                                    ChessScreen.this.restartingGame = true;
                                    ChessScreen.this.whitePlayer = null;
                                    ChessScreen.this.blackPlayer = null;
                                    this.init();
                                    ChessScreen.this.castleFlags = new CastleFlags();
                                    CheckState newCheckState = this.getCheckState();
                                    ChessScreen.this.mirrorBoard();
                                    ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundChessStatePacket(0, newCheckState.ordinal(), ChessScreen.this.whitePlayer, ChessScreen.this.blackPlayer, true, ChessScreen.this.chessBoardPos, this.whitePieces, this.blackPieces, ChessScreen.this.movedPiece, ChessScreen.this.castleFlags));
                                }
                            }
                        }
                    }
                }
            }));
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/ChessScreen$1.class */
    public class C01091 implements Button.OnPress {
        final /* synthetic */ ChessScreen val$thisScreen;

        C01091(ChessScreen this) {
            this = this;
        }

        public void onPress(Button arg0) {
            Piece kingPiece;
            Piece rookPiece;
            if (!ChessScreen.this.canPlay) {
                return;
            }
            if (ChessScreen.this.leftCastleButtonState == 0) {
                ChessScreen.this.restartButtonState = 0;
                ChessScreen.this.rightCastleButtonState = 0;
                ChessScreen.this.leftCastleButtonState = 1;
                Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                return;
            }
            if (ChessScreen.this.leftCastleButtonState == 1) {
                if (ChessScreen.this.playerIsWhite) {
                    kingPiece = this.whiteKing;
                } else {
                    kingPiece = this.blackKing;
                }
                if (kingPiece != null && (kingPiece instanceof KingPiece) && (rookPiece = this.getPieceAt(0, 7)) != null && (rookPiece instanceof RookPiece)) {
                    kingPiece.absMoveTo(kingPiece.f9x - 2, kingPiece.f10y);
                    rookPiece.absMoveTo(kingPiece.f9x + 1, kingPiece.f10y);
                    if (ChessScreen.this.playerIsWhite) {
                        ChessScreen.this.castleFlags.whiteKingHasMoved = true;
                    } else {
                        ChessScreen.this.castleFlags.blackKingHasMoved = true;
                    }
                    ChessScreen.this.movedPiece = kingPiece;
                    ChessScreen.this.assignNamesAndSubmitTurn();
                }
            }
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/ChessScreen$2.class */
    public class C01102 implements Button.OnPress {
        final /* synthetic */ ChessScreen val$thisScreen;

        C01102(ChessScreen this) {
            this = this;
        }

        public void onPress(Button arg0) {
            Piece kingPiece;
            Piece rookPiece;
            if (!ChessScreen.this.canPlay) {
                return;
            }
            if (ChessScreen.this.rightCastleButtonState == 0) {
                ChessScreen.this.restartButtonState = 0;
                ChessScreen.this.leftCastleButtonState = 0;
                ChessScreen.this.rightCastleButtonState = 1;
                Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                return;
            }
            if (ChessScreen.this.rightCastleButtonState == 1) {
                if (ChessScreen.this.playerIsWhite) {
                    kingPiece = this.whiteKing;
                } else {
                    kingPiece = this.blackKing;
                }
                if (kingPiece != null && (kingPiece instanceof KingPiece) && (rookPiece = this.getPieceAt(7, 7)) != null && (rookPiece instanceof RookPiece)) {
                    kingPiece.absMoveTo(kingPiece.f9x + 2, kingPiece.f10y);
                    rookPiece.absMoveTo(kingPiece.f9x - 1, kingPiece.f10y);
                    if (ChessScreen.this.playerIsWhite) {
                        ChessScreen.this.castleFlags.whiteKingHasMoved = true;
                    } else {
                        ChessScreen.this.castleFlags.blackKingHasMoved = true;
                    }
                    ChessScreen.this.movedPiece = kingPiece;
                    ChessScreen.this.assignNamesAndSubmitTurn();
                }
            }
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.chess.ChessScreen$3 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/chess/ChessScreen$3.class */
    public class C01113 implements Button.OnPress {
        final /* synthetic */ ChessScreen val$thisScreen;

        C01113(ChessScreen this) {
            this = this;
        }

        public void onPress(Button arg0) {
            if (ChessScreen.this.canRestart()) {
                if (ChessScreen.this.restartButtonState == 0) {
                    ChessScreen.this.leftCastleButtonState = 0;
                    ChessScreen.this.rightCastleButtonState = 0;
                    ChessScreen.this.restartButtonState = 1;
                    Minecraft.getInstance().player.displayClientMessage(new TextComponent("Click again to confirm"), false);
                    return;
                }
                if (ChessScreen.this.restartButtonState == 1) {
                    BlockEntity tileEntity = Minecraft.getInstance().level.getBlockEntity(ChessScreen.this.chessBoardPos);
                    if (tileEntity instanceof ChessBoardBlockEntity) {
                        ChessBoardBlockEntity chessBoard = (ChessBoardBlockEntity) tileEntity;
                        ItemStack itemStack = chessBoard.getItem(0);
                        if (itemStack.getItem() == CustomBlocks.ITEM_CHESS_BOARD.get() && itemStack.getTag() != null && itemStack.getTag().contains("BoardState")) {
                            itemStack.setTag(new CompoundTag());
                            this.whitePieces.clear();
                            this.blackPieces.clear();
                            this.movedPiece = null;
                            this.clearWidgets();
                            ChessScreen.this.restartingGame = true;
                            ChessScreen.this.whitePlayer = null;
                            ChessScreen.this.blackPlayer = null;
                            this.init();
                            ChessScreen.this.castleFlags = new CastleFlags();
                            CheckState newCheckState = this.getCheckState();
                            ChessScreen.this.mirrorBoard();
                            ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundChessStatePacket(0, newCheckState.ordinal(), ChessScreen.this.whitePlayer, ChessScreen.this.blackPlayer, true, ChessScreen.this.chessBoardPos, this.whitePieces, this.blackPieces, ChessScreen.this.movedPiece, ChessScreen.this.castleFlags));
                        }
                    }
                }
            }
        }
    }

    public boolean isPauseScreen() {
        return false;
    }

    private void drawCenteredStringNoShadow(PoseStack p_238472_0_, Font p_238472_1_, Component p_238472_2_, int p_238472_3_, int p_238472_4_, int p_238472_5_) {
        FormattedCharSequence lvt_6_1_ = p_238472_2_.getVisualOrderText();
        p_238472_1_.draw(p_238472_0_, lvt_6_1_, p_238472_3_ - (p_238472_1_.width(lvt_6_1_) / 2), p_238472_4_, p_238472_5_);
    }

    int availableMoveCount(Piece p) {
        int moveCount = 0;
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                if (p.canMove(x, y)) {
                    moveCount++;
                }
            }
        }
        return moveCount;
    }

    int whiteAvailableMoveCount() {
        int moveCount = 0;
        for (Piece p : this.whitePieces) {
            moveCount += availableMoveCount(p);
        }
        return moveCount;
    }

    int blackAvailableMoveCount() {
        int moveCount = 0;
        for (Piece p : this.blackPieces) {
            moveCount += availableMoveCount(p);
        }
        return moveCount;
    }

    public void showTooltipForPiece(PoseStack matrixStack, Piece p) {
        String pieceName = "";
        if (p instanceof RookPiece) {
            pieceName = "Rook";
        } else if (p instanceof BishopPiece) {
            pieceName = "Bishop";
        } else if (p instanceof PawnPiece) {
            pieceName = "Pawn";
        } else if (p instanceof KnightPiece) {
            pieceName = "Knight";
        } else if (p instanceof KingPiece) {
            pieceName = "King";
        } else if (p instanceof QueenPiece) {
            pieceName = "Queen";
        }
        renderTooltip(matrixStack, new TextComponent(pieceName), boardXtoScreenX(p.f9x), boardYtoScreenY(p.f10y));
    }

    public void render(PoseStack matrixStack, int p_230430_2_, int p_230430_3_, float p_230430_4_) {
        renderBackground(matrixStack);
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderTexture(0, CHESS_UI);
        int xStart = (this.width - 230) / 2;
        int yStart = (this.height - 230) / 2;
        m_93228_(matrixStack, xStart, yStart, 0, 0, 230, 230);
        if (this.movedPiece != null) {
            int screenX = boardXtoScreenX(this.movedPiece.f9x);
            int screenY = boardYtoScreenY(this.movedPiece.f10y);
            m_93179_(matrixStack, screenX, screenY, (screenX + 23) - 1, (screenY + 23) - 1, 1348134655, 1348134655);
        }
        if (this.drawSelectionX != -1 && this.drawSelectionY != -1 && this.selectedPiece != null) {
            m_93179_(matrixStack, this.drawSelectionX, this.drawSelectionY, (this.drawSelectionX + 23) - 1, (this.drawSelectionY + 23) - 1, 1358888960, 1342177280);
            for (int x = 0; x < 8; x++) {
                for (int y = 0; y < 8; y++) {
                    if (this.selectedPiece.canMove(x, y)) {
                        int screenX2 = boardXtoScreenX(x);
                        int screenY2 = boardYtoScreenY(y);
                        m_93179_(matrixStack, screenX2, screenY2, (screenX2 + 23) - 1, (screenY2 + 23) - 1, 1342242560, 1342177280);
                    }
                }
            }
        }
        CheckState checkState = this.cachedCheckState;
        if (checkState == CheckState.BLACK_CHECKMATE || checkState == CheckState.WHITE_CHECKMATE) {
            String toDisplay = "";
            if (checkState == CheckState.BLACK_CHECKMATE) {
                toDisplay = "WHITE WINS!  Black checkmate";
            } else if (checkState == CheckState.WHITE_CHECKMATE) {
                toDisplay = "BLACK WINS!  White checkmate";
            }
            m_93215_(matrixStack, this.minecraft.font, new TextComponent(toDisplay).m_130948_(Style.EMPTY.withColor(TextColor.fromRgb(-65536))), xStart + 115, yStart + 2, 0);
        } else if (checkState != CheckState.NONE) {
            String toDisplay2 = "";
            if (checkState == CheckState.BLACK_CHECK) {
                toDisplay2 = "BLACK CHECK";
            } else if (checkState == CheckState.WHITE_CHECK) {
                toDisplay2 = "WHITE CHECK";
            }
            m_93215_(matrixStack, this.minecraft.font, new TextComponent(toDisplay2).m_130948_(Style.EMPTY.withColor(TextColor.fromRgb(-65536))), xStart + 115, yStart + 2, 0);
        }
        if (this.canPlay) {
            m_93215_(matrixStack, this.minecraft.font, new TextComponent("It's your turn").m_130948_(Style.EMPTY.withColor(TextColor.fromRgb(-1))), xStart + 115, (yStart + 230) - 13, 0);
        }
        for (Piece p : this.whitePieces) {
            if (p.button.m_198029_()) {
                showTooltipForPiece(matrixStack, p);
            }
        }
        for (Piece p2 : this.blackPieces) {
            if (p2.button.m_198029_()) {
                showTooltipForPiece(matrixStack, p2);
            }
        }
        if (this.leftCastleButton != null && this.leftCastleButton.m_198029_()) {
            renderTooltip(matrixStack, new TextComponent("Castle Left (Special Move)"), this.leftCastleButton.f_93620_, this.leftCastleButton.f_93621_);
        }
        if (this.rightCastleButton != null && this.rightCastleButton.m_198029_()) {
            renderTooltip(matrixStack, new TextComponent("Castle Right (Special Move)"), this.rightCastleButton.f_93620_, this.rightCastleButton.f_93621_);
        }
        if (this.restartButton != null && this.restartButton.m_198029_()) {
            renderTooltip(matrixStack, new TextComponent("Restart Game"), this.restartButton.f_93620_, this.restartButton.f_93621_);
        }
        super.render(matrixStack, p_230430_2_, p_230430_3_, p_230430_4_);
    }
}
