package net.minecraft;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.logging.LogUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.UUID;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.level.storage.DataVersion;
import org.slf4j.Logger;

public class DetectedVersion {
    private static final Logger LOGGER = LogUtils.getLogger();
    public static final WorldVersion BUILT_IN = createFromConstants();

    private static WorldVersion createFromConstants() {
        return new WorldVersion.Simple(
            UUID.randomUUID().toString().replaceAll("-", ""),
            "1.21.8",
            new DataVersion(4440, "main"),
            SharedConstants.getProtocolVersion(),
            64,
            81,
            new Date(),
            true
        );
    }

    private static WorldVersion createFromJson(JsonObject json) {
        JsonObject asJsonObject = GsonHelper.getAsJsonObject(json, "pack_version");
        return new WorldVersion.Simple(
            GsonHelper.getAsString(json, "id"),
            GsonHelper.getAsString(json, "name"),
            new DataVersion(GsonHelper.getAsInt(json, "world_version"), GsonHelper.getAsString(json, "series_id", "main")),
            GsonHelper.getAsInt(json, "protocol_version"),
            GsonHelper.getAsInt(asJsonObject, "resource"),
            GsonHelper.getAsInt(asJsonObject, "data"),
            Date.from(ZonedDateTime.parse(GsonHelper.getAsString(json, "build_time")).toInstant()),
            GsonHelper.getAsBoolean(json, "stable")
        );
    }

    public static WorldVersion tryDetectVersion() {
        try {
            WorldVersion var2;
            try (InputStream resourceAsStream = DetectedVersion.class.getResourceAsStream("/version.json")) {
                if (resourceAsStream == null) {
                    LOGGER.warn("Missing version information!");
                    return BUILT_IN;
                }

                try (InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream)) {
                    var2 = createFromJson(GsonHelper.parse(inputStreamReader));
                }
            }

            return var2;
        } catch (JsonParseException | IOException var8) {
            throw new IllegalStateException("Game version information is corrupt", var8);
        }
    }
}
