package net.minecraft.client;

import java.text.SimpleDateFormat;
import org.apache.logging.log4j.LogManager;
import net.minecraft.network.chat.Style;
import java.util.Date;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.Util;
import com.mojang.blaze3d.systems.RenderSystem;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import java.util.function.Consumer;
import com.mojang.blaze3d.pipeline.RenderTarget;
import java.io.File;
import java.text.DateFormat;
import org.apache.logging.log4j.Logger;

public class Screenshot {
    private static final Logger LOGGER;
    private static final DateFormat DATE_FORMAT;
    
    public static void grab(final File file, final int integer2, final int integer3, final RenderTarget dgf, final Consumer<Component> consumer) {
        grab(file, null, integer2, integer3, dgf, consumer);
    }
    
    public static void grab(final File file, @Nullable final String string, final int integer3, final int integer4, final RenderTarget dgf, final Consumer<Component> consumer) {
        if (!RenderSystem.isOnRenderThread()) {
            RenderSystem.recordRenderCall(() -> _grab(file, string, integer3, integer4, dgf, consumer));
        }
        else {
            _grab(file, string, integer3, integer4, dgf, consumer);
        }
    }
    
    private static void _grab(final File file, @Nullable final String string, final int integer3, final int integer4, final RenderTarget dgf, final Consumer<Component> consumer) {
        final NativeImage dgs7 = takeScreenshot(integer3, integer4, dgf);
        final File file2 = new File(file, "screenshots");
        file2.mkdir();
        if (string == null) {
            final File file3 = getFile(file2);
        }
        else {
            final File file3 = new File(file2, string);
        }
        final NativeImage nativeImage;
        final File file4;
        Component mr4;
        final TranslatableComponent translatableComponent;
        final TranslatableComponent translatableComponent2;
        Util.ioPool().execute(() -> {
            try {
                nativeImage.writeToFile(file4);
                mr4 = new TextComponent(file4.getName()).withStyle(ChatFormatting.UNDERLINE).withStyle(nb -> nb.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, file4.getAbsolutePath())));
                new TranslatableComponent("screenshot.success", new Object[] { mr4 });
                consumer.accept(translatableComponent);
            }
            catch (Exception exception4) {
                Screenshot.LOGGER.warn("Couldn't save screenshot", (Throwable)exception4);
                new TranslatableComponent("screenshot.failure", new Object[] { exception4.getMessage() });
                consumer.accept(translatableComponent2);
            }
            finally {
                nativeImage.close();
            }
        });
    }
    
    public static NativeImage takeScreenshot(int integer1, int integer2, final RenderTarget dgf) {
        integer1 = dgf.width;
        integer2 = dgf.height;
        final NativeImage dgs4 = new NativeImage(integer1, integer2, false);
        RenderSystem.bindTexture(dgf.colorTextureId);
        dgs4.downloadTexture(0, true);
        dgs4.flipY();
        return dgs4;
    }
    
    private static File getFile(final File file) {
        final String string2 = Screenshot.DATE_FORMAT.format(new Date());
        int integer3 = 1;
        File file2;
        while (true) {
            file2 = new File(file, string2 + ((integer3 == 1) ? "" : ("_" + integer3)) + ".png");
            if (!file2.exists()) {
                break;
            }
            ++integer3;
        }
        return file2;
    }
    
    static {
        LOGGER = LogManager.getLogger();
        DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
    }
}
