package com.ht.svg.action;

import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.EventHandler;

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

import com.ht.data.RealTimeDataManager;
import com.ht.ui.MainApplication;
import com.ht.ui.controls.window.MessageDialog;
import com.ht.ui.util.HessianResponseService;
import com.ht.ui.util.HessianResponseTask;
import com.ht.ui.util.MessageConstants;
import com.ht.util.UserSession;
import com.ht.web.hessian.HessianFactoryManger;
import com.ht.web.mongo.domain.User;
import com.ht.web.remoting.HessianResponse;

/**
 * the superclass of the simple send command actions
 * 
 * @author Jordi SUC
 */
public class SimpleSendCommand extends AbstractActionListener {
	private static final Logger log = LoggerFactory
			.getLogger(SimpleSendCommand.class);

	/**
	 * the string names
	 */
	protected static String tagVarAttr = "tag-var", autoName = "auto",
			mouseUpEventName = "mouseUpEvent"; 
	private static final String valueName = "value";

	private String tagVarName;
	private boolean tagVarValue = false;

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

		super(actionElement);

		tagVarName = actionElement.getAttribute(tagVarAttr);
		String value = actionElement.getAttribute(valueName);
		tagVarValue = value == null || value.equals("1");

		if (confirmationMessage == null || confirmationMessage.isEmpty()) {
			confirmationMessage = "您确定要执行遥控操作吗?";
		}
	}

	@Override
	public void performAction(Event event) {

		if (tagVarName.isEmpty()) {
			log.warn("遥控变量未设置变量名");
			return;
		}
		
		log.debug("执行遥控操作 : {}", tagVarName);
		
		Platform.runLater(new Runnable() {

			@Override
			public void run() {
				MessageDialog.buildConfirmation("提示", confirmationMessage,
						new EventHandler<javafx.event.Event>() {
							@Override
							public void handle(javafx.event.Event event) {

								isEntitled(new Runnable() {

									@Override
									public void run() {

										User user = UserSession.INSTANCE.getUser();
										if (user != null
												&& user.getGroup().getYk()) {
											
											ActionUtil.yk(tagVarName, tagVarValue);
											
										} else {

											MainApplication.getMainApp().showMessage("您所在的用户组没有权限执行该操作!");
										}
									}
								});

							}
						}, null);

			}
		});
	}

}
