package com.ht.svg.action;

import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.Label;

import org.w3c.dom.Element;
import org.w3c.dom.events.Event;

import com.ht.ui.MainApplication;
import com.ht.ui.controls.window.MessageDialog;
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 class used to change a value
 * 
 * @author ITRIS, Jordi SUC
 */
public class SendMeasure extends AbstractActionListener {

	/**
	 * the string names
	 */
	protected static String tagToWriteName = "tag-var",
			returnToInitialValueMethodName = "returnToInitialValueMethod",
			autoName = "auto";

	private String tagVarName;

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

		super(actionElement);
		tagVarName = actionElement.getAttribute(tagToWriteName);

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

	@Override
	public void performAction(Event event) {
		if (tagVarName.isEmpty()) {
			return;
		}

		// TODO: 用户输入接口
		final double enteredNumber = 0;

		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().getYt()) {

											try {
												HessianResponse resp = HessianFactoryManger.INSTANCE.getTagVarService().yt(tagVarName, enteredNumber);
												MainApplication.getMainApp().showMessage(resp.getMsg());
											} catch (Exception e) {
												MainApplication.getMainApp().showMessage("服务器未响应.");
											}

										} else {

											Label label = new Label(
													"您所在的用户组没有权限执行该操作!");
											label.setId("modal_message");
											MainApplication.getMainApp()
													.showModalMessage(label);
										}
									}
								});

							}
						}, null);

			}
		});

	}
}
