/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ht.ui.svg;

import java.util.HashMap;
import java.util.Map;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.animation.TimelineBuilder;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.util.Duration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ht.data.RealTimeDataManager;
import com.ht.svg.view.SvgViewModel;
import com.ht.ui.MainApplication;
import com.ht.ui.monitor.TagVarMonitor;

/**
 * 
 * @author Administrator
 */
public class SVGViewerManager extends Region {
	private static final Logger log = LoggerFactory
			.getLogger(SVGViewerManager.class);

	private Group svgViewerContainer;
	private Map<SvgViewModel, SVGViewer> svgViewMap = new HashMap<SvgViewModel, SVGViewer>();
	private SVGViewer currentSVGViewer;

	private Region spacer = new Region();// 用于菜单显示
	private SVGViewNavigator viewNavigator;
	private ControlPanel controlPanel;

	public SVGViewerManager() {
		setVisible(false);

		// webEngine.executeScript("window.scrollTo(455,0); ");

		viewNavigator = new SVGViewNavigator(this);
		viewNavigator.setTranslateX(100);
		viewNavigator.setTranslateY(100);

		visibleProperty().addListener(new ChangeListener<Boolean>() {
			@Override
			public void changed(ObservableValue<? extends Boolean> observable,
					Boolean oldValue, Boolean newValue) {
				if (newValue) {
					viewNavigator.updateViewList();
					RealTimeDataManager.INSTANCE.updateAllVarValues();
				}
			}
		});

		HBox hb = createToolBar();

		// MoveScaleButton button = new MoveScaleButton() {
		//
		// @Override
		// void moveTop() {
		// currentSVGViewer.moveTop();
		// }
		//
		// @Override
		// void moveRight() {
		// currentSVGViewer.moveRight();
		// }
		//
		// @Override
		// void moveLeft() {
		// currentSVGViewer.moveLeft();
		// }
		//
		// @Override
		// void moveBottom() {
		// currentSVGViewer.moveBottom();
		// }
		//
		// @Override
		// void moveReset() {
		// currentSVGViewer.moveReset();
		// }
		// };
		// button.setTranslateY(200);
		// button.setTranslateX(100);

		svgViewerContainer = new Group();
		getChildren().addAll(spacer, svgViewerContainer, hb, viewNavigator);
	}

	private HBox createToolBar() {
		Button blinkingReset = new Button("闪烁复归");
		blinkingReset.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if (currentSVGViewer != null) {
					currentSVGViewer.getAnimateHandler().blinkingReset();
				}
			}
		});
		Button monitor = new Button("变量监测");
		monitor.setOnAction(new EventHandler<ActionEvent>() {
			private TagVarMonitor tagVarMonitor;

			@Override
			public void handle(ActionEvent event) {
				if (tagVarMonitor == null)
					tagVarMonitor = new TagVarMonitor();
				MainApplication.getMainApp().showModalMessage(tagVarMonitor);
			}
		});

		Button control = new Button("控制面板");
		control.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				if (controlPanel == null) {
					controlPanel = new ControlPanel();
				}
				MainApplication.getMainApp().showModalMessage(controlPanel);
			}
		});

		Button zoomIn = new Button();
		zoomIn.getStyleClass().add("image-button");
		zoomIn.setGraphic(new ImageView(getClass().getResource(
				"images/gnome_zoom_in.png").toExternalForm()));
		zoomIn.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.zoomIn();
			}
		});

		Button zoomOut = new Button();
		zoomOut.getStyleClass().add("image-button");
		zoomOut.setGraphic(new ImageView(getClass().getResource(
				"images/gnome_zoom_out.png").toExternalForm()));
		zoomOut.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.zoomOut();
			}
		});

		Button zoomFit = new Button();
		zoomFit.getStyleClass().add("image-button");
		zoomFit.setGraphic(new ImageView(getClass().getResource(
				"images/gnome_zoom_fit_best.png").toExternalForm()));
		zoomFit.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				// currentSVGViewer.zoomReset();
				currentSVGViewer.reset();
			}
		});

		Button right = new Button();
		right.getStyleClass().add("image-button");
		right.setGraphic(new ImageView(getClass().getResource(
				"images/arrow_right.png").toExternalForm()));
		right.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.moveRight();
			}
		});

		Button left = new Button();
		left.getStyleClass().add("image-button");
		ImageView arrowRight = new ImageView(getClass().getResource(
				"images/arrow_right.png").toExternalForm());
		arrowRight.setRotate(180);
		left.setGraphic(arrowRight);
		left.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.moveLeft();
			}
		});

		Button top = new Button();
		top.getStyleClass().add("image-button");
		ImageView arrowTop = new ImageView(getClass().getResource(
				"images/arrow_right.png").toExternalForm());
		arrowTop.setRotate(-90);
		top.setGraphic(arrowTop);
		top.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.moveTop();
			}
		});

		Button bottom = new Button();
		bottom.getStyleClass().add("image-button");
		ImageView arrowBottom = new ImageView(getClass().getResource(
				"images/arrow_right.png").toExternalForm());
		arrowBottom.setRotate(90);
		bottom.setGraphic(arrowBottom);
		bottom.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				currentSVGViewer.moveBottom();
			}
		});

		HBox hb = new HBox(10);
		hb.setId("svg-footer");
		StackPane.setAlignment(hb, Pos.BOTTOM_LEFT);
		hb.setMaxHeight(70);
		hb.setAlignment(Pos.CENTER_LEFT);
		hb.setTranslateY(Screen.getPrimary().getVisualBounds().getHeight() - 10);
		hb.getChildren().addAll(blinkingReset, monitor, control, zoomIn,
				zoomFit, zoomOut, left, top, bottom, right);
		return hb;
	}

	public void dispose() {
		for (SVGViewer viewer : svgViewMap.values()) {
			viewer.dispose();
		}
	}

	public void showView(SvgViewModel view) {

		SVGViewer svg = svgViewMap.get(view);
		if (svg == null) {
			svg = new SVGViewer();
			svgViewerContainer.getChildren().add(svg);
			svg.load(view);
			svgViewMap.put(view, svg);
		} else {
			svgViewerContainer.getChildren().clear();
			svgViewerContainer.getChildren().add(svg);
		}

		currentSVGViewer = svg;
	}

	public Region getSpacer() {
		return spacer;
	}
}
