package thb.jeanluc.adventure;

import lombok.extern.slf4j.Slf4j;
import thb.jeanluc.adventure.io.SwingIO;

import javax.swing.SwingUtilities;

/**
 * Entry point for the Swing version of the game. The Swing window is
 * built on the Event Dispatch Thread; the game loop itself runs in a
 * background worker so that EDT events (typing) and blocking
 * {@code readLine()} calls never deadlock each other.
 */
@Slf4j
public final class AppGui {

    private AppGui() {
    }

    /**
     * Standard JVM entry point.
     *
     * @param args ignored
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SwingIO io = new SwingIO("Haunted Manor");
            Thread worker = new Thread(() -> {
                try {
                    App.run(io);
                } catch (RuntimeException e) {
                    log.error("Fatal error during game startup", e);
                    io.write("Fatal error: " + e.getMessage());
                }
            }, "game-loop");
            worker.setDaemon(true);
            worker.start();
        });
    }
}
