package com.dairymoose.modernlife.blocks.gui;

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.core.ModernLifeConfig;
import com.dairymoose.modernlife.core.ModernLifeNetwork;
import com.dairymoose.modernlife.network.play.client.ServerboundMultipartEaselPacket;
import com.dairymoose.modernlife.util.CanvasData;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.AbstractWidget;
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.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
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/EaselScreen.class */
public class EaselScreen extends Screen {
    final int GUI_WIDTH = 256;
    final int GUI_HEIGHT = 216;
    private final int CANVAS_AREA_WIDTH = 128;
    private final int CANVAS_AREA_HEIGHT = 128;
    private Level world;
    private BlockPos pos;
    List<List<Integer>> colorRowRgbs;
    private int colorRgb;
    private int colorRgbLeft;
    private int colorRgbRight;
    private int lastMouseButton;

    /* renamed from: H */
    private double f6H;

    /* renamed from: S */
    private double f7S;

    /* renamed from: V */
    private double f8V;
    CanvasData canvasData;
    Deque<CanvasData> undoStack;
    private boolean dragStart;
    int dragStartX;
    int dragStartY;
    int lastX;
    int lastY;
    private DynamicTexture texture;
    private TextureManager textureManager;
    private ResourceLocation dynamicTextureResource;
    private boolean firstRender;
    private BlockPos easelPos;
    private long introTimestamp;
    private final int UNDO_STACK_MAX_SIZE = 10;
    private static final Logger LOGGER = LogManager.getLogger();
    private static final ResourceLocation CANVAS_UI = new ResourceLocation("modernlife", "textures/gui/canvas_ui.png");
    private static final ResourceLocation COLOR_PICKER_UI = new ResourceLocation("modernlife", "textures/gui/ui_color_picker.png");
    private static ImageButton pencil = null;
    private static ImageButton paintbrush = null;
    private static ImageButton line = null;
    private static ImageButton bucket = null;
    private static ImageButton colorPicker = null;
    private static ImageButton undo = null;
    private static ImageButton cancel = null;
    private static ImageButton accept = null;

