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

import java.util.List;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.Accordion;
import javafx.scene.control.ListView;
import javafx.scene.control.TitledPane;
import javafx.scene.input.MouseEvent;

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

import com.ht.svg.view.SvgViewModel;
import com.ht.ui.MainApplication;
import com.ht.ui.util.MessageConstants;
import com.ht.web.hessian.HessianFactoryManger;

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

	private double mouseDragOffsetX = 0;
	private double mouseDragOffsetY = 0;
	private double translateX = 0;
	private double translateY = 0;

	private SVGViewerManager svgViewer;
	private ObservableList<SvgViewModel> viewList = FXCollections
			.observableArrayList();

	public SVGViewNavigator(SVGViewerManager svgViewer) {

		this.svgViewer = svgViewer;

		setId("SVGViewNavigator");
		setMinSize(200, 400);
		setMaxSize(400, 800);
		setOpacity(0.5);
		setCache(true);

		final ListView<SvgViewModel> listView = new ListView<SvgViewModel>();
		listView.setItems(viewList);
		listView.getSelectionModel().selectedItemProperty()
				.addListener(new ChangeListener<SvgViewModel>() {
					@Override
					public void changed(
							ObservableValue<? extends SvgViewModel> observable,
							SvgViewModel oldValue, SvgViewModel newValue) {

						SVGViewNavigator.this.svgViewer.showView(newValue);
					}
				});

		final TitledPane titlePane = new TitledPane("视图列表", listView);
		titlePane.setId("TitledPane");
		titlePane.setAnimated(false);

		titlePane.setOnMousePressed(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				mouseDragOffsetX = event.getSceneX();
				mouseDragOffsetY = event.getSceneY();
				translateX = SVGViewNavigator.this.getLayoutX();
				translateY = SVGViewNavigator.this.getLayoutY();
			}
		});
		titlePane.setOnMouseDragged(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {

				double offsetX = event.getScreenX() - mouseDragOffsetX;
				double offsetY = event.getScreenY() - mouseDragOffsetY;
				if (offsetX > 2 || offsetX < -2 || offsetY > 2 || offsetY < -2) {

					translateY = translateY + event.getScreenY()
							- mouseDragOffsetY;
					if (translateY < 0) {
						translateY = 0;
					}
					translateX = translateX + event.getScreenX()
							- mouseDragOffsetX;

					SVGViewNavigator.this.setLayoutX(translateX);
					SVGViewNavigator.this.setLayoutY(translateY);

					mouseDragOffsetX = event.getSceneX();
					mouseDragOffsetY = event.getSceneY();
				}
			}
		});

		titlePane.setOnMouseEntered(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				SVGViewNavigator.this.setOpacity(1);
			};
		});

		titlePane.setOnMouseExited(new EventHandler<MouseEvent>() {
			public void handle(MouseEvent event) {
				SVGViewNavigator.this.setOpacity(.5);
			};
		});

		getPanes().add(titlePane);

	}

	public void updateViewList() {
		if (viewList.isEmpty()) {
			log.debug("更新索引列表");

			try {
				viewList.clear();
				List<SvgViewModel> list = HessianFactoryManger.INSTANCE
						.getSvgService().getSVGViewModelList();
				viewList.addAll(list);
			} catch (Exception e) {
				e.printStackTrace();
				MainApplication.getMainApp().showMessage(
						MessageConstants.ERROR_NO_RESPONSE);
			}
		}
	}

}
