/*
 *  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.ui;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;

/**
 * UI element displayed in each Tile with an image to add zoom and flip functionality
 * @author Andreas Waltenberger
 *
 */
public class ZoomBox extends HBox {

	private Button minus;
	private Button plus;
	private Button flip;
	private TextField label;
	private ImageView image;

	private double scalewidth;
	private double scaleheight;

	private int percent;

	public ZoomBox(ImageView i, double w, double h) {
		image = i;
		scalewidth = w * 0.1;
		scaleheight = h * 0.1;
		percent = 100;

		setPadding(new Insets(5));

		label = new TextField(percent + "%");
		label.setPrefColumnCount(3);
		label.setEditable(false);

		minus = new Button("-");
		minus.setPrefSize(30, 20);
		minus.setDisable(true);
		minus.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {

				// down-scales the image by 10 percent
				if (image.getFitHeight() == 0) {
					image.setFitWidth(image.getFitWidth() - scalewidth);
				} else {
					image.setFitHeight(image.getFitHeight() - scaleheight);
				}

				// resets the clip and image location
				Rectangle clip = (Rectangle) image.getClip();
				if (image.getScaleX() == -1) {
					image.setTranslateX(0);
					clip.setX(image.getLayoutBounds().getWidth() - clip.getWidth());
				} else {
					image.setTranslateX(0);
					clip.setX(0);
				}
				image.setTranslateY(0);
				clip.setY(0);

				percent -= 10;
				label.setText(percent + "%");

				if (percent == 100)
					minus.setDisable(true);

				event.consume();
			}
		});

		plus = new Button("+");
		plus.setPrefSize(30, 20);
		plus.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				minus.setDisable(false);

				// up-scales the image by 10 percent
				if (image.getFitHeight() == 0) {
					image.setFitWidth(image.getFitWidth() + scalewidth);
				} else {
					image.setFitHeight(image.getFitHeight() + scaleheight);
				}

				// resets the clip and image location
				Rectangle clip = (Rectangle) image.getClip();
				if (image.getScaleX() == -1) {
					image.setTranslateX(0);
					clip.setX(image.getLayoutBounds().getWidth() - clip.getWidth());
				} else {
					image.setTranslateX(0);
					clip.setX(0);
				}

				percent += 10;
				label.setText(percent + "%");

				event.consume();
			}
		});

		flip = new Button(">");
		flip.setPrefSize(30, 20);
		flip.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				// flips the image horizontally by setting the scale to -1
				image.setScaleX(image.getScaleX() * -1);
				// resets the clip and image location
				Rectangle clip = (Rectangle) image.getClip();
				if (image.getScaleX() == -1) {
					image.setTranslateX(0);
					clip.setX(image.getLayoutBounds().getWidth() - clip.getWidth());
				} else {
					image.setTranslateX(0);
					clip.setX(0);
				}
			}
		});

		getChildren().addAll(minus, label, plus, flip);
	}

	public Button getPlus() {
		return plus;
	}

	public Button getMinus() {
		return minus;
	}
}
