//@@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() {
        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() {
        hotkeyManager = Provider.getCurrentProvider(false);
        hotkeyManager.register(KeyStroke.getKeyStroke(LUCKY_DRAW_NOTIFICATION_COMBINATION), new HotKeyListener() {
            public void onHotKey(HotKey hotKey) {
                System.out.println(hotKey);
                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();
        }
    }
}

@@ -2610,32 +2610,77 @@
 rintln(hotKey);%0A
+                Platform.runLater(()-%3E %7B%0A    
                 
@@ -2736,32 +2736,36 @@
                 
+    
 @Override%0A      
@@ -2750,32 +2750,36 @@
       @Override%0A
+    
                 
@@ -2798,24 +2798,28 @@
 oid run() %7B%0A
+    
             
@@ -2860,24 +2860,28 @@
             
+    
 SystemTray t
@@ -2910,24 +2910,28 @@
 stemTray();%0A
+    
             
@@ -3055,16 +3055,20 @@
         
+    
 java.awt
@@ -3127,16 +3127,20 @@
         
+  
+  
 TrayIcon
@@ -3208,32 +3208,36 @@
                 
+    
 trayIcon.setImag
@@ -3273,32 +3273,36 @@
                 
+ 
+   
 trayIcon.setTool
@@ -3326,24 +3326,28 @@
 eminder%22);%0A%0A
+    
             
@@ -3442,16 +3442,20 @@
         
+    
 dismissM
@@ -3527,32 +3527,36 @@
                 
+   
+ 
 @Override%0A      
@@ -3541,32 +3541,36 @@
       @Override%0A
+    
                 
@@ -3662,24 +3662,28 @@
             
+  
+  
 tray.remove(
@@ -3685,32 +3685,36 @@
 move(trayIcon);%0A
+    
                 
@@ -3763,13 +3763,21 @@
     
+    
 %7D);%0A%0A
+    
     
@@ -3867,16 +3867,20 @@
         
+    
 popupMen
@@ -3900,24 +3900,28 @@
 MenuItem);%0A%0A
+    
             
@@ -3994,24 +3994,28 @@
             
+    
 tray.add(tra
@@ -4042,32 +4042,36 @@
                 
+  
+  
 trayIcon.display
@@ -4100,16 +4100,72 @@
 minder%22,
+%0A                                                       
  %22This t
@@ -4201,16 +4201,20 @@
 .INFO);%0A
+    
         
@@ -4284,16 +4284,20 @@
         
+    
 e.printS
@@ -4305,24 +4305,28 @@
 ackTrace();%0A
+    
             
@@ -4351,34 +4351,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -4441,16 +4441,36 @@
 ed once%0A
+                %7D);%0A
         

Update HotkeyManager