    public EaselScreen(Level world, BlockPos easelBlock) {
        super(new TextComponent("Canvas").m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(4210752))));
        this.GUI_WIDTH = 256;
        this.GUI_HEIGHT = 216;
        this.CANVAS_AREA_WIDTH = 128;
        this.CANVAS_AREA_HEIGHT = 128;
        this.world = null;
        this.pos = null;
        this.colorRgb = 0;
        this.colorRgbLeft = 0;
        this.colorRgbRight = 0;
        this.lastMouseButton = 0;
        this.f6H = -1.0d;
        this.f7S = 0.0d;
        this.f8V = 1.0d;
        this.canvasData = new CanvasData();
        this.undoStack = new LinkedList();
        this.dragStart = false;
        this.dragStartX = -1;
        this.dragStartY = -1;
        this.lastX = -1;
        this.lastY = -1;
        this.texture = null;
        this.textureManager = Minecraft.getInstance().getTextureManager();
        this.dynamicTextureResource = null;
        this.firstRender = true;
        this.easelPos = null;
        this.UNDO_STACK_MAX_SIZE = 10;
        this.world = world;
        this.pos = easelBlock;
        this.introTimestamp = System.currentTimeMillis();
        this.colorRowRgbs = new ArrayList();
        ArrayList<Integer> rgbRow = new ArrayList<>();
        rgbRow.add(13152666);
        rgbRow.add(7929856);
        rgbRow.add(10422798);
        rgbRow.add(15604517);
        rgbRow.add(16017996);
        rgbRow.add(16160633);
        rgbRow.add(15604517);
        rgbRow.add(16711680);
        this.colorRowRgbs.add(rgbRow);
        ArrayList<Integer> rgbRow2 = new ArrayList<>();
        rgbRow2.add(10127476);
        rgbRow2.add(8072704);
        rgbRow2.add(10568204);
        rgbRow2.add(16082463);
        rgbRow2.add(16289878);
        rgbRow2.add(16559489);
        rgbRow2.add(16512000);
        rgbRow2.add(16776960);
        this.colorRowRgbs.add(rgbRow2);
        ArrayList<Integer> rgbRow3 = new ArrayList<>();
        rgbRow3.add(7561815);
        rgbRow3.add(8210432);
        rgbRow3.add(10838281);
        rgbRow3.add(16356635);
        rgbRow3.add(16560477);
        rgbRow3.add(16697228);
        rgbRow3.add(42574);
        rgbRow3.add(65280);
        this.colorRowRgbs.add(rgbRow3);
        ArrayList<Integer> rgbRow4 = new ArrayList<>();
        rgbRow4.add(5457729);
        rgbRow4.add(8682496);
        rgbRow4.add(11378944);
        rgbRow4.add(16775017);
        rgbRow4.add(16775322);
        rgbRow4.add(16775322);
        rgbRow4.add(45549);
        rgbRow4.add(65535);
        this.colorRowRgbs.add(rgbRow4);
        ArrayList<Integer> rgbRow5 = new ArrayList<>();
        rgbRow5.add(3551277);
        rgbRow5.add(4154648);
        rgbRow5.add(5867303);
        rgbRow5.add(9422910);
        rgbRow5.add(11326578);
        rgbRow5.add(12771228);
        rgbRow5.add(2896020);
        rgbRow5.add(255);
        this.colorRowRgbs.add(rgbRow5);
        ArrayList<Integer> rgbRow6 = new ArrayList<>();
        rgbRow6.add(12885100);
        rgbRow6.add(89379);
        rgbRow6.add(1735216);
        rgbRow6.add(3716935);
        rgbRow6.add(8176757);
        rgbRow6.add(10605725);
        rgbRow6.add(15597709);
        rgbRow6.add(16515325);
        this.colorRowRgbs.add(rgbRow6);
        ArrayList<Integer> rgbRow7 = new ArrayList<>();
        rgbRow7.add(10910546);
        rgbRow7.add(23330);
        rgbRow7.add(28982);
        rgbRow7.add(42574);
        rgbRow7.add(3913592);
        rgbRow7.add(8768156);
        rgbRow7.add(9145227);
        rgbRow7.add(16777215);
        this.colorRowRgbs.add(rgbRow7);
        ArrayList<Integer> rgbRow8 = new ArrayList<>();
        rgbRow8.add(9265721);
        rgbRow8.add(22608);
        rgbRow8.add(29546);
        rgbRow8.add(43677);
        rgbRow8.add(1817268);
        rgbRow8.add(8113864);
        rgbRow8.add(8158332);
        rgbRow8.add(15461355);
        this.colorRowRgbs.add(rgbRow8);
        ArrayList<Integer> rgbRow9 = new ArrayList<>();
        rgbRow9.add(7686948);
        rgbRow9.add(22914);
        rgbRow9.add(30372);
        rgbRow9.add(45806);
        rgbRow9.add(48116);
        rgbRow9.add(7262711);
        rgbRow9.add(7434609);
        rgbRow9.add(14803682);
        this.colorRowRgbs.add(rgbRow9);
        ArrayList<Integer> rgbRow10 = new ArrayList<>();
        rgbRow10.add(6371347);
        rgbRow10.add(144743);
        rgbRow10.add(19837);
        rgbRow10.add(29117);
        rgbRow10.add(4361676);
        rgbRow10.add(8169946);
        rgbRow10.add(6513507);
        rgbRow10.add(14145495);
        this.colorRowRgbs.add(rgbRow10);
        ArrayList<Integer> rgbRow11 = new ArrayList<>();
        rgbRow11.add(4605510);
        rgbRow11.add(8533);
        rgbRow11.add(13674);
        rgbRow11.add(21929);
        rgbRow11.add(5600441);
        rgbRow11.add(8754636);
        rgbRow11.add(5460819);
        rgbRow11.add(13487565);
        this.colorRowRgbs.add(rgbRow11);
        ArrayList<Integer> rgbRow12 = new ArrayList<>();
        rgbRow12.add(4605510);
        rgbRow12.add(786507);
        rgbRow12.add(1840227);
        rgbRow12.add(2896020);
        rgbRow12.add(6315177);
        rgbRow12.add(9077695);
        rgbRow12.add(4605510);
        rgbRow12.add(12829635);
        this.colorRowRgbs.add(rgbRow12);
        ArrayList<Integer> rgbRow13 = new ArrayList<>();
        rgbRow13.add(4605510);
        rgbRow13.add(3145801);
        rgbRow13.add(4525921);
        rgbRow13.add(6630290);
        rgbRow13.add(8806312);
        rgbRow13.add(10520768);
        rgbRow13.add(3552822);
        rgbRow13.add(12040119);
        this.colorRowRgbs.add(rgbRow13);
        ArrayList<Integer> rgbRow14 = new ArrayList<>();
        rgbRow14.add(4605510);
        rgbRow14.add(4915273);
        rgbRow14.add(6489183);
        rgbRow14.add(9578129);
        rgbRow14.add(10838946);
        rgbRow14.add(12291260);
        rgbRow14.add(2434341);
        rgbRow14.add(11382189);
        this.colorRowRgbs.add(rgbRow14);
        ArrayList<Integer> rgbRow15 = new ArrayList<>();
        rgbRow15.add(4605510);
        rgbRow15.add(7929926);
        rgbRow15.add(10485853);
        rgbRow15.add(15597709);
        rgbRow15.add(15888042);
        rgbRow15.add(16162243);
        rgbRow15.add(987664);
        rgbRow15.add(10592672);
        this.colorRowRgbs.add(rgbRow15);
        ArrayList<Integer> rgbRow16 = new ArrayList<>();
        rgbRow16.add(4605510);
        rgbRow16.add(7929895);
        rgbRow16.add(10420280);
        rgbRow16.add(15733595);
        rgbRow16.add(15953018);
        rgbRow16.add(16226462);
        rgbRow16.add(0);
        rgbRow16.add(9868692);
        this.colorRowRgbs.add(rgbRow16);
    }

    public void setTextureSize(int w, int h) {
        if (w > 0 && h > 0) {
            this.canvasData.setTextureSize(w, h);
        }
    }

    public void setInitialData(byte[] initialData) {
        if (initialData != null) {
            this.canvasData.fromCompressedNbt(initialData);
        }
    }

    public void setBlockPos(BlockPos easelPos) {
        this.easelPos = easelPos;
    }

    public boolean blendColorIntoCanvas(int rgb, int pickedX, int pickedY, double blendFactor) {
        if (pickedX >= this.canvasData.textureWidth() || pickedY >= this.canvasData.textureHeight() || pickedX < 0 || pickedY < 0) {
            return false;
        }
        int newRgb = this.canvasData.applyBlend(rgb, this.canvasData.getRgbPixel(pickedX, pickedY), blendFactor);
        this.canvasData.setRgbPixel(pickedX, pickedY, newRgb);
        this.texture.getPixels().setPixelRGBA(pickedX, pickedY, this.canvasData.getRgbOutputPixel(pickedX, pickedY));
        return true;
    }

    public List<HPoint> lineTo(int fromX, int fromY, int toX, int toY) {
        int direction;
        List<HPoint> output = new ArrayList<>();
        output.add(new HPoint(fromX, fromY));
        int newX = fromX;
        int newY = fromY;
        int iterCount = 0;
        int fnX = 1;
        int xDiff = toX - fromX;
        int yDiff = -(toY - fromY);
        boolean inverted = false;
        if (Math.abs(yDiff) > Math.abs(xDiff)) {
            xDiff = yDiff;
            yDiff = xDiff;
            inverted = true;
        }
        int nIterations = Math.abs(xDiff);
        if (xDiff > 0) {
            direction = 1;
        } else {
            direction = -1;
        }
        double angle = Math.atan(yDiff / xDiff);
        if (Math.toDegrees(angle) == 90.0d) {
            return output;
        }
        for (int i = 0; i < nIterations; i++) {
            iterCount++;
            if (iterCount >= 1000) {
                break;
            }
            int yValue = (int) Math.round(Math.tan(angle) * fnX);
            if (!inverted) {
                newX = fromX + (fnX * direction);
                newY = fromY - (yValue * direction);
            }
            if (inverted) {
                newX = fromX + (yValue * direction);
                newY = fromY - (fnX * direction);
            }
            newX = Math.min(Math.max(newX, 0), this.canvasData.textureWidth() - 1);
            newY = Math.min(Math.max(newY, 0), this.canvasData.textureHeight() - 1);
            output.add(new HPoint(newX, newY));
            fnX++;
        }
        return output;
    }

    boolean pointIsValid(HPoint point) {
        int x = point.x;
        int y = point.y;
        if (x < 0 || x >= this.canvasData.textureWidth() || y < 0 || y >= this.canvasData.textureHeight()) {
            return false;
        }
        return true;
    }

    void iterativeFloodFill(int newRgb, int originalRgb, int startX, int startY) {
        Set<HPoint> visited = new HashSet<>(((int) (4097.0f / 0.75f)) + 1, 0.75f);
        Queue<HPoint> toVisit = new LinkedList<>();
        HPoint firstPoint = new HPoint(startX, startY);
        visited.add(firstPoint);
        toVisit.add(firstPoint);
        int visitCount = 0;
        while (!toVisit.isEmpty()) {
            visitCount++;
            HPoint current = toVisit.remove();
            int x = current.x;
            int y = current.y;
            if (pointIsValid(current)) {
                int currentRgb = this.canvasData.getRgbPixel(x, y);
                if (currentRgb == originalRgb) {
                    blendColorIntoCanvas(newRgb, x, y, 1.0d);
                    HPoint right = new HPoint(x + 1, y);
                    if (pointIsValid(right) && !visited.contains(right)) {
                        visited.add(right);
                        toVisit.add(right);
                    }
                    HPoint left = new HPoint(x - 1, y);
                    if (pointIsValid(left) && !visited.contains(left)) {
                        visited.add(left);
                        toVisit.add(left);
                    }
                    HPoint up = new HPoint(x, y - 1);
                    if (pointIsValid(up) && !visited.contains(up)) {
                        visited.add(up);
                        toVisit.add(up);
                    }
                    HPoint down = new HPoint(x, y + 1);
                    if (pointIsValid(down) && !visited.contains(down)) {
                        visited.add(down);
                        toVisit.add(down);
                    }
                }
            }
        }
    }

    public void assignHsvFromRgb() {
        Boolean extendedColorPicker = (Boolean) ModernLifeConfig.CLIENT.extendedEaselColorPicker.get();
        if (extendedColorPicker != null && extendedColorPicker.booleanValue()) {
            double dR = CanvasData.getRValue(this.colorRgb) / 255.0d;
            double dG = CanvasData.getGValue(this.colorRgb) / 255.0d;
            double dB = CanvasData.getBValue(this.colorRgb) / 255.0d;
            boolean rMax = false;
            boolean gMax = false;
            boolean bMax = false;
            if (dR >= dG && dR >= dB) {
                rMax = true;
            } else if (dG >= dR && dG >= dB) {
                gMax = true;
            } else if (dB >= dR && dB >= dG) {
                bMax = true;
            }
            double cMax = Math.max(Math.max(dR, dG), dB);
            double cMin = Math.min(Math.min(dR, dG), dB);
            double delta = cMax - cMin;
            if (delta == 0.0d) {
                this.f6H = 0.0d;
            } else if (rMax) {
                this.f6H = 60.0d * (((dG - dB) / delta) % 6.0d);
            } else if (gMax) {
                this.f6H = 60.0d * (((dB - dR) / delta) + 2.0d);
            } else if (bMax) {
                this.f6H = 60.0d * (((dR - dG) / delta) + 4.0d);
            }
            this.f6H %= 360.0d;
            if (this.f6H < 0.0d) {
                this.f6H += 360.0d;
            }
            ModernLifeCommon.LOGGER.debug("H = " + this.f6H);
            if (cMax == 0.0d) {
                this.f7S = 0.0d;
            } else {
                this.f7S = delta / cMax;
            }
            this.f8V = cMax;
            return;
        }
        this.f6H = -1.0d;
    }

    public void assignRgbFromHsv(int mouseButton) {
        double dR;
        double dG;
        double dB;
        double C = this.f8V * this.f7S;
        double X = C * (1.0d - Math.abs(((this.f6H / 60.0d) % 2.0d) - 1.0d));
        double m = this.f8V - C;
        if (this.f6H >= 0.0d && this.f6H < 60.0d) {
            dR = C;
            dG = X;
            dB = 0.0d;
        } else if (this.f6H < 120.0d) {
            dR = X;
            dG = C;
            dB = 0.0d;
        } else if (this.f6H < 180.0d) {
            dR = 0.0d;
            dG = C;
            dB = X;
        } else if (this.f6H < 240.0d) {
            dR = 0.0d;
            dG = X;
            dB = C;
        } else if (this.f6H < 300.0d) {
            dR = X;
            dG = 0.0d;
            dB = C;
        } else {
            dR = C;
            dG = 0.0d;
            dB = X;
        }
        int r = (int) ((dR + m) * 255.0d);
        int g = (int) ((dG + m) * 255.0d);
        int b = (int) ((dB + m) * 255.0d);
        if (mouseButton == 0) {
            this.colorRgbLeft = CanvasData.toRgb(r, g, b);
        } else if (mouseButton == 1) {
            this.colorRgbRight = CanvasData.toRgb(r, g, b);
        }
    }

    public void handleMouseEvent(double clickX, double clickY, boolean mouseDown, int mouseButton) {
        List<Integer> rgbRow;
        Integer rgb;
        if (this.texture == null || !this.canvasData.isValid()) {
            return;
        }
        long timestamp = System.currentTimeMillis();
        long timeDiff = timestamp - this.introTimestamp;
        if (timeDiff <= 350) {
            return;
        }
        int uiStartX = (this.width - 256) / 2;
        int uiStartY = (this.height - 216) / 2;
        if (clickX > uiStartX + 7.0d && clickX < uiStartX + 62.0d && clickY > uiStartY + 21.0d && clickY < uiStartY + 127.0d) {
            double cellWidth = 53 / 8;
            double cellHeight = 105 / 16;
            double yOffsetInPicker = clickY - (uiStartY + 21.0d);
            double xOffsetInPicker = clickX - (uiStartX + 7.0d);
            int pickedColumn = (int) (yOffsetInPicker / cellHeight);
            int pickedRow = (int) (xOffsetInPicker / cellWidth);
            if (pickedColumn < this.colorRowRgbs.size() && (rgbRow = this.colorRowRgbs.get(pickedColumn)) != null && pickedRow < rgbRow.size() && (rgb = rgbRow.get(pickedRow)) != null) {
                if (mouseButton == 0) {
                    this.colorRgbLeft = rgb.intValue();
                } else if (mouseButton == 1) {
                    this.colorRgbRight = rgb.intValue();
                }
                assignHsvFromRgb();
            }
        }
        if (mouseButton == 0) {
            this.colorRgb = this.colorRgbLeft;
        } else if (mouseButton == 1) {
            this.colorRgb = this.colorRgbRight;
        }
        Boolean extendedColorPicker = (Boolean) ModernLifeConfig.CLIENT.extendedEaselColorPicker.get();
        if (extendedColorPicker != null && extendedColorPicker.booleanValue() && clickX > xStartColorPicker() - 2 && clickX < xStartColorPicker() + colorPickerWidth() + 2 && clickY > yStartColorPicker() - 2 && clickY < yStartColorPicker() + colorPickerHeight() + 2) {
            double pickerX = clickX - xStartColorPicker();
            double pickerY = clickY - yStartColorPicker();
            double xPercent = pickerX / colorPickerWidth();
            double yPercent = pickerY / colorPickerHeight();
            if (xPercent < 0.0d) {
                xPercent = 0.0d;
            } else if (xPercent > 1.0d) {
                xPercent = 1.0d;
            }
            if (yPercent < 0.0d) {
                yPercent = 0.0d;
            }
            if (yPercent > 1.0d) {
                yPercent = 1.0d;
            }
            this.f6H = xPercent * 360.0d;
            this.f7S = 1.0d - yPercent;
            assignRgbFromHsv(mouseButton);
        }
        if (clickX > uiStartX + 94.0d && clickX < uiStartX + 223.0d && clickY > uiStartY + 25.0d && clickY < uiStartY + 154.0d) {
            int canvasWidth = canvasAspectedWidth();
            int canvasHeight = canvasAspectedHeight();
            int canvasMaxX = this.canvasData.textureWidth();
            int canvaxMaxY = this.canvasData.textureHeight();
            double cellWidth2 = canvasWidth / canvasMaxX;
            double cellHeight2 = canvasHeight / canvaxMaxY;
            double yOffsetInCanvas = clickY - (uiStartY + 25.0d);
            double xOffsetInCanvas = clickX - (uiStartX + 94.0d);
            int pickedY = (int) (yOffsetInCanvas / cellHeight2);
            int pickedX = (int) (xOffsetInCanvas / cellWidth2);
            if (pickedX < this.canvasData.textureWidth() && pickedY < this.canvasData.textureHeight() && (!mouseDown || pickedX != this.lastX || pickedY != this.lastY)) {
                if (!this.dragStart && m_7282_()) {
                    if (this.undoStack.size() >= 10) {
                        this.undoStack.removeLast();
                    }
                    CanvasData undoData = new CanvasData();
                    undoData.copyDataFrom(this.canvasData);
                    this.undoStack.push(undoData);
                    this.dragStart = true;
                    this.dragStartX = pickedX;
                    this.dragStartY = pickedY;
                }
                if (pencil.m_93696_()) {
                    blendColorIntoCanvas(this.colorRgb, pickedX, pickedY, 1.0d);
                    this.texture.upload();
                } else if (paintbrush.m_93696_() && mouseDown) {
                    blendColorIntoCanvas(this.colorRgb, pickedX, pickedY, 0.8d);
                    blendColorIntoCanvas(this.colorRgb, pickedX + 1, pickedY, 0.3125d);
                    blendColorIntoCanvas(this.colorRgb, pickedX - 1, pickedY, 0.3125d);
                    blendColorIntoCanvas(this.colorRgb, pickedX, pickedY + 1, 0.3125d);
                    blendColorIntoCanvas(this.colorRgb, pickedX, pickedY - 1, 0.3125d);
                    blendColorIntoCanvas(this.colorRgb, pickedX + 1, pickedY + 1, 0.04d);
                    blendColorIntoCanvas(this.colorRgb, pickedX - 1, pickedY + 1, 0.04d);
                    blendColorIntoCanvas(this.colorRgb, pickedX - 1, pickedY - 1, 0.04d);
                    blendColorIntoCanvas(this.colorRgb, pickedX + 1, pickedY - 1, 0.04d);
                    this.texture.upload();
                } else if (line.m_93696_()) {
                    ModernLifeCommon.LOGGER.debug("line focus");
                    if (this.dragStart || !m_7282_()) {
                        if (this.dragStart && m_7282_()) {
                            updateTexture(this.canvasData, this.texture);
                            List<HPoint> linePixels = lineTo(this.dragStartX, this.dragStartY, pickedX, pickedY);
                            for (int i = 0; i < linePixels.size(); i++) {
                                HPoint HPoint = linePixels.get(i);
                                this.texture.getPixels().setPixelRGBA(HPoint.x, HPoint.y, this.canvasData.convertToRgbOutput(this.colorRgb));
                            }
                            this.texture.upload();
                        } else if (this.dragStart && !m_7282_()) {
                            List<HPoint> linePixels2 = lineTo(this.dragStartX, this.dragStartY, pickedX, pickedY);
                            for (int i2 = 0; i2 < linePixels2.size(); i2++) {
                                HPoint HPoint2 = linePixels2.get(i2);
                                blendColorIntoCanvas(this.colorRgb, HPoint2.x, HPoint2.y, 1.0d);
                            }
                            this.texture.upload();
                        }
                    }
                } else if (bucket.m_93696_()) {
                    iterativeFloodFill(this.colorRgb, this.canvasData.getRgbPixel(pickedX, pickedY), pickedX, pickedY);
                    this.texture.upload();
                } else if (colorPicker.m_93696_() && mouseDown) {
                    if (mouseButton == 0) {
                        this.colorRgbLeft = this.canvasData.getRgbPixel(pickedX, pickedY);
                    } else if (mouseButton == 1) {
                        this.colorRgbRight = this.canvasData.getRgbPixel(pickedX, pickedY);
                    }
                    assignHsvFromRgb();
                }
                if (this.dragStart && !m_7282_()) {
                    ModernLifeCommon.LOGGER.debug("drag end");
                    this.dragStart = false;
                    this.dragStartX = -1;
                    this.dragStartY = -1;
                }
            }
            if (mouseDown) {
                this.lastX = pickedX;
                this.lastY = pickedY;
            } else {
                this.lastX = -1;
                this.lastY = -1;
            }
        }
        this.lastMouseButton = mouseButton;
    }

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

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

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

    public static void unfocusAll() {
        if (pencil != null && pencil.m_93696_()) {
            pencil.changeFocus(false);
        }
        if (paintbrush != null && paintbrush.m_93696_()) {
            paintbrush.changeFocus(false);
        }
        if (line != null && line.m_93696_()) {
            line.changeFocus(false);
        }
        if (bucket != null && bucket.m_93696_()) {
            bucket.changeFocus(false);
        }
        if (colorPicker != null && colorPicker.m_93696_()) {
            colorPicker.changeFocus(false);
        }
    }

    public boolean keyPressed(int key, int p_231046_2_, int p_231046_3_) {
        if (key == 80) {
            pencil.onPress();
            return false;
        }
        if (key == 66) {
            paintbrush.onPress();
            return false;
        }
        if (key == 76) {
            line.onPress();
            return false;
        }
        if (key == 70) {
            bucket.onPress();
            return false;
        }
        if (key == 67) {
            colorPicker.onPress();
            return false;
        }
        if (key == 90 && Screen.hasControlDown()) {
            undo.onPress();
            return false;
        }
        return super.keyPressed(key, p_231046_2_, p_231046_3_);
    }

    protected void init() {
        int startX = ((this.width - 256) / 2) + 77;
        int startY = ((this.height - 216) / 2) + 164;
        pencil = addRenderableWidget(new ImageButton(startX, startY, 20, 18, 0, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.1
            C00761() {
            }

            public void onPress(Button button) {
                EaselScreen.unfocusAll();
                button.changeFocus(true);
            }
        }));
        paintbrush = addRenderableWidget(new ImageButton(startX + 22, startY, 20, 18, 21, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.2
            C00772() {
            }

            public void onPress(Button button) {
                EaselScreen.unfocusAll();
                button.changeFocus(true);
            }
        }));
        line = addRenderableWidget(new ImageButton(startX + 44, startY, 20, 18, 42, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.3
            C00783() {
            }

            public void onPress(Button button) {
                EaselScreen.unfocusAll();
                button.changeFocus(true);
            }
        }));
        bucket = addRenderableWidget(new ImageButton(startX + 66, startY, 20, 18, 63, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.4
            C00794() {
            }

            public void onPress(Button button) {
                EaselScreen.unfocusAll();
                button.changeFocus(true);
            }
        }));
        colorPicker = addRenderableWidget(new ImageButton(startX + 88, startY, 20, 18, 84, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.5
            C00805() {
            }

            public void onPress(Button button) {
                EaselScreen.unfocusAll();
                button.changeFocus(true);
            }
        }));
        undo = addRenderableWidget(new ImageButton(startX + 110, startY, 20, 18, 105, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.6
            C00816() {
            }

            public void onPress(Button button) {
                if (!EaselScreen.this.undoStack.isEmpty()) {
                    CanvasData undoData = EaselScreen.this.undoStack.pop();
                    if (undoData.isValid()) {
                        EaselScreen.this.canvasData.copyDataFrom(undoData);
                        EaselScreen.updateTexture(EaselScreen.this.canvasData, EaselScreen.this.texture);
                    }
                }
            }
        }));
        cancel = addRenderableWidget(new ImageButton(startX + 132, startY, 20, 18, 126, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.7
            C00827() {
            }

            public void onPress(Button button) {
                Minecraft.getInstance().setScreen((Screen) null);
            }
        }));
        accept = addRenderableWidget(new ImageButton(startX + 154, startY, 20, 18, 147, 219, 19, CANVAS_UI, new Button.OnPress() { // from class: com.dairymoose.modernlife.blocks.gui.EaselScreen.8
            C00838() {
            }

            public void onPress(Button button) {
                if (EaselScreen.this.easelPos != null) {
                    byte[] compressed = EaselScreen.this.canvasData.toCompressedNbt();
                    List<byte[]> parts = new ArrayList<>();
                    for (int i = 0; i < Math.ceil(compressed.length / 30000); i++) {
                        ByteArrayOutputStream stream = new ByteArrayOutputStream();
                        int startIndex = i * 30000;
                        int bytesToWrite = Math.min(compressed.length - startIndex, 30000);
                        stream.write(compressed, startIndex, bytesToWrite);
                        byte[] part = stream.toByteArray();
                        parts.add(part);
                    }
                    for (int i2 = 0; i2 < parts.size(); i2++) {
                        int seqNo = i2 + 1;
                        boolean last = false;
                        if (seqNo == parts.size()) {
                            last = true;
                        }
                        ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundMultipartEaselPacket(seqNo, last, parts.get(i2), EaselScreen.this.canvasData.textureWidth(), EaselScreen.this.canvasData.textureHeight(), EaselScreen.this.easelPos));
                    }
                }
                Minecraft.getInstance().setScreen((Screen) null);
            }
        }));
        pencil.changeFocus(true);
    }

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

        public void onPress(Button button) {
            EaselScreen.unfocusAll();
            button.changeFocus(true);
        }
    }

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

        public void onPress(Button button) {
            EaselScreen.unfocusAll();
            button.changeFocus(true);
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$3 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$3.class */
    class C00783 implements Button.OnPress {
        C00783() {
        }

        public void onPress(Button button) {
            EaselScreen.unfocusAll();
            button.changeFocus(true);
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$4 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$4.class */
    class C00794 implements Button.OnPress {
        C00794() {
        }

        public void onPress(Button button) {
            EaselScreen.unfocusAll();
            button.changeFocus(true);
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$5 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$5.class */
    class C00805 implements Button.OnPress {
        C00805() {
        }

        public void onPress(Button button) {
            EaselScreen.unfocusAll();
            button.changeFocus(true);
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$6 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$6.class */
    class C00816 implements Button.OnPress {
        C00816() {
        }

        public void onPress(Button button) {
            if (!EaselScreen.this.undoStack.isEmpty()) {
                CanvasData undoData = EaselScreen.this.undoStack.pop();
                if (undoData.isValid()) {
                    EaselScreen.this.canvasData.copyDataFrom(undoData);
                    EaselScreen.updateTexture(EaselScreen.this.canvasData, EaselScreen.this.texture);
                }
            }
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$7 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$7.class */
    class C00827 implements Button.OnPress {
        C00827() {
        }

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

    /* renamed from: com.dairymoose.modernlife.blocks.gui.EaselScreen$8 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/EaselScreen$8.class */
    class C00838 implements Button.OnPress {
        C00838() {
        }

        public void onPress(Button button) {
            if (EaselScreen.this.easelPos != null) {
                byte[] compressed = EaselScreen.this.canvasData.toCompressedNbt();
                List<byte[]> parts = new ArrayList<>();
                for (int i = 0; i < Math.ceil(compressed.length / 30000); i++) {
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    int startIndex = i * 30000;
                    int bytesToWrite = Math.min(compressed.length - startIndex, 30000);
                    stream.write(compressed, startIndex, bytesToWrite);
                    byte[] part = stream.toByteArray();
                    parts.add(part);
                }
                for (int i2 = 0; i2 < parts.size(); i2++) {
                    int seqNo = i2 + 1;
                    boolean last = false;
                    if (seqNo == parts.size()) {
                        last = true;
                    }
                    ModernLifeNetwork.INSTANCE.sendToServer(new ServerboundMultipartEaselPacket(seqNo, last, parts.get(i2), EaselScreen.this.canvasData.textureWidth(), EaselScreen.this.canvasData.textureHeight(), EaselScreen.this.easelPos));
                }
            }
            Minecraft.getInstance().setScreen((Screen) null);
        }
    }

    public static void updateTexture(CanvasData canvasData, DynamicTexture texture) {
        if (!canvasData.isValid()) {
            return;
        }
        for (int i = 0; i < canvasData.textureHeight(); i++) {
            for (int j = 0; j < canvasData.textureWidth(); j++) {
                if (texture != null && texture.getPixels() != null && canvasData.isValid()) {
                    try {
                        texture.getPixels().setPixelRGBA(j, i, canvasData.getRgbOutputPixel(j, i));
                    } catch (IllegalStateException e) {
                        ModernLifeCommon.LOGGER.warn("Unallocated image error");
                    }
                }
            }
        }
        if (texture != null) {
            try {
                if (canvasData.isValid()) {
                    texture.upload();
                }
            } catch (IllegalStateException e2) {
                ModernLifeCommon.LOGGER.warn("Unallocated image error");
            }
        }
    }

    public static void updateTextureDownsampled(CanvasData canvasData, DynamicTexture texture, int newSize) {
        if (!canvasData.isValid()) {
            return;
        }
        int ratio = canvasData.textureWidth() / newSize;
        for (int i = 0; i < newSize; i++) {
            for (int j = 0; j < newSize; j++) {
                int sampleX = j * ratio;
                int sampleY = i * ratio;
                texture.getPixels().setPixelRGBA(j, i, canvasData.getRgbOutputPixel(sampleX, sampleY));
            }
        }
        texture.upload();
    }

    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_);
    }

    public boolean m_6050_(double a, double b, double c) {
        Boolean extendedColorPicker = (Boolean) ModernLifeConfig.CLIENT.extendedEaselColorPicker.get();
        if (extendedColorPicker != null && extendedColorPicker.booleanValue()) {
            if (c > 0.0d) {
                if (Screen.hasShiftDown()) {
                    this.f8V += 0.01d;
                } else {
                    this.f8V += 0.1d;
                }
            } else if (Screen.hasShiftDown()) {
                this.f8V -= 0.01d;
            } else {
                this.f8V -= 0.1d;
            }
            if (this.f8V > 1.0d) {
                this.f8V = 1.0d;
            }
            if (this.f8V < 0.0d) {
                this.f8V = 0.0d;
            }
            assignRgbFromHsv(this.lastMouseButton);
        }
        return super.m_6050_(a, b, c);
    }

    public void render(PoseStack matrixStack, int p_230430_2_, int p_230430_3_, float p_230430_4_) {
        renderBackground(matrixStack);
        if (this.firstRender) {
            this.firstRender = false;
            this.canvasData.initImage();
            if (this.texture == null) {
                this.texture = new DynamicTexture(this.canvasData.textureWidth(), this.canvasData.textureHeight(), true);
                this.dynamicTextureResource = this.textureManager.register("artpad/dyn", this.texture);
                updateTexture(this.canvasData, this.texture);
            }
        }
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.setShaderTexture(0, CANVAS_UI);
        int xStart = (this.width - 256) / 2;
        int yStart = (this.height - 216) / 2;
        m_93228_(matrixStack, xStart, yStart, 0, 0, 256, 216);
        int r = CanvasData.getRValue(this.colorRgbLeft);
        int g = CanvasData.getGValue(this.colorRgbLeft);
        int b = CanvasData.getBValue(this.colorRgbLeft);
        float fR = r / 255.0f;
        float fG = g / 255.0f;
        float fB = b / 255.0f;
        RenderSystem.setShaderColor(fR, fG, fB, 1.0f);
        m_93228_(matrixStack, xStart + 39, yStart + 135, 39, 135, 9, 9);
        int r2 = CanvasData.getRValue(this.colorRgbRight);
        int g2 = CanvasData.getGValue(this.colorRgbRight);
        int b2 = CanvasData.getBValue(this.colorRgbRight);
        float fR2 = r2 / 255.0f;
        float fG2 = g2 / 255.0f;
        float fB2 = b2 / 255.0f;
        RenderSystem.setShaderColor(fR2, fG2, fB2, 1.0f);
        m_93228_(matrixStack, xStart + 52, yStart + 135, 52, 135, 9, 9);
        if (this.dynamicTextureResource != null) {
            int canvasWidth = canvasAspectedWidth();
            int canvasHeight = canvasAspectedHeight();
            if (canvasWidth != canvasHeight) {
                GuiComponent.fill(matrixStack, xStart + 94, yStart + 25, xStart + 94 + 128, yStart + 25 + 128, -16777216);
            }
            RenderSystem.setShaderTexture(0, this.dynamicTextureResource);
            RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
            GuiComponent.blit(matrixStack, xStart + 94, yStart + 25, 0.0f, 0.0f, canvasWidth, canvasHeight, canvasWidth, canvasHeight);
        }
        drawCenteredStringNoShadow(matrixStack, this.minecraft.font, this.title, xStart + 128, yStart + 2, 0);
        for (ImageButton imageButton : children()) {
            if (imageButton instanceof AbstractWidget) {
                ImageButton imageButton2 = (AbstractWidget) imageButton;
                if (imageButton2.isHoveredOrFocused() && !imageButton2.isFocused()) {
                    String text = "";
                    if (imageButton2 == pencil) {
                        text = "Pencil (P)";
                    } else if (imageButton2 == paintbrush) {
                        text = "Paintbrush (B)";
                    } else if (imageButton2 == line) {
                        text = "Line (L)";
                    } else if (imageButton2 == bucket) {
                        text = "Bucket (F)";
                    } else if (imageButton2 == colorPicker) {
                        text = "Color Picker (C)";
                    } else if (imageButton2 == undo) {
                        text = "Undo (Ctrl+Z)";
                    } else if (imageButton2 == cancel) {
                        text = "Cancel";
                    } else if (imageButton2 == accept) {
                        text = "Accept";
                    }
                    if (!text.isEmpty()) {
                        renderTooltip(matrixStack, new TextComponent(text), ((AbstractWidget) imageButton2).x, ((AbstractWidget) imageButton2).y);
                    }
                }
            }
        }
        Boolean extendedColorPicker = (Boolean) ModernLifeConfig.CLIENT.extendedEaselColorPicker.get();
        if (extendedColorPicker != null && extendedColorPicker.booleanValue()) {
            RenderSystem.setShaderTexture(0, COLOR_PICKER_UI);
            int COLOR_PICKER_WIDTH = colorPickerWidth();
            int COLOR_PICKER_HEIGHT = colorPickerHeight();
            int xStartColorPicker = xStartColorPicker();
            int yStartColorPicker = yStartColorPicker();
            GuiComponent.blit(matrixStack, xStartColorPicker, yStartColorPicker, 0.0f, 0.0f, COLOR_PICKER_WIDTH, COLOR_PICKER_HEIGHT, COLOR_PICKER_WIDTH, COLOR_PICKER_HEIGHT);
            String rgbText = "R" + r + " G" + g + " B" + b;
            drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(rgbText), xStart + 128, (int) (yStart + 205.2d), 0);
            if (this.f6H >= 0.0d) {
                double sText = this.f7S * 100.0d;
                double vText = this.f8V * 100.0d;
                String hsvText = String.format("H%.1f S%.1f V%.0f", Double.valueOf(this.f6H), Double.valueOf(sText), Double.valueOf(vText));
                drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent(hsvText), xStart + 128, (int) (yStart + 194.4d), 0);
                drawCenteredStringNoShadow(matrixStack, this.minecraft.font, new TextComponent("(scroll wheel adjusts V)"), xStart + 128, (int) (yStart + 183.6d), 0);
            }
        }
        super.render(matrixStack, p_230430_2_, p_230430_3_, p_230430_4_);
    }

    public int xStartColorPicker() {
        return ((this.width - 256) / 2) + 5;
    }

    public int yStartColorPicker() {
        return ((this.height - 216) / 2) + 147;
    }

    public float getAspectRatio() {
        return this.canvasData.textureWidth() / this.canvasData.textureHeight();
    }

    public int canvasAspectedWidth() {
        if (this.canvasData.textureWidth() < this.canvasData.textureHeight()) {
            return (int) (128.0f * getAspectRatio());
        }
        return 128;
    }

    public int canvasAspectedHeight() {
        if (this.canvasData.textureHeight() < this.canvasData.textureWidth()) {
            return (int) (128.0f * (1.0f / getAspectRatio()));
        }
        return 128;
    }

    public int colorPickerWidth() {
        return 60;
    }

    public int colorPickerHeight() {
        return 60;
    }
}
