package fr.monkhanny.dorfromantik.game;

import fr.monkhanny.dorfromantik.Options;
import fr.monkhanny.dorfromantik.listeners.EscapeMenuCloseListener;
import fr.monkhanny.dorfromantik.listeners.GameQuitButtonListener;
import fr.monkhanny.dorfromantik.listeners.GameResumeButtonListener;
import fr.monkhanny.dorfromantik.listeners.GameSettingsButtonListener;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

public class PauseGame extends KeyAdapter {
    private EscapeMenu escapeMenu;
    private GameResumeButtonListener resumeButtonListener;
    private GameQuitButtonListener quitButtonListener;
    private GameSettingsButtonListener settingsButtonListener;

    public PauseGame(JFrame gameFrame, Game game) {
        // Initialiser escapeMenu ici avant de le passer à SettingsButtonListener
        this.escapeMenu = new EscapeMenu(gameFrame, game);  // Initialisation ici
        this.escapeMenu.setVisible(false);
        this.escapeMenu.setAlwaysOnTop(true);
        this.resumeButtonListener = new GameResumeButtonListener(this.escapeMenu);
        this.quitButtonListener = new GameQuitButtonListener();; // Initialisé après la création de escapeMenu
        this.settingsButtonListener = new GameSettingsButtonListener(gameFrame, this.escapeMenu); // Passer escapeMenu correctement
        this.escapeMenu.addWindowListener(new EscapeMenuCloseListener());
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
            if(Options.isPaused == false){
                Options.isPaused = true;
                escapeMenu.setResumeButtonListener(this.resumeButtonListener);
                escapeMenu.setQuitButtonListener(this.quitButtonListener);
                escapeMenu.setSettingsButtonListener(this.settingsButtonListener);
                this.escapeMenu.setVisible(true);
            }
            escapeMenu.toFront();
        }
    }
}

