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

import javafx.application.Platform;

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

import netscape.javascript.JSException;

import org.eclipse.jetty.util.log.Log;
import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;

import com.ht.data.script.ScriptUtil;
import com.ht.svg.util.DomUtil;
import com.sun.webpane.webkit.dom.ElementImpl;

/**
 * 填充动画烁 <set attributeName="fill" name="FillOnExpression"
 * tag="主楼/A区/1层/1号配置柜/回路1/遥信/第二路温度报警" to="mediumorchid" expression="v1"/>
 * 
 * @author Administrator
 */
public class SVGAnimationOnExpression extends AbstractAnimationDataListener {
	private static final org.slf4j.Logger log  = LoggerFactory.getLogger(SVGAnimationOnExpression.class);

	/**
	 * attribute names
	 */
	private final static String EXP_ATTR = "expression";
	private CompiledScript compiledScript = null;
	// private String id = null;
	private Boolean state = null;
	private String toFill = null;// 填充闪烁特殊处理

	private BlinkingModel blinkingModel;
	private String id;

	public SVGAnimationOnExpression(AnimatHandler animationHandler,
			Element animationElement, boolean isBlinking) {
		super(animationHandler, animationElement);

		this.isBlinkingAnimation = isBlinking;

		id = ID_PREFIX + DomUtil.generateID();
		animationElement.setAttribute("id", id);

		String attributeName = animationElement.getAttribute("attributeName");
		if (attributeName.equals("fill")) {
			toFill = animationElement.getAttribute("to");
		} else if (attributeName.equals("visibility")) {
			String to = animationElement.getAttribute("to");
			if (to.equals("visible") && !isBlinkingAnimation) {
				parentElement.setAttributeNS(null, "visibility", "hidden");
			}
		}

		if (isBlinkingAnimation) {
			String from = animationElement.getAttribute("from");
			String to = animationElement.getAttribute("to");
			if (!from.isEmpty() && !to.isEmpty()) {
				blinkingModel = new BlinkingModel((ElementImpl) parentElement, attributeName,
						from, to);
			}
		}

		String script = animationElement.getTextContent();
		if (script == null || script.isEmpty()) {
			script = animationElement.getAttribute(EXP_ATTR);
		}
		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();
				}
			}
		}

	}

	@Override
	public void dispose() {
		super.dispose();
		if (blinkingModel != null) {// 闪烁
		// animationsHandler.blinkingAnimatIdSet.remove(id);
			animationsHandler.removeBlinking(blinkingModel);
		}
	}

	private void onState(final Boolean state) {

		Platform.runLater(new Runnable() {
			public void run() {
				if (toFill != null && !toFill.isEmpty()) {
					((ElementImpl) parentElement).getStyle().setProperty(
							"fill", toFill, "");
				}

				// if (isBlinkingAnimation) {// 闪烁
				// animationsHandler.blinkingAnimatIdSet.add(id);
				// }
				if (blinkingModel != null) {// 闪烁
					if (state != null) {
						animationsHandler.addBlinking(blinkingModel);
					}
				} else {

					animationsHandler
							.getSvgViewer()
							.getWebEngine()
							.executeScript(
									"document.getElementById('" + id
											+ "').beginElement();");
				}
			}
		});
	}

	private void offState(final Boolean state) {
		Platform.runLater(new Runnable() {
			public void run() {
				animationsHandler
						.getSvgViewer()
						.getWebEngine()
						.executeScript(
								"document.getElementById('" + id
										+ "').endElement();");
				if (blinkingModel != null && state != null) {// 闪烁
					animationsHandler.removeBlinking(blinkingModel);
				}

			}
		});
	}

	@Override
	public void dataChanged(String... varNames) {

		if (compiledScript == null) {
			return;
		}
		// 执行表达式脚本
		try {
			final Object ret = compiledScript.eval();
			if (ret != null && ret instanceof Boolean) {// 要求返回 boolean型的值
				try {
					if ((Boolean) ret) {// 开始动画
						if (state == null || !state) {
							onState(state);
						}

					} else if (state == null || state) {// 停止动画

						offState(state);
					}
				} catch (DOMException e) {
					e.printStackTrace();
				} catch (JSException e) {
					e.printStackTrace();
				}

				state = (Boolean) ret;
			} else {// 解析脚本出错
			}
		} catch (Exception ex) {
			log.warn("SVG动画表达式执行出错", ex);
		}

	}
}
