package me.km.networking;

import com.mojang.blaze3d.systems.RenderSystem;
import me.km.KajetansMod;
import me.km.utils.ClientReflectionUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class ItemStackDisplayGui extends AbstractGui {
    private static final ResourceLocation CUSTOM_ICONS = new ResourceLocation(KajetansMod.MODID, "textures/gui/itemstacks.png");

    public final static ItemStackDisplayGui INSTANCE = new ItemStackDisplayGui(Minecraft.getInstance());

    private static class Stack {
        private int x = -1;
        private int y = 0;
        private int width = 0;
        private int height = 0;
        private String text = "";

        public void setIcon(int index, int c) {
            if(index < 0) {
                clear();
            } else if(index >= 0 && index < 104) {
                x = (index % 52) * 16 + 192;
                y = (index / 52) * 16;
                width = 16;
                height = 16;
                text = (c == 0) ? "" : String.valueOf(c);
            } else {
                index -= 104;
                x = (index % 16) * 64;
                y = (index / 16) * 64 + 32;
                width = 64;
                height = 64;
                text = null;
            }
        }

        public void clear() {
            x = -1;
        }
    }

    private final Minecraft mc;

    private final Stack[] icons = new Stack[9];
    private boolean inactive = true;

    public ItemStackDisplayGui(Minecraft mc) {
        this.mc = mc;

        for(int i = 0; i < icons.length; i++) {
            icons[i] = new Stack();
        }
    }

    public void setActive(boolean active) {
        this.inactive = !active;
    }

    public void setIcon(int index, int i, int count) {
        if(index >= 0 && index < 9) {
            icons[index].setIcon(i, count);
        }
    }

    public final void clear() {
        for(Stack stack : icons) {
            stack.clear();
        }
    }

    public void paint(KeyManager km) {
        if(inactive) {
            return;
        }

        RenderSystem.enableBlend();
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);

        mc.getTextureManager().bindTexture(CUSTOM_ICONS);

        int screenWidth = (mc.getMainWindow().getScaledWidth() >> 1) - 91;
        int y = 3;

        if(ClientReflectionUtils.isRenderingBossBar()) {
            y += 20;
        }

        blit(screenWidth, y - 3, 0, 0, 182, 22, 1024, 1024);

        int i = 0;
        for(int x = 3; x < 164; x += 20) {
            Stack s = icons[i];
            if(s.x != -1) {
                blit(screenWidth + x, y, 16, 16, s.x, s.y, s.width, s.height, 1024, 1024);
            }
            i++;
        }

        RenderSystem.disableBlend();

        FontRenderer fr = mc.fontRenderer;
        i = 0;
        for(int x = 20; x < 181; x += 20) {
            if(icons[i].x != -1) {
                if(icons[i].text == null) {
                    String s = km.getKeyDescription(i);
                    fr.drawStringWithShadow(s, (float) (screenWidth + x - fr.getStringWidth(s)), y + 9, 16777215);
                } else if(!icons[i].text.isEmpty()) {
                    fr.drawStringWithShadow(icons[i].text, (float) (screenWidth + x - fr.getStringWidth(icons[i].text)), y + 9, 16777215);
                }
            }
            i++;
        }
    }
}
