// Copyright (C) 2009 Red Hat, Inc.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

package net.adoptopenjdk.icedteaweb.jnlp.element.information;

import net.adoptopenjdk.icedteaweb.i18n.Translator;
import net.sourceforge.jnlp.config.ConfigurationConstants;

public final class ShortcutDesc {
    public static final String SHORTCUT_ELEMENT = "shortcut";
    public static final String ONLINE_ATTRIBUTE = "online";
    public static final String INSTALL_ATTRIBUTE = "install";

    public static final String DESKTOP_ELEMENT = "desktop";
    public static final String MENU_ELEMENT = "menu";


    // TODO: missing the shortcut attribute "install" (defined since spec version 7.0)

    /** Never create a shortcut */
    public static final String CREATE_NEVER = "NEVER";
    /** Always create a shortcut */
    public static final String CREATE_ALWAYS = "ALWAYS";
    /** Always ask user whether to create a shortcut */
    public static final String CREATE_ASK_USER = "ASK_USER";
    /** Ask user whether to create a shortcut but only if jnlp file asks for it */
    public static final String CREATE_ASK_USER_IF_HINTED = "ASK_IF_HINTED";
    /** Create a desktop shortcut without prompting if the jnlp asks for it */
    public static final String CREATE_ALWAYS_IF_HINTED = "ALWAYS_IF_HINTED";

    /**
     * The optional install attribute can be used in a shortcut element to describe the application's preference
     * for being considered "installed". If the value is "true" the application prefers to be considered installed.
     * The default value of the install attribute is "false".
     *
     * It is up to the JNLP Client to determine how and if applications are treated differently when install="true",
     * and this may vary from platform to platform. For example, a JNLP client on a Windows platforms may add the
     * application to the Windows Add/Remove menu only when install="true" is specified, while making no
     * distinction on unix platforms.
     */
    private boolean install = false;

    /**
     * the application wants to be placed on the desktop 
     * based of existence/not existence of desktop tag
     */
    
    private final boolean onDesktop;

    /** 
     * the application needs to be launched online 
     * happily ignored. itw is trying to run any app even if it is offline (some apps are enforcing it without sense)
     */
    private final boolean requiresOnline;

    /** 
     * the menu descriptor 
     * based on existence of menu tag
     * if null, then no tag was presented
     * if there is some value, then menu tag was presented
     * depending on value inside MenuDesc, the attribute submenu was/was not presented
     */
    private MenuDesc menu = null;

    /**
     * Create a new Shortcut descriptor
     * @param requiresOnline whether the shortcut requires connectivity
     * @param installed the application's preference for being considered installed
     * @param onDesktop whether the shortcut wants to be placed on the desktop
     */
    public ShortcutDesc(boolean requiresOnline, boolean installed, boolean onDesktop) {
        this.requiresOnline = requiresOnline;
        this.install = installed;
        this.onDesktop = onDesktop;
    }

    /**
     * @return whether the application prefers being considered installed.
     */
    public boolean isInstall() {
        return install;
    }

    /**
     * @return whether the shortcut requires being online
     */
    public boolean isOnline() {
        throw new RuntimeException("icedtea-web is not saving  online-enforcing attribute. See Xoffline implementations if needed.");
    }
     /**
      * For testing purposes. Verify if it have been parsed out correctly.
     * @return whether the shortcut requires being online.
     */
    public boolean isOnlineValue() {
        return requiresOnline;
    }

    /**
     * @return whether the shortcut should be placed on the desktop
     */
    public boolean onDesktop() {
        return onDesktop;
    }
    
     /**
     * @return whether the shortcut should be placed to the menus
     */
    public boolean toMenu() {
        return getMenu() != null;
    }

    /**
     * Add a shortcut to the 'start menu'
     * (whatever that means on gnome/kde/other ...)
     * @param menu if/what menu this shortcut should be added to
     */
    public void setMenu(final MenuDesc menu) {
        this.menu = menu;
    }

    /**
     * @return the menu this shortcut should be added to
     */
    public MenuDesc getMenu() {
        return menu;
    }

    public static String deploymentJavawsShortcutToString(final String i) {
        switch (i) {
            case ShortcutDesc.CREATE_NEVER:
                return Translator.R("DSPNeverCreate");
            case ShortcutDesc.CREATE_ALWAYS:
                return Translator.R("DSPAlwaysAllow");
            case ShortcutDesc.CREATE_ASK_USER:
                return Translator.R("DSPAskUser");
            case ShortcutDesc.CREATE_ASK_USER_IF_HINTED:
                return Translator.R("DSPAskIfHinted");
            case ShortcutDesc.CREATE_ALWAYS_IF_HINTED:
                return Translator.R("DSPAlwaysIfHinted");

        }
        throw new RuntimeException("Unknown value of " + ConfigurationConstants.KEY_CREATE_DESKTOP_SHORTCUT + " for " + i);

    }
}
