package cz.jzitnik.game.ui;

import cz.jzitnik.game.Game;

import java.util.HashMap;

/**
 * Handles clean-up or exit operations when closing certain UI windows.
 */
public class CloseHandler {
    /**
     * A functional interface for handling window close operations on the game instance.
     *
     * @param <T> The type of the argument passed to the function.
     */
    @FunctionalInterface
    public interface Function<T> {
        void call(T t);
    }

    /**
     * A map of window types to their corresponding close handler functions.
     */
    public static HashMap<Window, Function<Game>> handles = new HashMap<>();

    static {
        handles.put(Window.CRAFTING_TABLE, game -> game.getGameStates().craftingTable.exit());
        handles.put(Window.INVENTORY, game -> game.getInventory().exit());
    }

    /**
     * Executes the close handler function for the specified window, if one is registered.
     *
     * @param window The window being closed.
     * @param game   The game instance.
     */
    public static void handle(Window window, Game game) {
        if (handles.containsKey(window)) {
            var func = handles.get(window);

            func.call(game);
        }
    }
}
