//@@author A0139925U
package seedu.tache.ui;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

import com.tulskiy.keymaster.common.HotKey;
import com.tulskiy.keymaster.common.HotKeyListener;
import com.tulskiy.keymaster.common.Provider;

import javafx.application.Platform;
import javafx.stage.Stage;

public class HotkeyManager {
    public static final String DEFAULT_HOTKEY_COMBINATION = "control alt D";
    public static final String LUCKY_DRAW_NOTIFICATION_COMBINATION = "alt Q";

    private Provider hotkeyManager;
    private Stage stage;
    private String hotkeyCombination;
    private Timer notificationTimer;

    public HotkeyManager(Stage stage) {
        this.stage = stage;
        this.hotkeyCombination = DEFAULT_HOTKEY_COMBINATION;
        this.notificationTimer = new Timer();
    }

    public HotkeyManager(Stage stage, String hotkeyCombination) {
        this.stage = stage;
        this.hotkeyCombination = hotkeyCombination;
        this.notificationTimer = new Timer();
    }

    /**
     * Bind show/hide (toggle) hotkey
     */
    private void bindToggleSystemHotkey() {
        if (hotkeyManager == null) {
            hotkeyManager = Provider.getCurrentProvider(false);
        }
        hotkeyManager.register(KeyStroke.getKeyStroke(hotkeyCombination), new HotKeyListener() {
            public void onHotKey(HotKey hotKey) {
                System.out.println(hotKey);
                if (stage.isFocused()) {
                    if (stage.isIconified()) {
                        Platform.runLater(()-> {
                            stage.setIconified(false); });
                    } else {
                        Platform.runLater(()-> {
                            stage.setIconified(true); });
                    }
                } else {
                    Platform.runLater(()-> {
                        stage.setIconified(false);
                        stage.toFront(); });
                }
            }
        });
    }

    /**
     * Bind lucky draw notification trigger hotkey
     */
    private void bindTriggerNotificationHotkey() {
        if (hotkeyManager == null) {
            hotkeyManager = Provider.getCurrentProvider(false);
        }
        hotkeyManager.register(KeyStroke.getKeyStroke(LUCKY_DRAW_NOTIFICATION_COMBINATION), new HotKeyListener() {
            public void onHotKey(HotKey hotKey) {
                System.out.println(hotKey);
                Platform.runLater(()-> {
                    notificationTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            try {
                                SystemTray tray = SystemTray.getSystemTray();
                                ImageIcon icon = new ImageIcon(getClass().getResource("/images/info_icon.png"));
                                java.awt.Image image = icon.getImage();
                                TrayIcon trayIcon = new TrayIcon(image, "notification");
                                trayIcon.setImageAutoSize(true);
                                trayIcon.setToolTip("Lucky Draw Reminder");

                                MenuItem dismissMenuItem = new MenuItem("Dismiss");
                                dismissMenuItem.addActionListener(new ActionListener() {
                                    @Override
                                    public void actionPerformed(ActionEvent e) {
                                        tray.remove(trayIcon);
                                    }
                                });

                                PopupMenu popupMenu = new PopupMenu();
                                popupMenu.add(dismissMenuItem);

                                trayIcon.setPopupMenu(popupMenu);
                                tray.add(trayIcon);
                                trayIcon.displayMessage("Lucky Draw Reminder",
                                                        "This task is due now.", MessageType.INFO);
                            } catch (AWTException e) {
                                e.printStackTrace();
                            }
                        }
                    }, 0); //0 indicate that it will only be scheduled once
                });
            }
        });
    }

    /**
     * Starts binding of toggle hotkey
     */
    public void start() {
        bindToggleSystemHotkey();
        bindTriggerNotificationHotkey();
    }

    /**
     * Stops binding of toggle hotkey
     */
    public void stop() {
        if (hotkeyManager != null) {
            hotkeyManager.reset();
            hotkeyManager.stop();
            notificationTimer.cancel();
        }
    }
}

@@ -43,339 +43,8 @@
 i;%0A%0A
-import java.awt.AWTException;%0Aimport java.awt.MenuItem;%0Aimport java.awt.PopupMenu;%0Aimport java.awt.SystemTray;%0Aimport java.awt.TrayIcon;%0Aimport java.awt.TrayIcon.MessageType;%0Aimport java.awt.event.ActionEvent;%0Aimport java.awt.event.ActionListener;%0Aimport java.util.Timer;%0Aimport java.util.TimerTask;%0A%0Aimport javax.swing.ImageIcon;%0A
 impo
@@ -69,16 +69,16 @@
 Stroke;%0A
+
 %0Aimport 
@@ -386,86 +386,8 @@
  D%22;
-%0A    public static final String LUCKY_DRAW_NOTIFICATION_COMBINATION = %22alt Q%22;
 %0A%0A  
@@ -486,45 +486,8 @@
 ion;
-%0A    private Timer notificationTimer;
 %0A%0A  
@@ -617,54 +617,8 @@
 ON;%0A
-        this.notificationTimer = new Timer();%0A
     
@@ -770,54 +770,8 @@
 on;%0A
-        this.notificationTimer = new Timer();%0A
     
@@ -1748,2322 +1748,8 @@
  %7D%0A%0A
-    /**%0A     * Bind lucky draw notification trigger hotkey%0A     */%0A    private void bindTriggerNotificationHotkey() %7B%0A        if (hotkeyManager == null) %7B%0A            hotkeyManager = Provider.getCurrentProvider(false);%0A        %7D%0A        hotkeyManager.register(KeyStroke.getKeyStroke(LUCKY_DRAW_NOTIFICATION_COMBINATION), new HotKeyListener() %7B%0A            public void onHotKey(HotKey hotKey) %7B%0A                System.out.println(hotKey);%0A                Platform.runLater(()-%3E %7B%0A                    notificationTimer.schedule(new TimerTask() %7B%0A                        @Override%0A                        public void run() %7B%0A                            try %7B%0A                                SystemTray tray = SystemTray.getSystemTray();%0A                                ImageIcon icon = new ImageIcon(getClass().getResource(%22/images/info_icon.png%22));%0A                                java.awt.Image image = icon.getImage();%0A                                TrayIcon trayIcon = new TrayIcon(image, %22notification%22);%0A                                trayIcon.setImageAutoSize(true);%0A                                trayIcon.setToolTip(%22Lucky Draw Reminder%22);%0A%0A                                MenuItem dismissMenuItem = new MenuItem(%22Dismiss%22);%0A                                dismissMenuItem.addActionListener(new ActionListener() %7B%0A                                    @Override%0A                                    public void actionPerformed(ActionEvent e) %7B%0A                                        tray.remove(trayIcon);%0A                                    %7D%0A                                %7D);%0A%0A                                PopupMenu popupMenu = new PopupMenu();%0A                                popupMenu.add(dismissMenuItem);%0A%0A                                trayIcon.setPopupMenu(popupMenu);%0A                                tray.add(trayIcon);%0A                                trayIcon.displayMessage(%22Lucky Draw Reminder%22,%0A                                                        %22This task is due now.%22, MessageType.INFO);%0A                            %7D catch (AWTException e) %7B%0A                                e.printStackTrace();%0A                            %7D%0A                        %7D%0A                    %7D, 0); //0 indicate that it will only be scheduled once%0A                %7D);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     
@@ -1863,49 +1863,8 @@
 ();%0A
-        bindTriggerNotificationHotkey();%0A
     
@@ -2013,24 +2013,24 @@
 er.reset();%0A
+
             
@@ -2055,48 +2055,8 @@
 ();%0A
-            notificationTimer.cancel();%0A
     

Remove Lucky Draw Notification