/*
 * SubMenuSkin.fx
 */

package cookery.control;

import javafx.scene.control.Skin;

import javafx.scene.paint.Color;

import javafx.scene.layout.Container;


/**
 * A standard visual representation of the sub menu control.
 * @author Nick Apperley
 */
package class SubMenuSkin extends Skin{
    public var fill = Color.ORANGE;
    def subMenu = bind (control as SubMenu);
    // The width of the widest menu item in the sub menu.
    def widestMenuItem: Number = bind getWidestMenuItem() on replace{
        for(menuItem in subMenu.menuItems){
            menuItem.width = widestMenuItem;
        }
    };

    package def menuItemsLayout = Container{
        visible: false
        content: bind subMenu.menuItems
        onMouseExited: function(me){(behavior as SubMenuBehavior).alterMenuItemVisibility()}
    };

    package function layoutMenuItems(): Void{
        var prevMenuItem: MenuItem;
        
        for(menuItem in subMenu.menuItems){
            if(prevMenuItem != null){
                menuItem.layoutY = prevMenuItem.layoutBounds.maxY;
            }
            prevMenuItem = menuItem;
        }
    }

    /**
     * Gets the width of the widest menu item.
     @return The width of the widest menu item.
    */
    function getWidestMenuItem(): Number{
        var widest: Number;

        for(menuItem in subMenu.menuItems){
            if((menuItem.textWidth + menuItem.contentWidth) > widest){
                widest = menuItem.textWidth + menuItem.contentWidth;
            }
        }
        return widest;
    }

    /**
     * Gets the height of the highest menu item.
     @return The height of the highest menu item.
    */
    function getHighestMenuItem(): Number{
        var highest: Number;

        for(menuItem in subMenu.menuItems){
            if(menuItem.layoutBounds.height > highest){
                highest = menuItem.layoutBounds.height;
            }
        }
        return highest;
    }

    init{
        behavior = SubMenuBehavior{};
    }

    public override function intersects(localX: Number, localY: Number,
        localWidth: Number, localHeight: Number): Boolean
    {
        return node.intersects(localX, localY, localWidth, localHeight);
    }

    public override function contains(localX: Number, localY: Number): Boolean
    {
        return node.contains(localX, localY);
    }
}
