/*
 * Copyright 2015-2017 Matthew Aguirre
 *
 * 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 org.tros.torgo;

import java.awt.Window;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * Main entry point for torgo
 *
 * @author matta
 */
public class Main {

    private static final String DEFAULT_LANGUAGE = "dynamic-logo";

    /**
     * Entry Point
     *
     * @param args
     */
    public static void main(String[] args) {
        //initialize the logging
        MainSplash.splashInit();
        org.tros.utils.logging.Logging.initLogging(TorgoInfo.INSTANCE);
        org.tros.utils.logging.Logger logger = org.tros.utils.logging.Logging.getLogFactory().getLogger(Main.class);
        Options options = new Options();
        options.addOption("l", "lang", true, "Open using the desired language. [default is 'logo']");
        options.addOption("i", "list", false, "List available languages.");
        String lang = DEFAULT_LANGUAGE;
        final String fileArgument = args.length - 1 >= 0 ? args[args.length - 1] : null;
        String ext = null;
        boolean quit = false;
        if (fileArgument != null) {
            int index = fileArgument.lastIndexOf('.');
            if (index >= 0) {
                ext = fileArgument.substring(index + 1);
            }
        }

        boolean customLangUsed = false;
        try {
            CommandLineParser parser = new org.apache.commons.cli.DefaultParser();
            CommandLine cmd = parser.parse(options, args);
            if (cmd.hasOption("lang") || cmd.hasOption("l")) {
                lang = cmd.getOptionValue("lang");
                customLangUsed = true;
            } else if (cmd.hasOption("i") || cmd.hasOption("list")) {
                Set<String> toolkits = TorgoToolkit.getToolkits();
                toolkits.forEach((name) -> {
                    System.out.println(name);
                });
                //will force an exit
                lang = null;
                quit = true;
            }
        } catch (ParseException ex) {
            logger.fatal(null, ex);
        }

        //currently commented out for working with snapd
        if (System.getProperty("swing.defaultlaf") == null) {
            try {
                //set look and feel (laf) to that of the system.
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                logger.fatal(null, ex);
            }
        }

        java.util.prefs.Preferences prefs = java.util.prefs.Preferences.userNodeForPackage(TorgoToolkit.class);
        if (!customLangUsed) {
            lang = prefs.get("lang", lang);
        }
        if (ext != null && TorgoToolkit.getToolkits().contains(ext)) {
            lang = ext;
        }
        if (!TorgoToolkit.getToolkits().contains(lang)) {
            logger.warn("Could not load: {0}", lang);
            logger.warn("Loading Default: {0}", DEFAULT_LANGUAGE);
            lang = DEFAULT_LANGUAGE;
        }
        final String controlLang = lang;

        if (!quit) {
            prefs.put("lang", lang);
            SwingUtilities.invokeLater(() -> {
                Controller controller = TorgoToolkit.getController(controlLang);
                if (controller != null) {
                    controller.run();
                    if (fileArgument != null) {
                        controller.openFile(new File(fileArgument));
                    } else {
                        controller.newFile();
                    }
                }
            });
        }
    }

    public static final String IMAGE_ICON_CLASS_PATH = "torgo-48x48.png";

    public static void loadIcon(Window frame) {
        frame.setIconImage(getIcon().getImage());
    }

    public static ImageIcon getIcon() {
        return getIcon(IMAGE_ICON_CLASS_PATH);
    }

    public static ImageIcon getIcon(String path) {
        try {
            java.util.Enumeration<URL> resources = ClassLoader.getSystemClassLoader().getResources(path);
            ImageIcon ico = new ImageIcon(resources.nextElement());
            return ico;
        } catch (NoSuchElementException | IOException ex) {
            org.tros.utils.logging.Logging.getLogFactory().getLogger(Main.class).warn(null, ex);
        }
        return null;
    }
}

@@ -1102,16 +1102,50 @@
 ception;
+%0Aimport org.tros.utils.ImageUtils;
 %0A%0A/**%0A *
@@ -4969,398 +4969,53 @@
     
-try %7B%0A            java.util.Enumeration%3CURL%3E resources = ClassLoader.getSystemClassLoader().getResources(path);%0A            ImageIcon ico = new ImageIcon(resources.nextElement());%0A            return ico;%0A        %7D catch (NoSuchElementException %7C IOException ex) %7B%0A            org.tros.utils.logging.Logging.getLogFactory().getLogger(Main.class).warn(null, ex);%0A        %7D%0A        return null
+return ImageUtils.createImageIcon(path, path)
 ;%0A  

Load IconImage using ImageUtils