import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

/**
 * This demonstrates a bug observed on Mac 13.2.1 using openJDK 19.0.2 in Feb of 2023 when Stage Manager is active.
 *
 * Instructions for reproducing the bug are displayed inside this JFrame window.
 */
public class StageManagerTest2 extends JFrame {

    JMenuBar menuBar = new JMenuBar();
    JMenu windowMenu = new JMenu("Window");
    public static void main(String[] args) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        SwingUtilities.invokeLater(() -> {
            StageManagerTest2 test = new StageManagerTest2();
            test.pack();
            test.setLocationRelativeTo(null);
            test.setVisible(true);
        });
    }

    JTextPane instructions = new JTextPane();


    JLabel repaintingColorBar = new JLabel(new Icon() {
        @Override
        public void paintIcon(Component c, Graphics g, int x, int y) {
            float hue = ((float) (System.currentTimeMillis()%1000)) / 1000f;
            Color color = new Color(Color.HSBtoRGB(hue, 1, 1));
            g.setColor(color);
            g.fillRect(0,0,getIconWidth(),getIconHeight());
            c.repaint();
        }

        @Override
        public int getIconWidth() {
            return 600;
        }

        @Override
        public int getIconHeight() {
            return 20;
        }
    });

    public StageManagerTest2() {
        AbstractAction minimizeAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setExtendedState(JFrame.ICONIFIED);
            }
        };
        String name = "Minimize (Iconify)";
        minimizeAction.putValue(Action.NAME, name);
        JButton minimizeButton = new JButton(minimizeAction);
        minimizeAction.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_M, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
        JMenuItem minimizeMenuItem = new JMenuItem(minimizeAction);
        windowMenu.add(minimizeMenuItem);
        menuBar.add(windowMenu);
        setJMenuBar(menuBar);

        instructions.setEditable(false);
        instructions.setBorder(new EmptyBorder(10, 10, 10, 10));
        instructions.setText("Instructions:\n" +
                "1. Open this app.\n" +
                "2. Turn on Stage Manager (see Mac system preferences)\n" +
                "3. Select \"" + name + "\" button or menu item\n" +
                "4. Click thumbnail in stage manager to reopen application\n\n" +
                "Expected behavior: animated color bar repaints continuously.\n\n" +
                "Observed behavior: nothing repaints.");

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(instructions, BorderLayout.NORTH);
        getContentPane().add(minimizeButton, BorderLayout.CENTER);
        getContentPane().add(repaintingColorBar, BorderLayout.SOUTH);
    }
}

