package com.dairymoose.modernlife.blocks.gui;

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.util.CanvasData;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
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.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.level.Level;
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/EaselCustomSizeScreen.class */
public class EaselCustomSizeScreen extends Screen {
    final int GUI_WIDTH = 80;
    final int GUI_HEIGHT = 58;
    BlockPos pos;
    Level level;
    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 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 = 13;
    public static final int CANCEL_BUTTON_START_Y = 37;
    public static final int ACCEPT_BUTTON_START_X = 47;
    public static final int ACCEPT_BUTTON_START_Y = 37;
    private int texWidth;
    private int texHeight;
    private int blockWidthInputLength;
    private int blockHeightInputLength;
    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_easel_custom_size.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)));
    }

    private void goToEaselScreen() {
        EaselScreen screen = new EaselScreen(this.level, this.pos);
        screen.setTextureSize(this.texWidth, this.texHeight);
        screen.setBlockPos(this.pos);
        Minecraft.getInstance().setScreen(screen);
    }

    public EaselCustomSizeScreen(Level level, BlockPos pos) {
        super(getComponentForTitle(PRINT_SIZE));
        this.GUI_WIDTH = 80;
        this.GUI_HEIGHT = 58;
        this.chosenFieldId = -1;
        this.texWidth = 64;
        this.texHeight = 64;
        this.blockWidthInputLength = 0;
        this.blockHeightInputLength = 0;
        this.negateX = false;
        this.negateY = false;
        chosenX = -1;
        chosenY = -1;
        this.pos = pos;
    }

    public boolean m_6375_(double clickX, double clickY, int mouseButton) {
        int xStart = (this.width - 80) / FIELD_ID_X_OFFSET;
        int yStart = (this.height - 58) / FIELD_ID_X_OFFSET;
        int fieldStartX = xStart + 5;
        int fieldStartRightX = xStart + 48;
        int fieldStartY = yStart + 20;
        int fieldEndX = fieldStartX + 27;
        int fieldEndRightX = fieldStartRightX + 27;
        int fieldEndY = fieldStartY + 16;
        if (mouseButton == 0) {
            if (clickX >= fieldStartX && clickX <= fieldEndX && clickY >= fieldStartY && clickY <= fieldEndY) {
                this.chosenFieldId = 0;
                setChosenFieldToZero();
                chosenX = fieldStartX;
                chosenY = fieldStartY;
            }
            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 applyInputToIntegerField(int field, int input, int inputCount) {
        return (field * 10) + input;
    }

    public static int sanitizeInput(int input) {
        if (input < 0) {
            return 0;
        }
        if (input > 1000) {
            return 1000;
        }
        return input;
    }

    public void addInputToChosenField(int input) {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.texWidth = applyInputToIntegerField(this.texWidth, input, this.blockWidthInputLength);
            this.texWidth = sanitizeInput(this.texWidth);
            this.blockWidthInputLength++;
        }
        if (this.chosenFieldId == 1) {
            this.texHeight = applyInputToIntegerField(this.texHeight, input, this.blockHeightInputLength);
            this.texHeight = sanitizeInput(this.texHeight);
            this.blockHeightInputLength++;
        }
    }

    public void setChosenFieldToZero() {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.texWidth = 0;
            this.blockWidthInputLength = 0;
        }
        if (this.chosenFieldId == 1) {
            this.texHeight = 0;
            this.blockHeightInputLength = 0;
        }
    }

    public void backspaceChosenField() {
        if (this.chosenFieldId == -1) {
            return;
        }
        if (this.chosenFieldId == 0) {
            this.texWidth /= 10;
            this.blockWidthInputLength = 1;
        }
        if (this.chosenFieldId == 1) {
            this.texHeight /= 10;
            this.blockHeightInputLength = 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 - 58) / FIELD_ID_X_OFFSET;
        addRenderableWidget(new ImageButton(startX + 13, startY + 37, 20, 18, 215, 0, 19, PRINTER_GUI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselCustomSizeScreen.1
            C00741() {
            }

            public void onPress(Button arg0) {
                Minecraft.getInstance().setScreen((Screen) null);
            }
        }));
        addRenderableWidget(new ImageButton(startX + 47, startY + 37, 20, 18, 236, 0, 19, PRINTER_GUI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselCustomSizeScreen.2
            final /* synthetic */ EaselCustomSizeScreen val$thisPrinter;

            C00752(EaselCustomSizeScreen this) {
                this = this;
            }

            public void onPress(Button arg0) {
                this.goToEaselScreen();
            }
        }));
    }

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

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

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselCustomSizeScreen$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselCustomSizeScreen$2.class */
    class C00752 implements Button.OnPress {
        final /* synthetic */ EaselCustomSizeScreen val$thisPrinter;

        C00752(EaselCustomSizeScreen this) {
            this = this;
        }

        public void onPress(Button arg0) {
            this.goToEaselScreen();
        }
    }

    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 - 58) / FIELD_ID_X_OFFSET;
        m_93228_(matrixStack, xStart, yStart, 0, 0, 80, 58);
        int fieldStartX = xStart + 5;
        int fieldStartRightX = xStart + 48;
        int fieldStartY = yStart + 20;
        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);
        if (chosenX > 0 && chosenY > 0) {
            RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
            m_93228_(matrixStack, chosenX, chosenY, 5, 20, 27, 16);
        }
        String widthText = String.valueOf(this.texWidth);
        String heightText = String.valueOf(this.texHeight);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(widthText), fieldStartX + 13, fieldStartY + 3, 0);
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(heightText), fieldStartRightX + 13, fieldStartY + 3, 0);
        super.render(matrixStack, p_230430_2_, p_230430_3_, p_230430_4_);
    }
}
