package com.dairymoose.modernlife.blocks.gui;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.core.ModernLifeNetwork;
import com.dairymoose.modernlife.network.play.client.ServerboundPrintSizePacket;
import com.dairymoose.modernlife.util.CanvasData;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import java.text.DecimalFormat;
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.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/CustomPrintScreen.class */
public class CustomPrintScreen extends Screen {
    final int GUI_WIDTH = 80;
    final int GUI_HEIGHT = 110;
    BlockPos pos;
    private static final String PRINT_SIZE = "Canvas Resize";
    public static final int FIELD_START_X = 5;
    public static final int FIELD_START_RIGHT_X = 48;
    public static final int FIELD_START_Y = 20;
    public static final int FIELD_START_Y2 = 55;
    public static final int FIELD_START_Y3 = 88;
    public static final int INPUT_FIELD_WIDTH = 27;
    public static final int INPUT_FIELD_HEIGHT = 16;
    public static final int TEXT_START_OFFSET_X = 13;
    public static final int TEXT_START_OFFSET_Y = 3;
    private int chosenFieldId;
    private static final int FIELD_ID_WIDTH = 0;
    private static final int FIELD_ID_HEIGHT = 1;
    private static final int FIELD_ID_X_OFFSET = 2;
    private static final int FIELD_ID_Y_OFFSET = 3;
    public static final int CANCEL_BUTTON_START_X = 56;
    public static final int CANCEL_BUTTON_START_Y = 68;
    public static final int ACCEPT_BUTTON_START_X = 56;
    public static final int ACCEPT_BUTTON_START_Y = 88;
    private float blockWidth;
    private float blockHeight;
    private float xOffset;
    private float yOffset;
    private int blockWidthInputLength;
    private int blockHeightInputLength;
    private int xOffsetInputLength;
    private int yOffsetInputLength;
    private boolean negateX;
    private boolean negateY;
    private static final Logger LOGGER = LogManager.getLogger();
    private static final ResourceLocation PRINTER_GUI = new ResourceLocation("modernlife", "textures/gui/gui_printer_custom.png");
    public static int grayColor = 9145227;
    public static int chosenX = -1;
    public static int chosenY = -1;

