package org.shortpasta.smartgwttest.portal.client.layout;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ImageStyle;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * ShortPasta Foundation
 * http://www.shortpasta.org
 * Copyright 2009 and beyond, Sal Ingrilli at the ShortPasta Software Foundation
 * <p/>
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation as long as:
 * 1. You credit the original author somewhere within your product or website
 * 2. The credit is easily reachable and not burried deep
 * 3. Your end-user can easily see it
 * 4. You register your name (optional) and company/group/org name (required)
 * at http://www.shortpasta.org
 * 5. You do all of the above within 4 weeks of integrating this software
 * 6. You contribute feedback, fixes, and requests for features
 * <p/>
 * If/when you derive a commercial gain from using this software
 * please donate at http://www.shortpasta.org
 * <p/>
 * If prefer or require, contact the author specified above to:
 * 1. Release you from the above requirements
 * 2. Acquire a commercial license
 * 3. Purchase a support contract
 * 4. Request a different license
 * 5. Anything else
 * <p/>
 * This software 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, similarly
 * to how this is described in the GNU Lesser General Public License.
 * <p/>
 * User: sal
 * Date: 5/6/2017 12:46 PM
 */
public class HeaderIconAlignment implements EntryPoint {

    /**
     * The EntryPoint interface
     * Upgrading SmartGWT from 4.0-2013-11-30 to 5.0-2015-06-25
     * On FF19 the following shows only the "A" item
     * On Chome 43 and IE9 it properly shows all items
     */
    public void onModuleLoad () {

        // Using:
        // GWT: 2.8.1
        // SmartGWT: 6.0p-2017-05-04
        // Chrome: 58.0.3029.96 (64-bit)
        // Java: 1.8.0_111-b14

        // layout
        final HLayout layout = new HLayout ();
        layout.setMembersMargin (10);
        layout.addMember (createNode ("/images/icons/login_64x64.png", "LOGIN"));
        layout.addMember (createNode ("/images/icons/register_64x64.png", "COLLECTORS"));
        layout.addMember (createNode ("/images/icons/register_64x64.png", "COLLECTORSXXXXX"));
        draw (layout);
    }

    /**
     * Adds the given button to this layout
     * @param iconSrc
     * @param title
     * @return Node
     */
    private VLayout createNode (final String iconSrc, final String title) {

        // image
        final Img img = new Img ();
        img.setWidth (48);
        img.setHeight (48);
        img.setImageType (ImageStyle.STRETCH);
        img.setSrc (iconSrc);
        img.setBackgroundColor ("red");

        // label
        final Label label = new Label ();
        label.setWrap (false);
        label.setAutoHeight ();
        label.setContents (title);
        label.setTooltip (title);
        label.setBackgroundColor ("green");

        // layout
        // ify ou specified the width, set it, otherwise compact it
        final VLayout layout = new VLayout ();
        layout.setAutoWidth ();
        layout.setAutoHeight ();
        layout.setDefaultLayoutAlign (Alignment.CENTER);
        layout.setDefaultLayoutAlign (VerticalAlignment.CENTER);
        layout.setBackgroundColor ("blue");
        layout.setTooltip (title);
        layout.addMember (img);
        layout.addMember (label);

        // done
        return layout;
    }

    ////////////////////////////////////////////////////////////////////////////////
    /// The following methods are here so that:
    /// 1. MainLayout can embed and execute this test
    /// 2. We can post a standalone EntryPoint test-case to SmartGWT
    ////////////////////////////////////////////////////////////////////////////////

    // my attributes
    private VLayout parentLayout;

    /**
     * The EntryPoint interface
     * Overload of the EntryPoint interface so that MainLayout can specify the parent layout
     * @param parentLayout
     */
    public final void onModuleLoad (final VLayout parentLayout) {

        // track and delegate
        this.parentLayout = parentLayout;
        onModuleLoad ();
    }

    /**
     * Shows the given Canvas
     * @param canvas
     */
    private void draw (final Canvas canvas) {

        // layout
        if (parentLayout != null) {
            parentLayout.addMember (canvas);
        }
        else {
            canvas.draw ();
        }
    }
}