package net.minecraft.client.gui.components.toasts;

import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.gui.Font;
import java.util.function.ToIntFunction;
import net.minecraft.network.chat.Style;
import net.minecraft.client.Minecraft;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import java.util.List;
import net.minecraft.network.chat.FormattedText;

public class SystemToast implements Toast {
    private final SystemToastIds id;
    private FormattedText title;
    private List<FormattedText> messageLines;
    private long lastChanged;
    private boolean changed;
    private final int width;
    
    public SystemToast(final SystemToastIds a, final Component mr2, @Nullable final Component mr3) {
        this(a, mr2, nullToEmpty(mr3), 160);
    }
    
    public static SystemToast multiline(final Minecraft dlx, final SystemToastIds a, final Component mr3, final Component mr4) {
        final Font dmt5 = dlx.font;
        final List<FormattedText> list6 = dmt5.getSplitter().splitLines(mr4, 200, Style.EMPTY);
        final int integer7 = Math.max(200, list6.stream().mapToInt(dmt5::width).max().orElse(200));
        return new SystemToast(a, mr3, list6, integer7 + 30);
    }
    
    private SystemToast(final SystemToastIds a, final Component mr, final List<FormattedText> list, final int integer) {
        this.id = a;
        this.title = mr;
        this.messageLines = list;
        this.width = integer;
    }
    
    private static ImmutableList<FormattedText> nullToEmpty(@Nullable final Component mr) {
        return (ImmutableList<FormattedText>)((mr == null) ? ImmutableList.<Component>of() : ImmutableList.<Component>of(mr));
    }
    
    @Override
    public int width() {
        return this.width;
    }
    
    @Override
    public Visibility render(final PoseStack dhl, final ToastComponent doo, final long long3) {
        if (this.changed) {
            this.lastChanged = long3;
            this.changed = false;
        }
        doo.getMinecraft().getTextureManager().bind(SystemToast.TEXTURE);
        RenderSystem.color3f(1.0f, 1.0f, 1.0f);
        final int integer6 = this.width();
        final int integer7 = 12;
        if (integer6 == 160 && this.messageLines.size() <= 1) {
            doo.blit(dhl, 0, 0, 0, 64, integer6, this.height());
        }
        else {
            final int integer8 = this.height() + Math.max(0, this.messageLines.size() - 1) * 12;
            final int integer9 = 28;
            final int integer10 = Math.min(4, integer8 - 28);
            this.renderBackgroundRow(dhl, doo, integer6, 0, 0, 28);
            for (int integer11 = 28; integer11 < integer8 - integer10; integer11 += 10) {
                this.renderBackgroundRow(dhl, doo, integer6, 16, integer11, Math.min(16, integer8 - integer11 - integer10));
            }
            this.renderBackgroundRow(dhl, doo, integer6, 32 - integer10, integer8 - integer10, integer10);
        }
        if (this.messageLines == null) {
            doo.getMinecraft().font.draw(dhl, this.title, 18.0f, 12.0f, -256);
        }
        else {
            doo.getMinecraft().font.draw(dhl, this.title, 18.0f, 7.0f, -256);
            for (int integer8 = 0; integer8 < this.messageLines.size(); ++integer8) {
                doo.getMinecraft().font.draw(dhl, this.messageLines.get(integer8), 18.0f, (float)(18 + integer8 * 12), -1);
            }
        }
        return (long3 - this.lastChanged < 5000L) ? Visibility.SHOW : Visibility.HIDE;
    }
    
    private void renderBackgroundRow(final PoseStack dhl, final ToastComponent doo, final int integer3, final int integer4, final int integer5, final int integer6) {
        final int integer7 = (integer4 == 0) ? 20 : 5;
        final int integer8 = Math.min(60, integer3 - integer7);
        doo.blit(dhl, 0, integer5, 0, 64 + integer4, integer7, integer6);
        for (int integer9 = integer7; integer9 < integer3 - integer8; integer9 += 64) {
            doo.blit(dhl, integer9, integer5, 32, 64 + integer4, Math.min(64, integer3 - integer9 - integer8), integer6);
        }
        doo.blit(dhl, integer3 - integer8, integer5, 160 - integer8, 64 + integer4, integer8, integer6);
    }
    
    public void reset(final Component mr1, @Nullable final Component mr2) {
        this.title = mr1;
        this.messageLines = nullToEmpty(mr2);
        this.changed = true;
    }
    
    @Override
    public SystemToastIds getToken() {
        return this.id;
    }
    
    public static void add(final ToastComponent doo, final SystemToastIds a, final Component mr3, @Nullable final Component mr4) {
        doo.addToast(new SystemToast(a, mr3, mr4));
    }
    
    public static void addOrUpdate(final ToastComponent doo, final SystemToastIds a, final Component mr3, @Nullable final Component mr4) {
        final SystemToast dom5 = doo.<SystemToast>getToast(SystemToast.class, a);
        if (dom5 == null) {
            add(doo, a, mr3, mr4);
        }
        else {
            dom5.reset(mr3, mr4);
        }
    }
    
    public static void onWorldAccessFailure(final Minecraft dlx, final String string) {
        add(dlx.getToasts(), SystemToastIds.WORLD_ACCESS_FAILURE, new TranslatableComponent("selectWorld.access_failure"), new TextComponent(string));
    }
    
    public static void onWorldDeleteFailure(final Minecraft dlx, final String string) {
        add(dlx.getToasts(), SystemToastIds.WORLD_ACCESS_FAILURE, new TranslatableComponent("selectWorld.delete_failure"), new TextComponent(string));
    }
    
    public static void onPackCopyFailure(final Minecraft dlx, final String string) {
        add(dlx.getToasts(), SystemToastIds.PACK_COPY_FAILURE, new TranslatableComponent("pack.copyFailure"), new TextComponent(string));
    }
    
    public enum SystemToastIds {
        TUTORIAL_HINT, 
        NARRATOR_TOGGLE, 
        WORLD_BACKUP, 
        WORLD_GEN_SETTINGS_TRANSFER, 
        PACK_LOAD_FAILURE, 
        WORLD_ACCESS_FAILURE, 
        PACK_COPY_FAILURE;
    }
}
