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.items.GuitarItem;
import com.dairymoose.modernlife.network.play.client.ServerboundGuitarPacket;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.Font;
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.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@OnlyIn(Dist.CLIENT)
/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/gui/GuitarScreen.class */
public class GuitarScreen extends Screen {
    private static final Logger LOGGER = LogManager.getLogger();
    private static final ResourceLocation GUITAR_GUI = new ResourceLocation("modernlife", "textures/gui/mc_guitar_keys_full.png");
    final int GUI_WIDTH = 256;
    final int GUI_HEIGHT = 134;
    private Level world;
    private Player player;
    GuitarItem.Tuning string;
    int lastPlayedFret;
    int counter;
    char[] fretKeys;
    char[] stringKeys;

    public GuitarScreen(Level world, Player player) {
        super(new TextComponent("Guitar").m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(4210752))));
        this.GUI_WIDTH = 256;
        this.GUI_HEIGHT = 134;
        this.string = GuitarItem.Tuning.STRING_E;
        this.lastPlayedFret = -1;
        this.counter = 1;
        this.fretKeys = new char[]{'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N'};
        this.stringKeys = new char[]{'1', '2', '3', '4', '5', '6'};
        this.world = world;
        this.player = player;
    }

    protected void init() {
        ModernLifeCommon.LOGGER.debug("in init!");
        int i = (this.width - 256) / 2;
        int i2 = (this.height - 134) / 2;
    }

    public boolean isPauseScreen() {
        return false;
    }

    private boolean areaContainsAmplifier(BlockPos pos) {
        BlockState state = this.world.getBlockState(pos);
        if (state.is(CustomBlocks.BLOCK_GUITAR_AMPLIFIER.get())) {
            return true;
        }
        BlockState state2 = this.world.getBlockState(pos.above());
        if (state2.is(CustomBlocks.BLOCK_GUITAR_AMPLIFIER.get())) {
            return true;
        }
        BlockState state3 = this.world.getBlockState(pos.below());
        if (state3.is(CustomBlocks.BLOCK_GUITAR_AMPLIFIER.get())) {
            return true;
        }
        return false;
    }

    public boolean isNearAmplifier(Level world, BlockPos pos) {
        if (world == null || pos == null) {
            return false;
        }
        for (int x = -6; x < 6; x++) {
            for (int z = -6; z < 6; z++) {
                BlockPos searchPos = pos.east(x);
                if (areaContainsAmplifier(searchPos.south(z))) {
                    return true;
                }
            }
        }
        return false;
    }

    public void applyGuitarStringChange(int stringNo) {
        if (stringNo == 1) {
            this.string = GuitarItem.Tuning.STRING_E;
            return;
        }
        if (stringNo == 2) {
            this.string = GuitarItem.Tuning.STRING_A;
            return;
        }
        if (stringNo == 3) {
            this.string = GuitarItem.Tuning.STRING_D;
            return;
        }
        if (stringNo == 4) {
            this.string = GuitarItem.Tuning.STRING_G;
        } else if (stringNo == 5) {
            this.string = GuitarItem.Tuning.STRING_B;
        } else if (stringNo == 6) {
            this.string = GuitarItem.Tuning.STRING_HI_E;
        }
    }

    public int getGuitarStringNo() {
        if (this.string == GuitarItem.Tuning.STRING_E) {
            return 1;
        }
        if (this.string == GuitarItem.Tuning.STRING_A) {
            return 2;
        }
        if (this.string == GuitarItem.Tuning.STRING_D) {
            return 3;
        }
        if (this.string == GuitarItem.Tuning.STRING_G) {
            return 4;
        }
        if (this.string == GuitarItem.Tuning.STRING_B) {
            return 5;
        }
        if (this.string == GuitarItem.Tuning.STRING_HI_E) {
            return 6;
        }
        return 1;
    }

    public boolean m_6050_(double a, double b, double c) {
        int currentStringNo = getGuitarStringNo();
        if (c > 0.0d) {
            applyGuitarStringChange(currentStringNo - 1);
        } else {
            applyGuitarStringChange(currentStringNo + 1);
        }
        return super.m_6050_(a, b, c);
    }

    public boolean keyPressed(int key, int p_231046_2_, int p_231046_3_) {
        ModernLifeCommon.LOGGER.debug("key press: " + key);
        int keyIndex = ArrayUtils.indexOf(this.stringKeys, (char) key);
        if (keyIndex != -1) {
            int stringNo = keyIndex + 1;
            applyGuitarStringChange(stringNo);
        }
        int keyIndex2 = ArrayUtils.indexOf(this.fretKeys, (char) key);
        boolean distorted = isNearAmplifier(this.world, this.player.blockPosition());
        if (keyIndex2 != -1) {
            int fret = keyIndex2 + 1;
            if (this.player != null && this.world != null) {
                ServerboundGuitarPacket.playGuitarSound(this.world, this.player, this.player.blockPosition(), ServerboundGuitarPacket.getSoundEvent(this.string, distorted), distorted, fret);
            }
            ServerboundGuitarPacket packet = new ServerboundGuitarPacket(this.string, fret, distorted);
            ModernLifeNetwork.INSTANCE.sendToServer(packet);
            this.lastPlayedFret = fret - 1;
        }
        return super.keyPressed(key, p_231046_2_, p_231046_3_);
    }

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

    private String numberSuffix(int fret) {
        int lastDigit = fret % 10;
        if (fret == 11 || fret == 12 || fret == 13) {
            return "th";
        }
        if (lastDigit == 1) {
            return "st";
        }
        if (lastDigit == 2) {
            return "nd";
        }
        if (lastDigit == 3) {
            return "rd";
        }
        return "th";
    }

    public void render(PoseStack p_230430_1_, int p_230430_2_, int p_230430_3_, float p_230430_4_) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderTexture(0, GUITAR_GUI);
        int lvt_5_1_ = (this.width - 256) / 2;
        int lvt_6_1_ = (this.height - 134) / 2;
        m_93228_(p_230430_1_, lvt_5_1_, lvt_6_1_, 0, 0, 256, 134);
        String stringText = "";
        if (this.string == GuitarItem.Tuning.STRING_E) {
            stringText = "E String";
        } else if (this.string == GuitarItem.Tuning.STRING_A) {
            stringText = "A String";
        } else if (this.string == GuitarItem.Tuning.STRING_D) {
            stringText = "D String";
        } else if (this.string == GuitarItem.Tuning.STRING_G) {
            stringText = "G String";
        } else if (this.string == GuitarItem.Tuning.STRING_B) {
            stringText = "B String";
        } else if (this.string == GuitarItem.Tuning.STRING_HI_E) {
            stringText = "High E String";
        }
        drawCenteredStringNoShadow(p_230430_1_, this.minecraft.font, new TextComponent(stringText).m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(16777215))), lvt_5_1_ + 128, lvt_6_1_ + 53, 0);
        if (this.lastPlayedFret != -1) {
            String fretText = this.lastPlayedFret + numberSuffix(this.lastPlayedFret) + " fret";
            drawCenteredStringNoShadow(p_230430_1_, this.minecraft.font, new TextComponent(fretText).m_6270_(Style.EMPTY.withColor(TextColor.fromRgb(16777215))), lvt_5_1_ + 128, lvt_6_1_ + 140, 0);
        }
        super.render(p_230430_1_, p_230430_2_, p_230430_3_, p_230430_4_);
    }
}
