/*******************************************************************************
 * Copyright (c) 2015 IBH SYSTEMS GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package de.dentrassi.pm.common.web.menu;

import com.google.common.html.HtmlEscapers;

import de.dentrassi.osgi.web.LinkTarget;
import de.dentrassi.pm.common.web.Modifier;
import de.dentrassi.pm.common.web.menu.Modal.Button;
import de.dentrassi.pm.common.web.menu.Modal.ButtonFunction;

/**
 * A menu entry from an extension point
 */
public class MenuEntry implements Comparable<MenuEntry>
{
    private String category;

    private String label;

    private LinkTarget target;

    private Modifier modifier;

    private int categoryOrder;

    private int entryOrder;

    private boolean newWindow;

    private String icon;

    private Modal modal;

    public MenuEntry ( final String category, final int categoryOrder, final String label, final int entryOrder, final LinkTarget target, final Modifier modifier, final String icon )
    {
        this ( category, categoryOrder, label, entryOrder, target, modifier, icon, false );
    }

    public MenuEntry ( final String category, final int categoryOrder, final String label, final int entryOrder, final LinkTarget target, final Modifier modifier, final String icon, final boolean newWindow )
    {
        this.category = category;
        this.categoryOrder = category != null ? categoryOrder : entryOrder;
        this.label = label;
        this.entryOrder = category != null ? entryOrder : 0;
        this.target = target;
        this.modifier = modifier;
        this.icon = icon;
        this.newWindow = newWindow;
    }

    public MenuEntry ( final String label, final int entryOrder, final LinkTarget target, final Modifier modifier, final String icon )
    {
        this.category = null;
        this.categoryOrder = entryOrder;
        this.label = label;
        this.entryOrder = 0;
        this.target = target;
        this.modifier = modifier;
        this.icon = icon;
        this.newWindow = false;
    }

    public String getIcon ()
    {
        return this.icon;
    }

    public boolean isNewWindow ()
    {
        return this.newWindow;
    }

    public String getCategory ()
    {
        return this.category;
    }

    public int getCategoryOrder ()
    {
        return this.categoryOrder;
    }

    public int getEntryOrder ()
    {
        return this.entryOrder;
    }

    public String getLabel ()
    {
        return this.label;
    }

    public LinkTarget getTarget ()
    {
        return this.target;
    }

    public Modifier getModifier ()
    {
        return this.modifier;
    }

    protected String getMainLabel ()
    {
        if ( this.category != null )
        {
            return this.category;
        }
        return this.label;
    }

    public MenuEntry setCategory ( final String category )
    {
        this.category = category;
        return this;
    }

    public MenuEntry setLabel ( final String label )
    {
        this.label = label;
        return this;
    }

    public MenuEntry setTarget ( final LinkTarget target )
    {
        this.target = target;
        return this;
    }

    public MenuEntry setModifier ( final Modifier modifier )
    {
        this.modifier = modifier;
        return this;
    }

    public MenuEntry setCategoryOrder ( final int categoryOrder )
    {
        this.categoryOrder = categoryOrder;
        return this;
    }

    public MenuEntry setEntryOrder ( final int entryOrder )
    {
        this.entryOrder = entryOrder;
        return this;
    }

    public MenuEntry setNewWindow ( final boolean newWindow )
    {
        this.newWindow = newWindow;
        return this;
    }

    public MenuEntry setIcon ( final String icon )
    {
        this.icon = icon;
        return this;
    }

    public MenuEntry setModal ( final Modal modal )
    {
        this.modal = modal;
        return this;
    }

    public Modal getModal ()
    {
        return this.modal;
    }

    public MenuEntry makeModalMessage ( final String title, final String message )
    {
        this.modal = new Modal ( title, new Button ( ButtonFunction.CLOSE, "Close" ), new Button ( ButtonFunction.SUBMIT, this.label, this.icon, this.modifier ) );
        this.modal.setBody ( "<p>" + HtmlEscapers.htmlEscaper ().escape ( message ) + "</p>" );
        return this;
    }

    @Override
    public int compareTo ( final MenuEntry o )
    {
        // first by category
        int rc = Integer.compare ( this.categoryOrder, o.categoryOrder );
        if ( rc != 0 )
        {
            return rc;
        }

        // then by entry
        rc = Integer.compare ( this.entryOrder, o.entryOrder );
        if ( rc != 0 )
        {
            return rc;
        }

        // last by label
        return getMainLabel ().compareTo ( o.getMainLabel () );
    }

}

@@ -5246,10 +5246,205 @@
 %0A    %7D%0A%0A
+    @Override%0A    public String toString ()%0A    %7B%0A        return String.format ( %22%5BMenu: %25s@%25s/%25s@%25s - %25s%5D%22, this.category, this.categoryOrder, this.label, this.entryOrder, this.target );%0A    %7D%0A%0A
 %7D%0A

add toString method