package aeronicamc.mods.mxtune.gui;

import aeronicamc.mods.mxtune.Reference;
import aeronicamc.mods.mxtune.inventory.InstrumentContainer;
import aeronicamc.mods.mxtune.util.SoundFontProxyManager;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

import javax.annotation.Nullable;
import java.util.Objects;

public class InstrumentScreen extends ContainerScreen<InstrumentContainer>
{
    private final ResourceLocation GUI = new ResourceLocation(Reference.MOD_ID, "textures/gui/multi_inst_inventory.png");
    Button buttonChangeInstrument;
    int instIndex;

    public InstrumentScreen(InstrumentContainer screenContainer, PlayerInventory inv, ITextComponent titleIn)
    {
        super(screenContainer, inv, titleIn);
        minecraft = Minecraft.getInstance();
        assert minecraft.player != null;
        this.imageWidth = 166;
        this.imageWidth = 184;
    }

    @Override
    protected void init()
    {
        super.init();

        int yPos = topPos + 6;
        int xPos = leftPos + 12;
        assert minecraft != null;
        assert minecraft.player != null;

        buttonChangeInstrument = new Button(xPos, yPos, imageWidth - 24, 20, minecraft.player.getMainHandItem().getHoverName(), (open) ->
                minecraft.setScreen(new GuiMultiInstChooser(this)));
        this.addButton(buttonChangeInstrument);
        buttonChangeInstrument.setMessage(new TranslationTextComponent(SoundFontProxyManager.getLangKeyName(minecraft.player.getMainHandItem().getMaxDamage())));
    }

    @Override
    public void tick()
    {
        int newIndex;
        super.tick();
        if (mcPlayerOk(minecraft) && instIndex != (newIndex = Objects.requireNonNull(minecraft.player).getMainHandItem().getMaxDamage()))
        {
            instIndex = newIndex;
            updateInstrumentName();
        }
    }

    private boolean mcPlayerOk(@Nullable Minecraft minecraft) { return minecraft != null && minecraft.player != null; }

    private void updateInstrumentName()
    {
        if (mcPlayerOk(minecraft))
            buttonChangeInstrument.setMessage(new TranslationTextComponent(SoundFontProxyManager.getLangKeyName(Objects.requireNonNull(minecraft.player).getMainHandItem().getMaxDamage())));
    }

    @Override
    public void removed()
    {
        Objects.requireNonNull(this.minecraft).keyboardHandler.setSendRepeatsToGui(false);
        super.removed();
    }

    @Override
    protected void renderBg(MatrixStack matrixStack, float partialTicks, int x, int y)
    {
        RenderSystem.clearColor(1.0F, 1.0F, 1.0F, 1.0F);
        Objects.requireNonNull(this.minecraft).getTextureManager().bind(GUI);
        int relX = (this.width - this.imageWidth) / 2;
        int relY = (this.height - this.imageHeight) / 2;
        this.blit(matrixStack, relX, relY, 0, 0, this.imageWidth, this.imageHeight);
    }

    @Override
    public void render(MatrixStack matrixStack , int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderTooltip(matrixStack, mouseX, mouseY);
    }

    @Override
    protected void renderLabels(MatrixStack matrixStack , int mouseX, int mouseY) {
        //this.font.draw(matrixStack, menu.getName().getContents(), 10, 8, TextColorFg.DARK_GRAY);
        this.font.draw(matrixStack, this.title, 10, 72, TextColorFg.DARK_GRAY);
    }
}

@@ -128,24 +128,77 @@
 tContainer;%0A
+import aeronicamc.mods.mxtune.util.SheetMusicHelper;%0A
 import aeron
@@ -548,16 +548,53 @@
 entory;%0A
+import net.minecraft.item.ItemStack;%0A
 import n
@@ -1047,16 +1047,42 @@
 stIndex;
+%0A    String musicKey = %22%22;
 %0A%0A    pu
@@ -1682,40 +1682,29 @@
 20, 
-m
 in
-ecraft.player.getMainHandItem
+ventory.getSelected
 ().g
@@ -1957,40 +1957,29 @@
 ame(
-m
 in
-ecraft.player.getMainHandItem
+ventory.getSelected
 ().g
@@ -2080,140 +2080,108 @@
     
-super.tick();%0A        if (mcPlayerOk(minecraft) && instIndex != (newIndex = Objects.requireNonNull(minecraft.player).getMainHandItem
+String newMusicKey;%0A        super.tick();%0A        if (instIndex != (newIndex = inventory.getSelected
 ().g
@@ -2286,16 +2286,340 @@
       %7D%0A
+        ItemStack sheetMusic = SheetMusicHelper.getIMusicFromIInstrument(inventory.getSelected());%0A        if (SheetMusicHelper.hasMusicText(sheetMusic) && !(newMusicKey = SheetMusicHelper.getMusicTextKey(sheetMusic)).equals(musicKey))%0A        %7B%0A            musicKey = newMusicKey;%0A            updateSheetMusic();%0A        %7D%0A
     %7D%0A%0A 
@@ -2935,64 +2935,29 @@
 ame(
-Objects.requireNonNull(minecraft.player).getMainHandItem
+inventory.getSelected
 ().g
@@ -2973,32 +2973,83 @@
 ge())));%0A    %7D%0A%0A
+    private void updateSheetMusic()%0A    %7B%0A%0A    %7D%0A%0A%0A
     @Override%0A  
@@ -3865,16 +3865,246 @@
 Ticks);%0A
+        ModGuiHelper.RenderGuiItemScaled(this.itemRenderer,%0A            Objects.requireNonNull(Objects.requireNonNull(minecraft).player).getMainHandItem().getStack(),%0A                getGuiLeft() + 51, getGuiTop() + 50, 2, true);%0A
         
@@ -4261,107 +4261,8 @@
 ) %7B%0A
-        //this.font.draw(matrixStack, menu.getName().getContents(), 10, 8, TextColorFg.DARK_GRAY);%0A
     

Add a scaled gui item icon to the instrument screen.