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

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import com.ht.data.RealTimeDataManager;
import com.ht.svg.util.DomUtil;

/**
 * 动画->属性->图元
 *
 * @author Administrator
 */
public class SymbolOnState extends AbstractAnimationDataListener {

    private static final Logger log = LoggerFactory.getLogger(SymbolOnState.class.getName());
    /**
     * attribute names
     */
    private final static String defaultValueAtt = "defaultValueAttr",
            usedAtt = "used", valueAtt = "value", attrValueAttr = "attributeValue";
    private String defaultAttrValue = "";
    /**
     * 用于记录不同遥信状态下的图元属性值
     */
    private Map<String, String> attrMap = new HashMap<String, String>();
//    private String id = null;
    private String tagVarName = "";

    public SymbolOnState(AnimatHandler animationHandler, Element animationElement) {
        super(animationHandler, animationElement);
//        id = ID_PREFIX + DomUtil.generateID();
//        parentElement.setAttribute("id", id);

    	if (dataNames.isEmpty()) {
    		log.error("未设置变量!!!!!!!");
    		return;
    	}
    	
    	tagVarName = dataNames.iterator().next();

        defaultAttrValue = animationElement.getAttribute(defaultValueAtt);

        //fills the maps with the fill and stroke values
        String value = "";
        boolean isUsed = false;

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

            if (cur instanceof Element) {
                Element valueEl = (Element) cur;

                //whether the child should be used or not
                isUsed = Boolean.parseBoolean(valueEl.getAttribute(usedAtt));// 是否有效

                if (isUsed) {

                    //getting one of the possible values of the tag
                    value = valueEl.getAttribute(valueAtt);

                    if (!value.isEmpty()) {
                        String attr = valueEl.getAttribute(attrValueAttr);
                        //String exp = valueEl.getTextContent();

                        if (!attr.equals("")) {
                            attrMap.put(value, attr);
                        }
                    }
                }
            }
        }

    }

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

    	if (tagVarName == null) {
    		log.warn("未设置动画变量");
    		return;
    	}
    	
        //the new values
        String newAttrValue = null;

        //getting the value of the tag
        Object v = RealTimeDataManager.INSTANCE.getVarValue( tagVarName );
        if (v == null) {
        	newAttrValue = defaultAttrValue;
        } else {
        	
	        String value = "0";
	        if (v instanceof Boolean) {
	        	value = ((Boolean) v) ? "1" : "0";
	        } else {
	            value = v.toString();
	        }
	
	        if (attrMap.containsKey(value)) {
	            newAttrValue = attrMap.get(value);
	        }
        }

        if (newAttrValue != null) {

        	try {
        		DomUtil.activeSymbolAnimat(parentElement, newAttrValue);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        }


    }
}