    private static Component getComponentForTitle(String title) {
        return new TextComponent(title).m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(4210752)));
    }

    public CustomPrintScreen(BlockPos pos) {
        super(getComponentForTitle(PRINT_SIZE));
        CompoundTag tag;
        this.GUI_WIDTH = 80;
        this.GUI_HEIGHT = 110;
        this.chosenFieldId = -1;
        this.blockWidth = 1.0f;
        this.blockHeight = 1.0f;
        this.xOffset = 0.0f;
        this.yOffset = 0.0f;
        this.blockWidthInputLength = 0;
        this.blockHeightInputLength = 0;
        this.xOffsetInputLength = 0;
        this.yOffsetInputLength = 0;
        this.negateX = false;
        this.negateY = false;
        chosenX = -1;
        chosenY = -1;
        this.pos = pos;
        ItemStack itemStack = Minecraft.getInstance().player.getMainHandItem();
        if (itemStack != null && itemStack.is(CustomBlocks.ITEM_CANVAS.get()) && (tag = itemStack.getTag()) != null && tag.contains("Size")) {
            int size = tag.getInt("Size");
            if (size == 0) {
                if (tag.contains("BlockWidth") && tag.contains("BlockHeight")) {
                    float blockWidth = tag.getFloat("BlockWidth");
                    float blockHeight = tag.getFloat("BlockHeight");
                    this.blockWidth = blockWidth;
                    this.blockHeight = blockHeight;
                }
                if (tag.contains("xOffset") && tag.contains("yOffset")) {
                    float xOffset = tag.getFloat("xOffset");
                    float yOffset = tag.getFloat("yOffset");
                    this.xOffset = xOffset;
                    this.yOffset = yOffset;
                    return;
                }
                return;
            }
            this.xOffset = 0.0f;
            this.yOffset = 0.0f;
            if (size == 1) {
                this.blockWidth = 1.0f;
                this.blockHeight = 1.0f;
                return;
            }
            if (size == FIELD_ID_X_OFFSET) {
                this.blockWidth = 1.5f;
                this.blockHeight = 1.25f;
                this.yOffset = 0.125f;
                return;
            }
            if (size == 3) {
                this.blockWidth = 2.0f;
                this.blockHeight = 1.5f;
                this.yOffset = 0.25f;
                return;
            }
            if (size == 4) {
                this.blockWidth = 3.0f;
                this.blockHeight = 2.0f;
                this.yOffset = 0.5f;
            } else if (size == 5) {
                this.blockWidth = 1.625f;
                this.blockHeight = 1.625f;
            } else if (size == 6) {
                this.blockWidth = 2.25f;
                this.blockHeight = 2.25f;
            } else if (size == 7) {
                this.blockWidth = 3.0f;
                this.blockHeight = 3.0f;
            }
        }
    }

    public boolean m_6375_(double clickX, double clickY, int mouseButton) {
        int xStart = (this.width - 80) / FIELD_ID_X_OFFSET;
        int yStart = (this.height - 110) / FIELD_ID_X_OFFSET;
        int fieldStartX = xStart + 5;
        int fieldStartRightX = xStart + 48;
        int fieldStartY = yStart + 20;
        int fieldStartY2 = yStart + 55;
        int fieldStartY3 = yStart + 88;
        int fieldEndX = fieldStartX + 27;
        int fieldEndRightX = fieldStartRightX + 27;
        int fieldEndY = fieldStartY + 16;
        int fieldEndY2 = fieldStartY2 + 16;
        int fieldEndY3 = fieldStartY3 + 16;
        if (mouseButton == 0) {
            if (clickX >= fieldStartX && clickX <= fieldEndX) {
                if (clickY >= fieldStartY && clickY <= fieldEndY) {
                    this.chosenFieldId = 0;
                    setChosenFieldToZero();
                    chosenX = fieldStartX;
                    chosenY = fieldStartY;
                }
                if (clickY >= fieldStartY2 && clickY <= fieldEndY2) {
                    this.chosenFieldId = FIELD_ID_X_OFFSET;
                    setChosenFieldToZero();
                    chosenX = fieldStartX;
                    chosenY = fieldStartY2;
                }
                if (clickY >= fieldStartY3 && clickY <= fieldEndY3) {
                    this.chosenFieldId = 3;
                    setChosenFieldToZero();
                    chosenX = fieldStartX;
                    chosenY = fieldStartY3;
                }
            }
            if (clickX >= fieldStartRightX && clickX <= fieldEndRightX && clickY >= fieldStartY && clickY <= fieldEndY) {
                this.chosenFieldId = 1;
                setChosenFieldToZero();
                chosenX = fieldStartRightX;
                chosenY = fieldStartY;
            }
        }
        return super.m_6375_(clickX, clickY, mouseButton);
    }

    public int getDigitCount(float f) {
        float remainder = f;
        int count = 0;
        while (remainder != 0.0f) {
            int truncated = (int) remainder;
            count++;
            if (truncated != 0) {
                remainder %= truncated;
            }
            remainder *= 10.0f;
        }
        return count;
    }

    public float applyInputToFloatField(float field, float input, int inputCount) {
        float fInput = input;
        for (int i = 0; i < inputCount; i++) {
            fInput /= 10.0f;
        }
        return field + fInput;
    }

    public void addInputToChosenField(int input) {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.blockWidth = applyInputToFloatField(this.blockWidth, input, this.blockWidthInputLength);
            this.blockWidth = ServerboundPrintSizePacket.sanitizeWidthHeight(this.blockWidth);
            this.blockWidthInputLength++;
        }
        if (this.chosenFieldId == 1) {
            this.blockHeight = applyInputToFloatField(this.blockHeight, input, this.blockHeightInputLength);
            this.blockHeight = ServerboundPrintSizePacket.sanitizeWidthHeight(this.blockHeight);
            this.blockHeightInputLength++;
        }
        if (this.chosenFieldId == FIELD_ID_X_OFFSET) {
            this.xOffset = applyInputToFloatField(this.xOffset, input, this.xOffsetInputLength);
            this.xOffset = ServerboundPrintSizePacket.sanitizeOffset(this.xOffset);
            this.xOffsetInputLength++;
        }
        if (this.chosenFieldId == 3) {
            this.yOffset = applyInputToFloatField(this.yOffset, input, this.yOffsetInputLength);
            this.yOffset = ServerboundPrintSizePacket.sanitizeOffset(this.yOffset);
            this.yOffsetInputLength++;
        }
    }

    public void setChosenFieldToZero() {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.blockWidth = 0.0f;
            this.blockWidthInputLength = 0;
        }
        if (this.chosenFieldId == 1) {
            this.blockHeight = 0.0f;
            this.blockHeightInputLength = 0;
        }
        if (this.chosenFieldId == FIELD_ID_X_OFFSET) {
            this.xOffset = 0.0f;
            this.negateX = false;
            this.xOffsetInputLength = 1;
        }
        if (this.chosenFieldId == 3) {
            this.yOffset = 0.0f;
            this.negateY = false;
            this.yOffsetInputLength = 1;
        }
    }

    public void backspaceChosenField() {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.blockWidth = (int) this.blockWidth;
            this.blockWidthInputLength = 1;
        }
        if (this.chosenFieldId == 1) {
            this.blockHeight = (int) this.blockHeight;
            this.blockHeightInputLength = 1;
        }
        if (this.chosenFieldId == FIELD_ID_X_OFFSET) {
            this.xOffset = (int) this.xOffset;
            this.xOffsetInputLength = 1;
        }
        if (this.chosenFieldId == 3) {
            this.yOffset = (int) this.yOffset;
            this.yOffsetInputLength = 1;
        }
    }

    public void negateChosenField() {
        if (this.chosenFieldId == FIELD_ID_X_OFFSET) {
            this.negateX = !this.negateX;
        }
        if (this.chosenFieldId == 3) {
            this.negateY = !this.negateY;
        }
    }

    public boolean keyPressed(int key, int p_231046_2_, int p_231046_3_) {
        ModernLifeCommon.LOGGER.debug("key press: " + key);
        if (key == 261) {
            setChosenFieldToZero();
        } else if (key == 259) {
            backspaceChosenField();
        } else if (key == 48 || (key >= 49 && key <= 57)) {
            int digit = Character.digit(key, 10);
            addInputToChosenField(digit);
        } else if (key == 45) {
            negateChosenField();
        } else if (key == 257) {
            return true;
        }
        return super.keyPressed(key, p_231046_2_, p_231046_3_);
    }

    protected void init() {
        int startX = (this.width - 80) / FIELD_ID_X_OFFSET;
        int startY = (this.height - 110) / FIELD_ID_X_OFFSET;
        addRenderableWidget(new ImageButton(startX + 56, startY + 68, 20, 18, 215, 0, 19, PRINTER_GUI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.CustomPrintScreen.1
            C00721() {
            }

            public void onPress(Button arg0) {
                Minecraft.getInstance().setScreen((Screen) null);
            }
        }));
        addRenderableWidget(new ImageButton(startX + 56, startY + 88, 20, 18, 236, 0, 19, PRINTER_GUI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.CustomPrintScreen.2
            C00732() {
            }

            public void onPress(Button arg0) {
                ItemStack itemStack = Minecraft.getInstance().player.getMainHandItem();
                if (itemStack != null && ((itemStack.is(CustomBlocks.ITEM_CANVAS.get()) && itemStack.getTag() != null && itemStack.getTag().contains("UniqueId")) || itemStack.is(CustomBlocks.ITEM_CAMERA.get()))) {
                    float nX = CustomPrintScreen.this.xOffset;
                    if (CustomPrintScreen.this.negateX) {
                        nX = -nX;
                    }
                    float nY = CustomPrintScreen.this.yOffset;
                    if (CustomPrintScreen.this.negateY) {
                        nY = -nY;
                    }
                    ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundPrintSizePacket(CustomPrintScreen.this.blockWidth, CustomPrintScreen.this.blockHeight, nX, nY, CustomPrintScreen.this.pos));
                }
                Minecraft.getInstance().setScreen((Screen) null);
            }
        }));
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.CustomPrintScreen$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/CustomPrintScreen$1.class */
    class C00721 implements Button.OnPress {
        C00721() {
        }

        public void onPress(Button arg0) {
            Minecraft.getInstance().setScreen((Screen) null);
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.CustomPrintScreen$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/CustomPrintScreen$2.class */
    class C00732 implements Button.OnPress {
        C00732() {
        }

        public void onPress(Button arg0) {
            ItemStack itemStack = Minecraft.getInstance().player.getMainHandItem();
            if (itemStack != null && ((itemStack.is(CustomBlocks.ITEM_CANVAS.get()) && itemStack.getTag() != null && itemStack.getTag().contains("UniqueId")) || itemStack.is(CustomBlocks.ITEM_CAMERA.get()))) {
                float nX = CustomPrintScreen.this.xOffset;
                if (CustomPrintScreen.this.negateX) {
                    nX = -nX;
                }
                float nY = CustomPrintScreen.this.yOffset;
                if (CustomPrintScreen.this.negateY) {
                    nY = -nY;
                }
                ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundPrintSizePacket(CustomPrintScreen.this.blockWidth, CustomPrintScreen.this.blockHeight, nX, nY, CustomPrintScreen.this.pos));
            }
            Minecraft.getInstance().setScreen((Screen) null);
        }
    }

    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_) / FIELD_ID_X_OFFSET), p_238472_4_, p_238472_5_);
    }

    public void render(PoseStack matrixStack, int p_230430_2_, int p_230430_3_, float p_230430_4_) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderTexture(0, PRINTER_GUI);
        int xStart = (this.width - 80) / FIELD_ID_X_OFFSET;
        int yStart = (this.height - 110) / FIELD_ID_X_OFFSET;
        m_93228_(matrixStack, xStart, yStart, 0, 0, 80, 110);
        int fieldStartX = xStart + 5;
        int fieldStartRightX = xStart + 48;
        int fieldStartY = yStart + 20;
        int fieldStartY2 = yStart + 55;
        int fieldStartY3 = yStart + 88;
        int r = CanvasData.getRValue(grayColor);
        int g = CanvasData.getGValue(grayColor);
        int b = CanvasData.getBValue(grayColor);
        float fR = r / 255.0f;
        float fG = g / 255.0f;
        float fB = b / 255.0f;
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderTexture(0, PRINTER_GUI);
        RenderSystem.setShaderColor(fR, fG, fB, 1.0f);
        m_93228_(matrixStack, fieldStartX, fieldStartY, 5, 20, 27, 16);
        m_93228_(matrixStack, fieldStartRightX, fieldStartY, 48, 20, 27, 16);
        m_93228_(matrixStack, fieldStartX, fieldStartY2, 5, 55, 27, 16);
        m_93228_(matrixStack, fieldStartX, fieldStartY3, 5, 88, 27, 16);
        if (chosenX > 0 && chosenY > 0) {
            RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
            m_93228_(matrixStack, chosenX, chosenY, 5, 20, 27, 16);
        }
        DecimalFormat df = new DecimalFormat("#.##");
        String textWidth = df.format(this.blockWidth);
        String textHeight = df.format(this.blockHeight);
        float nX = this.xOffset;
        if (this.negateX) {
            nX = -nX;
        }
        float nY = this.yOffset;
        if (this.negateY) {
            nY = -nY;
        }
        String textXOffset = df.format(nX);
        String textYOffset = df.format(nY);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(textWidth), fieldStartX + 13, fieldStartY + 3, 0);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(textHeight), fieldStartRightX + 13, fieldStartY + 3, 0);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(textXOffset), fieldStartX + 13, fieldStartY2 + 3, 0);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(textYOffset), fieldStartX + 13, fieldStartY3 + 3, 0);
        super.render(matrixStack, p_230430_2_, p_230430_3_, p_230430_4_);
    }
}
