/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. 
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Sun Microsystems nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package photoflockr;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.transform.Transform;
import javafx.scene.shape.Rectangle;

/**
 * This class is used to display a tag list.
 */
public class TagList extends CustomNode {
    public var textOpacity: Number;
    public var height: Number;
    public var width: Number;
    public var tags: String[];
    public var clickAction: function(tag: String): Void;

    var color = bind Color.rgb(150, 150, 150, textOpacity);
    var selectionColor = Color.WHITE;
    var font = Font { size: 14 };
    var listGroup: Group;
    var maxWidth: Number = 150;
    var scrollY: Number = 0;

    override public function create(): Node {
        Group {
            var margin = 5;
            var spacing = 6;
            clip: Rectangle {
                height: height
                width: width
            }
            content: [
                // The rectangle manages scrolling.
                Rectangle {
                    var h = bind sizeof tags * (font.size + spacing);
                    var dif = bind h - height
                    height: bind h
                    width: bind width;
                    fill: Color.color(0, 0, 0, 0);
                    smooth: false
                    onMouseWheelMoved: function(e) {
                        if ((scrollY >= 0 and e.wheelRotation < 0) or
                            (scrollY + dif <= 0 and e.wheelRotation > 0))
                        {
                            return;
                        }
                        if (scrollY - e.wheelRotation*5 > 0) {
                            scrollY = 0;
                        } else if (scrollY + dif - e.wheelRotation*5 < 0) {
                            scrollY = -dif;
                        } else {
                            scrollY -= e.wheelRotation*5;
                        }
                    }
                },
                // A list of tags.
                listGroup = Group {
                    translateY: bind scrollY
                    content: bind for (tag in tags)
                        Group {
                            translateY: indexof tag * (font.size + spacing)
                            var r: Rectangle;
                            content: [
                                // The rectangle manages tag selection.
                                r = Rectangle {
                                    height: 20
                                    width: 150
                                    smooth: false
                                    fill: Color.color(0, 0, 0, 0)
                                    onMouseClicked: function(e) {
                                        clickAction(tag);
                                    }
                                },
                                // A single tag.
                                Text {
                                    fill: bind if (r.hover) selectionColor else color
                                    translateX: 5
                                    translateY: 12
                                    content: tag
                                    font: font
                                }
                            ]
                        }
                }
            ]
        }
    }
}