// Copyright 2015 Eivind Vegsundvåg
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ninja.eivind.hotsreplayuploader.services.platform;

import java.awt.Desktop;
import java.awt.TrayIcon;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import javafx.application.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javafx.event.EventType;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
/**
 * {@link PlatformService} that is active on Apple OSX systems.
 */
public class OSXService implements PlatformService {
    private static final Logger LOG = LoggerFactory.getLogger(OSXService.class);
    private final String libraryPath = "/Library/Application Support";
    private Desktop desktop;

    public OSXService() {
        desktop = Desktop.getDesktop();
    }

    @Override
    public File getApplicationHome() {
        return new File(USER_HOME + "/" + libraryPath + "/" + APPLICATION_DIRECTORY_NAME);
    }

    @Override
    public File getHotSHome() {
        return new File(USER_HOME + "/" + libraryPath + "/" + "Blizzard/Heroes of the Storm/Accounts/");
    }

    @Override
    public void browse(final URI uri) throws IOException {
        desktop.browse(uri);
    }

    @Override
    public URL getLogoUrl() {
        String logoVariant = isMacMenuBarDarkMode() ? "" : "-black";
        return getClass().getResource(
                "/images/logo-desktop" + logoVariant + ".png");
    }

    @Override
    public TrayIcon getTrayIcon(final Stage primaryStage) throws PlatformNotSupportedException {
        URL imageURL = getLogoUrl();
        EventType<KeyEvent> keyPressed = KeyEvent.KEY_PRESSED;
        primaryStage.addEventHandler(keyPressed, event -> {
            if (event.getCode() == KeyCode.Q && event.isMetaDown()) {
                LOG.info("Exiting application due to keyboard shortcut.");
                Platform.exit();
            } else if(event.getCode() == KeyCode.H && event.isMetaDown()) {
                LOG.info("Hiding application due to keyboard shortcut.");
                primaryStage.hide();
            }
        });
        return buildTrayIcon(imageURL, primaryStage);
    }

    /**
     * Checks, if the OS X dark mode is used by querying the defaults CLI.<br>
     * This is needed for adding the correct tray icon once at the startup.
     * @return true if <code>defaults read -g AppleInterfaceStyle</code>
     * has an exit status of <code>0</code> (i.e. _not_ returning "key not found").
     */
    private boolean isMacMenuBarDarkMode() {
        try {
            /* check for exit status only.
             * Once there are more modes than "dark" and "default",
             *  we might need to analyze string contents...
             */
            final Process proc = Runtime.getRuntime().exec(
                    new String[] {"defaults", "read", "-g", "AppleInterfaceStyle"});
            proc.waitFor(100, TimeUnit.MILLISECONDS);
            return proc.exitValue() == 0;
        } catch (IOException | InterruptedException | IllegalThreadStateException ex) {
            //process didn't terminate properly
            LOG.warn("Could not determine, whether 'dark mode' is being used. "
                    + "Falling back to default (light) mode.");
            return false;
        }
    }

}

@@ -659,178 +659,135 @@
 java
-.awt.Desktop;%0Aimport java.awt.TrayIcon;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URL;%0Aimport java.util.concurrent.TimeUni
+fx.application.Platform;%0Aimport javafx.event.EventType;%0Aimport javafx.scene.input.KeyCode;%0Aimport javafx.scene.input.KeyEven
 t;%0A
-%0A
 impo
@@ -796,36 +796,27 @@
  javafx.
-application.Platform
+stage.Stage
 ;%0Aimport
@@ -882,125 +882,145 @@
 java
-fx.event.EventType;%0Aimport javafx.scene.input.KeyCode;%0Aimport javafx.scene.input.KeyEvent;%0Aimport javafx.stage.Stage;
+.awt.*;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.net.URI;%0Aimport java.net.URL;%0Aimport java.util.concurrent.TimeUnit;%0A
 %0A/**
@@ -2334,56 +2334,80 @@
 ent.
-getCode() == KeyCode.Q && event.isMetaDown()) %7B%0A
+isMetaDown()) %7B%0A                if (event.getCode() == KeyCode.Q) %7B%0A    
     
@@ -2493,16 +2493,20 @@
         
+    
 Platform
@@ -2526,16 +2526,20 @@
         
+   
+ 
 %7D else i
@@ -2539,16 +2539,17 @@
  else if
+ 
 (event.g
@@ -2573,34 +2573,16 @@
 de.H
- && event.isMetaDown()
 ) %7B%0A
+    
     
@@ -2659,32 +2659,36 @@
                 
+    
 primaryStage.hid
@@ -2688,24 +2688,42 @@
 age.hide();%0A
+                %7D%0A
             
@@ -2960,16 +2960,23 @@
 tartup.%0A
+     *%0A
      * @
@@ -3470,17 +3470,16 @@
 String%5B%5D
- 
 %7B%22defaul

extract commonality