package org.jmeterplugins.repository;

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

import javax.swing.*;

import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.MainFrame;
import org.apache.jmeter.gui.util.JMeterToolBar;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.jmeterplugins.repository.logging.LoggingHooker;
import org.jmeterplugins.repository.util.ComponentFinder;

import java.util.Arrays;

public class PluginManagerMenuItem extends JMenuItem implements ActionListener {
    private static final long serialVersionUID = -8708638472918746046L;
    private static final Logger log = LoggingManager.getLoggerForClass();
    private static PluginManagerDialog dialog;
    private final PluginManager mgr;

    public PluginManagerMenuItem() {
        super("Plugins Manager");
        addActionListener(this);

        mgr = new PluginManager(); // don't delay startup for longer that 1 second
        LoggingHooker hooker = new LoggingHooker(mgr);
        hooker.hook();
        final JButton toolbarButton = getToolbarButton();
        addToolbarIcon(toolbarButton);
        setIcon(getPluginsIcon(false));

        new Thread("repo-downloader-thread") {
            @Override
            public void run() {
                try {
                    mgr.load();
                } catch (Throwable e) {
                    log.warn("Failed to load plugin updates info", e);
                }

                if (mgr.hasAnyUpdates()) {
                    setText("Plugins Manager (has upgrades)");
                    log.info("Plugins Manager has upgrades: " + Arrays.toString(mgr.getUpgradablePlugins().toArray()));
                }

                setIcon(getPluginsIcon(mgr.hasAnyUpdates()));
                toolbarButton.setIcon(getIcon22Px(mgr.hasAnyUpdates()));

            }
        }.start();
    }

    private void addToolbarIcon(final Component toolbarButton) {
        GuiPackage instance = GuiPackage.getInstance();
        if (instance != null) {
            final MainFrame mf = instance.getMainFrame();
            final ComponentFinder<JMeterToolBar> finder = new ComponentFinder<>(JMeterToolBar.class);
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JMeterToolBar toolbar = null;
                    while (toolbar == null) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            log.debug("Did not add btn to toolbar", e);
                        }
                        log.debug("Searching for toolbar");
                        toolbar = finder.findComponentIn(mf);
                    }

                    int pos = toolbar.getComponents().length - 1;
                    toolbarButton.setSize(toolbar.getComponent(pos).getSize());
                    toolbar.add(toolbarButton, pos + 1);
                }
            });
        }
    }

    private JButton getToolbarButton() {
        JButton button = new JButton(getIcon22Px(mgr.hasAnyUpdates()));
        button.setToolTipText("Plugins Manager (has upgrades)");
        button.addActionListener(this);
        return button;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (dialog == null) {
            dialog = new PluginManagerDialog(mgr);
        }

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

    public static ImageIcon getIcon22Px(boolean hasUpdates) {
        if (hasUpdates) {
            return new ImageIcon(PluginManagerMenuItem.class.getResource("/org/jmeterplugins/logo22Update.png"));
        } else {
            return new ImageIcon(PluginManagerMenuItem.class.getResource("/org/jmeterplugins/logo22.png"));
        }
    }

    public static ImageIcon getPluginsIcon(boolean hasUpdates) {
        if (hasUpdates) {
            return new ImageIcon(PluginManagerMenuItem.class.getResource("/org/jmeterplugins/logoUpdate.png"));
        } else {
            return new ImageIcon(PluginManagerMenuItem.class.getResource("/org/jmeterplugins/logo.png"));
        }
    }
}

@@ -1783,32 +1783,93 @@
              %7D%0A%0A
+                boolean hasAnyUpdates = mgr.hasAnyUpdates();%0A
                 
@@ -1891,20 +1891,16 @@
 insIcon(
-mgr.
 hasAnyUp
@@ -1896,34 +1896,32 @@
 on(hasAnyUpdates
-()
 ));%0A            
@@ -1962,32 +1962,206 @@
 2Px(
-mgr.hasAnyUpdates())
+hasAnyUpdates));%0A                toolbarButton.setToolTipText(hasAnyUpdates ?%0A                        %22Plugins Manager (has upgrades)%22 :%0A                        %22Plugins Manager%22%0A                
 );%0A
-%0A
     
@@ -3394,24 +3394,77 @@
 rButton() %7B%0A
+        boolean hasAnyUpdates = mgr.hasAnyUpdates();%0A
         JBut
@@ -3500,20 +3500,16 @@
 con22Px(
-mgr.
 hasAnyUp
@@ -3513,18 +3513,16 @@
 yUpdates
-()
 ));%0A    
@@ -3551,40 +3551,117 @@
 ext(
-%22Plugins Manager (has upgrades)%22
+hasAnyUpdates ?%0A                %22Plugins Manager (has upgrades)%22 :%0A                %22Plugins Manager%22%0A        
 );%0A 

Fix "has upgrades" Tool Tip text (#50)