package me.km.networking;

import com.mojang.blaze3d.systems.RenderSystem;
import me.km.KajetansMod;
import me.km.inventory.CustomContainer;
import me.km.inventory.ModInventory;
import net.minecraft.client.gui.IHasContainer;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class CustomInventoryScreen extends ContainerScreen<CustomContainer> implements IHasContainer<CustomContainer> {
    private static final ResourceLocation CHEST_GUI_TEXTURE = new ResourceLocation("textures/gui/container/generic_54.png");
    private static final ResourceLocation EMPTY_TILE = new ResourceLocation(KajetansMod.MODID, "textures/gui/container/empty_tile.png");

    private final int inventoryRows;

    public CustomInventoryScreen(CustomContainer cc, PlayerInventory pInv, ITextComponent title) {
        super(cc, pInv, title);
        passEvents = false;
        inventoryRows = cc.getInventoryBase().getRows();
        ySize = 114 + inventoryRows * 18;
    }

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

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
        this.font.drawString(title.getFormattedText(), 8.0f, 6.0f, 4210752);
        this.font.drawString(playerInventory.getDisplayName().getFormattedText(), 8.0f, ySize - 94, 4210752);
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        minecraft.getTextureManager().bindTexture(CHEST_GUI_TEXTURE);
        int i = (width - xSize) / 2;
        int j = (height - ySize) / 2;
        blit(i, j, 0, 0, this.xSize, inventoryRows * 18 + 17);
        blit(i, j + inventoryRows * 18 + 17, 0, 126, xSize, 96);

        i += 7;
        j += 17;
        minecraft.getTextureManager().bindTexture(EMPTY_TILE);
        ModInventory base = this.getContainer().getInventoryBase();
        for(int x = 0; x < 9; x++) {
            for(int y = 0; y < inventoryRows; y++) {
                if(base.shouldRenderOverlay(x, y)) {
                    blit(i + 18 * x, j + 18 * y, 0, 0, 18, 18, 64, 64);
                } else if(base.shouldRenderDarker(x, y)) {
                    blit(i + 18 * x, j + 18 * y, 18, 0, 18, 18, 64, 64);
                }
            }
        }
    }

    /*public void blit(int startX, int startY, int tStartX, int tStartY, int width, int height)
    {
        blit(startX, startY, this.blitOffset, tStartX, tStartY, width, height, 256, 256);
    }

    public static void blit(int startX, int startY, int z, float tStartX, float tStartY, int width, int height, int rWidth, int rHeight)
    {
        innerBlit(startX, startX + width, startY, startY + height, z, width, height, tStartX, tStartY, rHeight, rWidth);
    }

    public static void blit(int minX, int minY, int width, int height, float tStartX, float tStartY, int tWidth, int tHeight, int rWidth, int rHeight)
    {
        innerBlit(minX, minX + width, minY, minY + height, 0, tWidth, tHeight, tStartX, tStartY, rWidth, rHeight);
    }
    
    public static void blit(int p_blit_0_, int p_blit_1_, float p_blit_2_, float p_blit_3_, int p_blit_4_, int p_blit_5_, int p_blit_6_, int p_blit_7_)
    {
        blit(p_blit_0_, p_blit_1_, p_blit_4_, p_blit_5_, p_blit_2_, p_blit_3_, p_blit_4_, p_blit_5_, p_blit_6_, p_blit_7_);
    }

    private static void innerBlit(int minX, int maxX, int minY, int maxY, int z, int tWidth, int tHight, float tStartX, float tStartY, int rWidth, int rHeight)
    {
        innerBlit(minX, maxX, minY, maxY, z, 
                tStartX / rWidth, (tStartX + tWidth) / rWidth, 
                tStartY / rHeight, (tStartY + tHight) / rHeight);
    }

    protected static void innerBlit(int minX, int maxX, int minY, int maxY, int z, float tMinX, float tMaxX, float tMinY, float tMaxY)
    {
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuffer();
        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);
        bufferbuilder.pos(minX, maxY, z).tex(tMinX, tMaxY).endVertex();
        bufferbuilder.pos(maxX, maxY, z).tex(tMaxX, tMaxY).endVertex();
        bufferbuilder.pos(maxX, minY, z).tex(tMaxX, tMinY).endVertex();
        bufferbuilder.pos(minX, minY, z).tex(tMinX, tMinY).endVertex();
        tessellator.draw();
    }*/
}
