package nl.paulinternet.gtasaveedit.view;

import nl.paulinternet.gtasaveedit.view.updater.Updater;
import nl.paulinternet.gtasaveedit.view.window.AboutWindow;
import nl.paulinternet.gtasaveedit.view.window.ExceptionDialog;
import nl.paulinternet.gtasaveedit.view.window.MainWindow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    private static final Logger log = LoggerFactory.getLogger(Main.class);

    public static final boolean WINDOWS = System.getProperty("os.name").toLowerCase().startsWith("windows");
    public static final boolean MAC = System.getProperty("os.name").toLowerCase().startsWith("mac");
    public static final boolean LINUX = System.getProperty("os.name").toLowerCase().startsWith("linux");

    public static void main(String[] args) {
        try {

            // setup look and feel
            try {
                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("GTK+".equals(info.getName()) || "Windows".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                log.error("Unable to set theme!", ex);
            }

            // OS X specific
            if (MAC) {
                System.setProperty("apple.laf.useScreenMenuBar", "true");
                System.setProperty("apple.awt.application.name", "GTA:SA Savegame Editor");
                System.setProperty("com.apple.mrj.application.apple.menu.about.name", "GTA:SA Savegame Editor");
                Taskbar.getTaskbar().setIconImage(Images.readImage("icon-256.png"));

                Desktop.getDesktop().setPreferencesHandler(pe -> MainWindow.getInstance().getTabbedPane().onShowPreferences());
                Desktop.getDesktop().setAboutHandler(aboutEvent -> AboutWindow.get().setVisible(true));
            }

            // Set the icons
            List<Image> images = new ArrayList<>(Arrays.asList(Images.readImage("icon-16.png"),
                    Images.readImage("icon-32.png"),
                    Images.readImage("icon-48.png")));
            MainWindow.getInstance().setIconImages(images);

            // Create GUI
            GUICreator guiCreator = new GUICreator();
            SwingUtilities.invokeAndWait(guiCreator);
            SwingUtilities.invokeAndWait(guiCreator); // not a typo, it has to be invoked twice.

            // Load images
            Images.loadImages();

            Updater.start();
        } catch (Throwable e) {
            e.printStackTrace();
            new ExceptionDialog(e).setVisible(true);
        }
    }
}

@@ -970,138 +970,42 @@
  // 
-setup look and feel%0D%0A            try %7B%0D%0A                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()
+OS X specific%0D%0A            if (MAC
 ) %7B%0D
@@ -1025,492 +1025,104 @@
     
-    
-if (%22GTK+%22.equals(info.getName()) %7C%7C %22Windows%22.equals(info.getName())) %7B%0D%0A                        UIManager.setLookAndFeel(info.getClassName());%0D%0A                        break;%0D%0A                    %7D%0D%0A                %7D%0D%0A            %7D catch (ClassNotFoundException %7C InstantiationException %7C IllegalAccessException %7C UnsupportedLookAndFeelException ex) %7B%0D%0A                log.error(%22Unable to set the
+System.setProperty(%22com.apple.mrj.application.apple.menu.about.na
 me
-!
 %22, 
-ex);%0D%0A            %7D%0D%0A%0D%0A            // OS X specific%0D%0A            if (MAC) %7B
+%22GTA:SA Savegame Editor%22);
 %0D%0A  
@@ -1291,122 +1291,8 @@
 );%0D%0A
-                System.setProperty(%22com.apple.mrj.application.apple.menu.about.name%22, %22GTA:SA Savegame Editor%22);%0D%0A
     
@@ -1622,24 +1622,622 @@
        %7D%0D%0A%0D%0A
+            // setup look and feel%0D%0A            try %7B%0D%0A                for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) %7B%0D%0A                    if (%22GTK+%22.equals(info.getName()) %7C%7C %22Windows%22.equals(info.getName())) %7B%0D%0A                        UIManager.setLookAndFeel(info.getClassName());%0D%0A                        break;%0D%0A                    %7D%0D%0A                %7D%0D%0A            %7D catch (ClassNotFoundException %7C InstantiationException %7C IllegalAccessException %7C UnsupportedLookAndFeelException ex) %7B%0D%0A                log.error(%22Unable to set theme!%22, ex);%0D%0A            %7D%0D%0A%0D%0A
             

fix macOS application name