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

import java.io.File;

import at.ac.tuwien.ims.event.ZoomEvent;
import at.ac.tuwien.ims.layout.Template;
import at.ac.tuwien.ims.ui.KeyframePane;
import at.ac.tuwien.ims.ui.LayoutPane;
import at.ac.tuwien.ims.util.ImageExporter;
import at.ac.tuwien.ims.util.TemplateChooser;
import at.ac.tuwien.ims.util.TemplateImporter;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextField;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;

/**
 * The main class for the Keyframe-Based Video Summarization Designer which assembles the entire UI
 * 
 * @author Andreas Waltenberger
 *
 */
public class Designer extends Application {

	private BorderPane root;
	private SplitPane split;
	private LayoutPane layout;
	private KeyframePane keyframes;
	private MenuBar menuBar;
	private Menu templateMenu;
	private Menu videoMenu;
	private Menu editMenu;
	private Menu exportMenu;
	private MenuItem chooseMenuItem;
	private MenuItem importMenuItem;
	private MenuItem openMenuItem;
	private MenuItem imageMenuItem;
	private Button minus;
	private Button plus;
	private TextField label;
	private HBox zoomBox;
	private CustomMenuItem zoomMenuItem;
	private CustomMenuItem colorMenuItem;
	private ColorPicker colorPicker;

	private FileChooser importer;
	private FileChooser opener;

	private TemplateImporter templateImporter;
	private TemplateChooser templateChooser;

	private ImageExporter imageExporter;

	// Directory that stores the XML-Template files
	public static final File templateDir = new File(System.getProperty("user.home") + File.separator + ".designer");

	@Override
	public void start(final Stage primaryStage) {
		if (!templateDir.exists())
			templateDir.mkdir();

		root = new BorderPane();
		split = new SplitPane();
		layout = new LayoutPane();
		keyframes = new KeyframePane();

		importer = new FileChooser();
		importer.getExtensionFilters().add(new ExtensionFilter("XML", "*.xml"));

		opener = new FileChooser();

		chooseMenuItem = new MenuItem("Choose ...");
		chooseMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				templateChooser = new TemplateChooser(primaryStage, layout);
				templateChooser.showAndWait();
				if (layout.getPercent() == 100) {
					minus.setDisable(true);
					label.setText(layout.getPercent() + "%");
					editMenu.setDisable(false);
					exportMenu.setDisable(false);
				}
			}
		});

		importMenuItem = new MenuItem("Import ...");
		importMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				File xml = importer.showOpenDialog(primaryStage);
				if (xml != null) {
					if (templateImporter.importTemplate(xml, layout)) {
						label.setText(layout.getPercent() + "%");
						editMenu.setDisable(false);
						exportMenu.setDisable(false);
					}
				}
			}
		});

		openMenuItem = new MenuItem("Open ...");
		openMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				File video = opener.showOpenDialog(primaryStage);
				if (video != null)
					keyframes.openVideo(video);
			}
		});

		templateMenu = new Menu("Template");
		templateMenu.getItems().addAll(chooseMenuItem, importMenuItem);

		videoMenu = new Menu("Video");
		videoMenu.getItems().add(openMenuItem);

		// Zoom in function for the Template
		minus = new Button("-");
		minus.setPrefSize(30, 20);
		minus.setDisable(true);
		minus.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				layout.getContent().setTranslateX(0);
				layout.getContent().setTranslateY(0);
				Event.fireEvent(layout.getContent(), new ZoomEvent(ZoomEvent.ZOOM_OUT));
				layout.decPercent();
				label.setText(layout.getPercent() + "%");
				if (layout.getPercent() == 100)
					minus.setDisable(true);
				event.consume();
			}
		});

		// Zoom out function for the Template
		plus = new Button("+");
		plus.setPrefSize(30, 20);
		plus.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				minus.setDisable(false);
				Event.fireEvent(layout.getContent(), new ZoomEvent(ZoomEvent.ZOOM_IN));
				layout.incPercent();
				label.setText(layout.getPercent() + "%");
				event.consume();
			}
		});

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

		zoomBox = new HBox();
		zoomBox.getChildren().addAll(minus, label, plus);
		zoomMenuItem = new CustomMenuItem(zoomBox);
		
		colorPicker = new ColorPicker(Color.GREY);
		colorPicker.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				((Template) layout.getContent()).setColor(colorPicker.getValue());
			}
		});
		colorMenuItem = new CustomMenuItem(colorPicker);
		colorMenuItem.setHideOnClick(false);

		editMenu = new Menu("Edit");
		editMenu.getItems().addAll(zoomMenuItem, colorMenuItem);
		editMenu.setDisable(true);

		imageMenuItem = new MenuItem("Image");
		imageMenuItem.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				WritableImage image = layout.getContent().snapshot(null, null);
				imageExporter = new ImageExporter(primaryStage, image);
				imageExporter.showAndWait();
				event.consume();
			}
		});

		exportMenu = new Menu("Export");
		exportMenu.getItems().addAll(imageMenuItem);
		exportMenu.setDisable(true);

		menuBar = new MenuBar();
		menuBar.getMenus().addAll(templateMenu, videoMenu, editMenu, exportMenu);

		templateImporter = new TemplateImporter();

		split.getItems().addAll(layout, keyframes);
		split.setDividerPosition(0, 1);

		root.setTop(menuBar);
		root.setCenter(split);

		Scene scene = new Scene(root, 1000, 500);
		primaryStage.setTitle("Keyframe-Based Video Summarization Designer");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(args);
	}
}
