package Console;

import Constants.ConsoleCommands;
import Networking.Server.ServerDispatcher;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultCaret;

/**
 * Clasa singleton prin care se vor afisa toate mesajele importante ale
 * serverului sau clientului pentru debug !OBS: Se poate afisa fie print
 * instanta sau prin metoda statica
 *
 * @author Dragos-Alexandru
 */
public final class ConsoleFrame extends JFrame {

    private static ConsoleFrame instance;
    public boolean ready = false;
    public static boolean showConsole;

    /**
     * Creates new form ConsoleFrame
     *
     * @param isServer
     */
    private ConsoleFrame() {
        initComponents();
        outputScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        outputArea.setLineWrap(true);
        DefaultCaret caret = (DefaultCaret) outputArea.getCaret();
        caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
        printMessage("Console", "***Welcome to BattleAI Console***\n");
    }

    public static ConsoleFrame getInstance() {
        if (instance == null) {
            instance = new ConsoleFrame();

            instance.inputField.addKeyListener(new KeyAdapter() {
                @Override
                public void keyTyped(KeyEvent e) {
                    if (e.getKeyChar() == 10) {
                        instance.sendButtonActionPerformed(null);
                    }
                }
            });
        }
        return instance;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        outputScroll = new javax.swing.JScrollPane();
        outputArea = new javax.swing.JTextArea();
        inputField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        outputArea.setColumns(20);
        outputArea.setRows(5);
        outputArea.setEditable(false);
        outputScroll.setViewportView(outputArea);

        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(inputField, javax.swing.GroupLayout.PREFERRED_SIZE, 303, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(outputScroll))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(outputScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputField)
                    .addComponent(sendButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed

        String command = inputField.getText();
        inputField.setText("");
        parseCommand(command);

    }//GEN-LAST:event_sendButtonActionPerformed

    private void parseCommand(String command) {
        if (command.startsWith("/")) {
            if (command.equals(ConsoleCommands.EXIT.getValue())) {
                this.printMessage("EXIT", "Closing server");
                System.exit(0);
            } else if (command.equals(ConsoleCommands.HELP.getValue())) {
                ConsoleCommands[] listCommands = ConsoleCommands.values();
                for (ConsoleCommands commandHelp : listCommands) {
                    this.printMessage("HELP", commandHelp.getValue() + " | " + commandHelp.getCommentary());
                }
            } else if (command.equals(ConsoleCommands.CHECK_CONNECTION.getValue())) {
                List<String> activeMatches = getConnections();
                if (activeMatches.isEmpty()) {
                    this.printMessage("CHECK_CONNECTIONS", "NONE");
                } else {
                    this.printMessage("CHECK_CONNECTIONS", activeMatches.toString());
                }
            }
        } else {
            this.printMessage(this.getClass().getSimpleName(), "Commands must start with /");
        }
    }

    private List<String> getConnections() {
        List<String> connectionsIp = ServerDispatcher.getInstance().getLocalConnections();
        return connectionsIp;
    }

    /**
     * Metoda sincronizata ce afiseaza un mesaj pe consola (Nestatica)
     *
     * @param className
     * @param message
     */
    public synchronized void printMessage(String className, String message) {
        if (showConsole) {
            outputArea.append(" " + className + ": " + message + "\n");
        }
        sendMessageStandardOutput(className, message);
    }

    /**
     * Metoda sincronizata ce afiseaza un mesaj pe consola (Statica)
     *
     * @param className
     * @param message
     */
    public synchronized static void sendMessage(String className, String message) {
        if (showConsole) {
            ConsoleFrame consola = getInstance();
            consola.printMessage(className, message);
        }
        sendMessageStandardOutput(className, message);
    }

    private synchronized static void sendMessageStandardOutput(String className, String message) {
        System.out.println(" " + className + ": " + message + "\n");
    }

    public static void showError(String message) {
        SwingUtilities.invokeLater(() -> {
            /*JOptionPane jop = new JOptionPane();
            JDialog dialog = jop.createDialog(null, message);
            dialog.setAlwaysOnTop(true);
            dialog.setVisible(true);*/
            JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField inputField;
    private javax.swing.JTextArea outputArea;
    private javax.swing.JScrollPane outputScroll;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables
}

@@ -7248,18 +7248,16 @@
         
-/*
 JOptionP
@@ -7260,19 +7260,26 @@
 ionPane 
-j
 op
+tionPane
  = new J
@@ -7285,26 +7285,64 @@
 JOptionPane(
-);
+message, JOptionPane.ERROR_MESSAGE);    
 %0A           
@@ -7363,11 +7363,18 @@
 g = 
-j
 op
+tionPane
 .cre
@@ -7383,29 +7383,23 @@
 eDialog(
-null, message
+%22Error%22
 );%0A     
@@ -7474,104 +7474,8 @@
 ue);
-*/%0A            JOptionPane.showMessageDialog(null, message, %22Error%22, JOptionPane.ERROR_MESSAGE);
 %0A   

Make error popups be always on top