package com.ht.svg.action;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.MouseEvent;

import com.ht.data.script.ScriptUtil;
import com.ht.ui.MainApplication;
import com.ht.ui.controls.window.MessageDialog;
import java.util.logging.Level;
import javax.script.*;

/**
 * the class of the login command
 *
 * @author ITRIS, Jordi SUC
 */
public class ShowMenuAction extends AbstractActionListener {

	private static final Logger log = LoggerFactory.getLogger(ShowMenuAction.class);
	private ContextMenu popupMenu;

	/**
	 * the constructor of the class
	 *
	 * @param actionElement the dom element defining the action
	 */
	public ShowMenuAction(Element actionElement) {

		super(actionElement);
		// if (getEventType() != null) {
		// parentElement.setAttribute("on" + getEventType(), "alert('" + this.id
		// + ",'+evt.screenX+','+evt.screenY);");
		// }

		popupMenu = new ContextMenu();
		popupMenu.setAutoHide(true);

		initializeAction();
	}

	/**
	 * initializes the action
	 */
	private void initializeAction() {

		for (Node cur = actionElement.getFirstChild(); cur != null; cur = cur.getNextSibling()) {

			if (cur instanceof Element) {

				Element childElement = (Element) cur;
				final RTDAMenuItemModel itemModel = new RTDAMenuItemModel(
						childElement);

				MenuItem item = new MenuItem(
						childElement.getAttribute("itemLabel"));
				item.setOnAction(new EventHandler<ActionEvent>() {

					public void handle(ActionEvent t) {
						itemModel.performAction();
					}
				});

				popupMenu.getItems().add(item);
			}
		}

		// initializeAuthorizationTag();
	}

	@Override
	public void performAction(Event event) {
		log.info("显示菜单");
		final MouseEvent mouseEvt = (MouseEvent) event;
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				// popupMenu.show(MainApplication.getMainApp().getSvgViewer(),
				// Double.parseDouble(array[1]), Double.parseDouble(array[2]));
				popupMenu.show(MainApplication.getMainApp().getSvgViewer().getSpacer(), mouseEvt.getScreenX(),
						mouseEvt.getScreenY());
			}
		});
	}

	public static class RTDAMenuItemModel {

		private String confirmationMessage;
		private CompiledScript compiledScript = null;
		private String script;

		public RTDAMenuItemModel(Element menuItemElement) {

			confirmationMessage = menuItemElement.getAttribute(AbstractActionListener.confirmationDialogAtt);

			script = menuItemElement.getTextContent();
			if (ScriptUtil.INSTANCE.getEngine() instanceof Compilable) {
				if (script != null && !script.isEmpty()) {

					Compilable compilable = (Compilable) ScriptUtil.INSTANCE.getEngine();
					try {
						compiledScript = compilable.compile(script);
					} catch (ScriptException ex) {
						// java.util.logging.Logger.getLogger(ScriptHandler.class.getName()).log(Level.SEVERE,
						// null, ex);
						ex.printStackTrace();
						log.error("编译菜单动作脚本出错", ex);
					}
				}
			}

		}

		// 执行操作
		public void performAction() {
			log.info("执行菜单项操作");
			if (confirmationMessage != null && !confirmationMessage.isEmpty()) {

				MessageDialog.buildConfirmation("提示",
						confirmationMessage,
						new EventHandler<javafx.event.Event>() {

							@Override
							public void handle(javafx.event.Event event) {
								exeuteScript();
							}
						}, null);
			} else {
				exeuteScript();
			}
		}

		private void exeuteScript() {
			try {
				ScriptUtil.INSTANCE.getEngine().eval("Packages.com.ht.ui.MainApplication.getMainApp().showMessage(\"========\");");
			} catch (ScriptException ex) {
				java.util.logging.Logger.getLogger(ShowMenuAction.class.getName()).log(Level.SEVERE, null, ex);
			}
//			if (compiledScript != null) {
//
//				try {
//					compiledScript.eval();
//				} catch (ScriptException ex) {
//					ex.printStackTrace();
//					log.error("执行SVG动作脚本出错", ex);
//				}
//			}
		}
	}
}
