import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class NasaCounter {
    private static void Window() {
        // Create and set up the window.
        JFrame frame = new JFrame("NASA COUNTER");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        JPanel bottom_buttons = new JPanel();

        //Spécifier la couleur d'arrière-plan du JPanel
        bottom_buttons.setBackground(Color.lightGray);

        //Créer le texte
        CounterComponent counter = new CounterComponent();

        //Créer le bouton 1
        JButton btn1 = new JButton("-");
        btn1.addActionListener(e -> counter.decrease());

        //Créer le bouton 2
        JButton btn2 = new JButton("+");
        btn2.addActionListener(e -> counter.increase());

        //Créer le bouton start
        JButton btnstart = new JButton("Start");
        btnstart.addActionListener(e -> counter.startCounter());

        //Créer le bouton start
        JButton btnstop = new JButton("Pause");
        btnstop.addActionListener(e -> counter.stopCounter());

        //Créer le bouton start
        //JButton btnstop = new JButton("Stop");
        //btn2.addActionListener(e -> counter.stop());

        //Ajouter les deux boutons au JPanel
        bottom_buttons.add(btn1);
        bottom_buttons.add(btn2);
        bottom_buttons.add(btnstart);
        bottom_buttons.add(btnstop);
        panel.add(counter, BorderLayout.CENTER);
        panel.add(bottom_buttons, BorderLayout.PAGE_END);

        //Ajouter le JPanel au JFrame
        frame.add(panel);

        frame.pack();
        frame.setVisible(true);
    }

    static class CounterComponent extends JLabel {
        private int counter = 0;
        private Timer timer;

        public CounterComponent() {
            this.updateText();
            this.setPreferredSize(new Dimension(500, 200));
            this.setHorizontalAlignment(CENTER);
            //this.setVerticalAlignment(CENTER);
        }

        private void updateText() {
            this.setText("<html><h1><font color='black'>"+counter+"</font></h1></html>");
        }

        private void endTextTimer() {
            this.setText("<html><h1><font color='red'>END</font></h1></html>");
        }

        /*public void setCounter(int newValue) {
            counter = newValue;
            updateText();
        }*/

        public void startCounter() {
            if (timer != null) {
                timer.stop(); // Si un timer précédent est en cours, on l'arrête
            }

            // Créer un nouveau timer qui s'exécute chaque seconde
            timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (counter > 0) {
                        decrease();  // Décrémenter le compteur chaque seconde
                        if (counter == 0) {
                            endTextTimer();
                        } else {
                            updateText();
                        }
                    } else {
                        timer.stop(); // Arrêter le timer quand le compteur atteint 0
                    }
                }
            });
            timer.start();
        }
        public void stopCounter() {
            if (timer != null) {
                timer.stop(); // Si un timer précédent est en cours, on l'arrête
            }
        }

        public void increase() {
            counter++;
            updateText();
        }

        public void decrease() {
            if(counter>0) {
                counter--;
                updateText();
            }
        }
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Window();
            }
        });
    }
}
