package de.winniepat.citrus.utils.toasts;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.toast.Toast;
import net.minecraft.client.toast.ToastManager;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class CustomToast implements Toast {
    private final Text title;
    private final Text description;
    private final int titleColor;
    private final int descColor;
    private final int width;

    private static final Identifier TEXTURE = Identifier.of("minecraft", "toast/advancement");

    public CustomToast(TextRenderer renderer, Text title, int titleColor, Text description, int descColor) {
        this.title = title;
        this.titleColor = titleColor;
        this.description = description;
        this.descColor = descColor;

        int titleWidth = renderer.getWidth(title);
        int descWidth = renderer.getWidth(description);

        this.width = Math.max(40, Math.max(titleWidth, descWidth) + 16);
    }

    @Override
    public int getWidth() {
        return this.width;
    }

    @Override
    public Visibility draw(DrawContext context, ToastManager manager, long startTime) {
        context.drawGuiTexture(TEXTURE, 0, 0, this.getWidth(), 32);

        TextRenderer renderer = manager.getClient().textRenderer;

        context.drawText(renderer, title, 8, 7, titleColor, false);
        context.drawText(renderer, description, 8, 18, descColor, false);

        return startTime >= 5000L ? Visibility.HIDE : Visibility.SHOW;
    }

    public static void sendCustomToast(String title, int titleCol, String desc, int descCol) {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client != null) {
            client.getToastManager().add(new CustomToast(
                    client.textRenderer,
                    Text.literal(title), titleCol,
                    Text.literal(desc), descCol
            ));
        }
    }
}
