package com.metacodestudio.hotsuploader;

import com.metacodestudio.hotsuploader.files.FileHandler;
import com.metacodestudio.hotsuploader.utils.SimpleHttpClient;
import com.metacodestudio.hotsuploader.utils.StormHandler;
import com.metacodestudio.hotsuploader.versions.ReleaseManager;
import com.metacodestudio.hotsuploader.window.HomeController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.FlowHandler;
import io.datafx.controller.flow.container.DefaultFlowContainer;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.awt.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;


public class Client extends Application {

    public static void main(String[] args) {
        Application.launch(Client.class, args);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        URL logo = loader.getResource("images/logo-desktop.png");
        assert logo != null;
        Image image = new Image(logo.toString());
        primaryStage.getIcons().add(image);
        primaryStage.setResizable(false);
        primaryStage.setTitle("HotSLogs UploaderFX");
        addToTray(logo, primaryStage);

        Flow flow = new Flow(HomeController.class);
        FlowHandler flowHandler = flow.createHandler(new ViewFlowContext());
        ViewFlowContext flowContext = flowHandler.getFlowContext();

        StormHandler stormHandler = new StormHandler();
        SimpleHttpClient httpClient = new SimpleHttpClient();
        ReleaseManager releaseManager = new ReleaseManager(httpClient);

        registerInContext(flowContext, stormHandler, releaseManager, setupFileHandler(stormHandler), httpClient);

        DefaultFlowContainer container = new DefaultFlowContainer();

        releaseManager.verifyLocalVersion(stormHandler);

        StackPane pane = flowHandler.start(container);
        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }

    private FileHandler setupFileHandler(final StormHandler stormHandler) throws IOException {
        FileHandler fileHandler = new FileHandler(stormHandler);
        fileHandler.cleanup();
        fileHandler.registerInitial();
        return fileHandler;
    }

    private void registerInContext(ViewFlowContext context, Object... itemsToAdd) {
        for (final Object itemToAdd : itemsToAdd) {
            context.register(itemToAdd);
        }
    }

    private void addToTray(final URL imageURL, Stage primaryStage) {
        if (SystemTray.isSupported()) {
            Platform.setImplicitExit(false);
            primaryStage.setOnCloseRequest(value -> {
                primaryStage.hide();
                value.consume();
            });
            SystemTray tray = SystemTray.getSystemTray();
            java.awt.Image image = Toolkit.getDefaultToolkit().getImage(imageURL);
            PopupMenu popup = new PopupMenu();
            MenuItem showItem = new MenuItem("Show");
            MenuItem exitItem = new MenuItem("Exit");

            popup.add(showItem);
            popup.add(exitItem);

            TrayIcon trayIcon = new TrayIcon(image, StormHandler.getApplicationName(), popup);
            trayIcon.setImageAutoSize(true);

            ActionListener showListener = event -> Platform.runLater(() -> {
                primaryStage.show();
                primaryStage.toFront();
            });


            trayIcon.addActionListener(showListener);
            showItem.addActionListener(showListener);
            exitItem.addActionListener(event -> {
                Platform.exit();
                System.exit(0);
            });

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }


}

@@ -791,22 +791,55 @@
 t.event.
-Action
+MouseEvent;%0Aimport java.awt.event.Mouse
 Listener
@@ -1461,24 +1461,80 @@
 loaderFX%22);%0A
+        StormHandler stormHandler = new StormHandler();%0A
         addT
@@ -1557,16 +1557,30 @@
 aryStage
+, stormHandler
 );%0A%0A    
@@ -1777,64 +1777,8 @@
 );%0A%0A
-        StormHandler stormHandler = new StormHandler();%0A
     
@@ -2814,16 +2814,49 @@
 aryStage
+, final StormHandler stormHandler
 ) %7B%0A    
@@ -3369,24 +3369,183 @@
 m(%22Exit%22);%0A%0A
+            Runnable openAction = () -%3E Platform.runLater(() -%3E %7B%0A                primaryStage.show();%0A                primaryStage.toFront();%0A            %7D);%0A
             
@@ -3752,30 +3752,41 @@
         
-Action
+trayIcon.addMouse
 Listener
  showLis
@@ -3781,21 +3781,22 @@
 ener
- show
+(mouse
 Listener
  = e
@@ -3795,246 +3795,83 @@
 ener
- = event -%3E Platform.runLater(() -%3E %7B%0A                primaryStage.show();%0A                primaryStage.toFront();%0A            %7D);%0A%0A%0A            trayIcon.addActionListener(showListener);%0A            showItem.addActionListener(showListener
+(openAction));%0A            showItem.addActionListener(e -%3E openAction.run()
 );%0A 
@@ -4162,11 +4162,724 @@
 %0A    %7D%0A%0A
+    private MouseListener mouseListener(final Runnable result) %7B%0A        return new MouseListener() %7B%0A            @Override%0A            public void mouseClicked(final MouseEvent e) %7B%0A                if (e.getButton() == MouseEvent.BUTTON1) %7B%0A                    result.run();%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void mousePressed(final MouseEvent e) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void mouseReleased(final MouseEvent e) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void mouseEntered(final MouseEvent e) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void mouseExited(final MouseEvent e) %7B%0A%0A            %7D%0A        %7D;%0A    %7D%0A
 %0A%7D%0A

make single left click open window