package dev.photosync.platform.impl;

import dev.photosync.mcapi.screen.ScreenHost;
import dev.photosync.mcapi.screen.ScreenModel;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;

import java.util.Optional;

/** Puts screen models on screen, and reports which one is there. */
public final class ScreenAdapter implements ScreenHost {

    @Override
    public void open(ScreenModel screen) {
        Minecraft.getInstance().setScreen(new ModelScreen(screen));
    }

    /**
     * Closes only our own screen. If the player has since opened the pause menu
     * or a chest, a late close from a finishing upload must not yank it away.
     */
    @Override
    public void close() {
        if (current().isPresent()) {
            Minecraft.getInstance().setScreen(null);
        }
    }

    @Override
    public Optional<ScreenModel> current() {
        Screen screen = Minecraft.getInstance().screen;
        return screen instanceof ModelScreen hosted ? Optional.of(hosted.model()) : Optional.empty();
    }
}
