package net.minecraft.client.gui.screens.inventory;

import net.minecraft.world.effect.MobEffectUtil;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.client.resources.MobEffectTextureManager;
import net.minecraft.client.gui.GuiComponent;
import java.util.Iterator;
import java.util.Collection;
import net.minecraft.world.effect.MobEffectInstance;
import com.google.common.collect.Ordering;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;

public abstract class EffectRenderingInventoryScreen<T extends AbstractContainerMenu> extends AbstractContainerScreen<T> {
    protected boolean doRenderEffects;
    
    public EffectRenderingInventoryScreen(final T bgi, final Inventory beb, final Component mr) {
        super(bgi, beb, mr);
    }
    
    @Override
    protected void init() {
        super.init();
        this.checkEffectRendering();
    }
    
    protected void checkEffectRendering() {
        if (this.minecraft.player.getActiveEffects().isEmpty()) {
            this.leftPos = (this.width - this.imageWidth) / 2;
            this.doRenderEffects = false;
        }
        else {
            this.leftPos = 160 + (this.width - this.imageWidth - 200) / 2;
            this.doRenderEffects = true;
        }
    }
    
    @Override
    public void render(final PoseStack dhl, final int integer2, final int integer3, final float float4) {
        super.render(dhl, integer2, integer3, float4);
        if (this.doRenderEffects) {
            this.renderEffects(dhl);
        }
    }
    
    private void renderEffects(final PoseStack dhl) {
        final int integer3 = this.leftPos - 124;
        final Collection<MobEffectInstance> collection4 = this.minecraft.player.getActiveEffects();
        if (collection4.isEmpty()) {
            return;
        }
        RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
        int integer4 = 33;
        if (collection4.size() > 5) {
            integer4 = 132 / (collection4.size() - 1);
        }
        final Iterable<MobEffectInstance> iterable6 = Ordering.<Comparable>natural().<MobEffectInstance>sortedCopy(collection4);
        this.renderBackgrounds(dhl, integer3, integer4, iterable6);
        this.renderIcons(dhl, integer3, integer4, iterable6);
        this.renderLabels(dhl, integer3, integer4, iterable6);
    }
    
    private void renderBackgrounds(final PoseStack dhl, final int integer2, final int integer3, final Iterable<MobEffectInstance> iterable) {
        this.minecraft.getTextureManager().bind(EffectRenderingInventoryScreen.INVENTORY_LOCATION);
        int integer4 = this.topPos;
        for (final MobEffectInstance aog8 : iterable) {
            RenderSystem.color4f(1.0f, 1.0f, 1.0f, 1.0f);
            this.blit(dhl, integer2, integer4, 0, 166, 140, 32);
            integer4 += integer3;
        }
    }
    
    private void renderIcons(final PoseStack dhl, final int integer2, final int integer3, final Iterable<MobEffectInstance> iterable) {
        final MobEffectTextureManager emi6 = this.minecraft.getMobEffectTextures();
        int integer4 = this.topPos;
        for (final MobEffectInstance aog9 : iterable) {
            final MobEffect aoe10 = aog9.getEffect();
            final TextureAtlasSprite elv11 = emi6.get(aoe10);
            this.minecraft.getTextureManager().bind(elv11.atlas().location());
            GuiComponent.blit(dhl, integer2 + 6, integer4 + 7, this.getBlitOffset(), 18, 18, elv11);
            integer4 += integer3;
        }
    }
    
    private void renderLabels(final PoseStack dhl, final int integer2, final int integer3, final Iterable<MobEffectInstance> iterable) {
        int integer4 = this.topPos;
        for (final MobEffectInstance aog8 : iterable) {
            String string9 = I18n.get(aog8.getEffect().getDescriptionId());
            if (aog8.getAmplifier() >= 1 && aog8.getAmplifier() <= 9) {
                string9 = string9 + ' ' + I18n.get("enchantment.level." + (aog8.getAmplifier() + 1));
            }
            this.font.drawShadow(dhl, string9, (float)(integer2 + 10 + 18), (float)(integer4 + 6), 16777215);
            final String string10 = MobEffectUtil.formatDuration(aog8, 1.0f);
            this.font.drawShadow(dhl, string10, (float)(integer2 + 10 + 18), (float)(integer4 + 6 + 10), 8355711);
            integer4 += integer3;
        }
    }
}
