/*
 *  Keyframe-Based Video Summarization Designer
 *  Copyright (C) 2014 Waltenberger Andreas
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package at.ac.tuwien.ims.layout;

import at.ac.tuwien.ims.event.ZoomEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;

/**
 * This class represents the Template defined by the XML-Template files
 * Each Template can contain multiple sub-templates which correspond to sub-tiles in the layout
 * @author Andreas Waltenberger
 *
 */
public class Template extends FlowPane {

	double scalewidth;

	public Template(int width) {
		scalewidth = width * 0.1;

		setPrefWrapLength(width);
		setBackground(new Background(new BackgroundFill(Color.GRAY, null, null)));

		// if the Template gets zoomed in we have to set the new width of the template, each tile and sub-template
		addEventHandler(ZoomEvent.ZOOM_IN, new EventHandler<ZoomEvent>() {
			@Override
			public void handle(ZoomEvent event) {
				setPrefWrapLength(getPrefWrapLength() + scalewidth);
				for (Node n : getChildren()) {
					if (n instanceof Tile)
						Event.fireEvent(n, event);
					else
						Event.fireEvent(n, new ZoomEvent(ZoomEvent.ZOOM_IN));
				}
				event.consume();
			}
		});

		addEventHandler(ZoomEvent.ZOOM_OUT, new EventHandler<ZoomEvent>() {
			@Override
			public void handle(ZoomEvent event) {
				setPrefWrapLength(getPrefWrapLength() - scalewidth);
				for (Node n : getChildren()) {
					if (n instanceof Tile)
						Event.fireEvent(n, event);
					else
						Event.fireEvent(n, new ZoomEvent(ZoomEvent.ZOOM_OUT));
				}
				event.consume();
			}
		});
	}

	public void setColor(Color value) {
		for(Node n : getChildren()) {
			if(n instanceof Tile)
				((Tile) n).setBorder(new Border(new BorderStroke(value, BorderStrokeStyle.SOLID, null, new BorderWidths(3))));
			else
				((Template) n).setColor(value);
		}
	}
}
