package com.ht.svg.animat;

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;

/**
 * the listener to the changes of data for the "label" animation node 遥信文本
 *
 * @author ITRIS, Jordi SUC
 */
public class TextOnState extends AbstractAnimationDataListener {

	private static final Logger log = LoggerFactory.getLogger(TextOnState.class);
    /**
     * attribute names
     */
    private final static String defaultTextAtt = "defaultText", textAtt = "text", usedAtt = "used", valueAtt = "value";
    private String label0 = "";
    private String label1 = "";
//    private String id = null;
    private String tagVarName = "";

    /**
     * the constructor of the class
     *
     * @param animationHandler
     * @param animationElement an animation node
     */
    public TextOnState(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();

        String value = "", text = "";
        Element childElement;
        boolean isUsed = false;

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

            if (cur instanceof Element) {

                childElement = (Element) cur;

                //whether the child should be used or not
                isUsed = Boolean.parseBoolean(childElement.getAttribute(usedAtt));

                if (isUsed) {

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

                    if (!value.equals("")) {

                        //getting the text value for the given tag value
                        text = childElement.getAttribute(textAtt);
                        if (value.equals("0")) {
                            label0 = text;
                        } else if (value.equals("1")) {

                            label1 = text;
                        }
                    }
                }
            }
        }

    }

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

    	if (tagVarName == null) {
    		log.warn("未设置动画变量");
    		return;
    	}
    	
        //getting the value of the tag
        Boolean value = (Boolean) RealTimeDataManager.INSTANCE.getVarValue( tagVarName );

        //the new text value
        String newTextValue = "";

        if (value == null) {
        	
            newTextValue = animationElement.getAttribute(defaultTextAtt);

        } else {

            if (value) {
                newTextValue = label1;
            } else {
                newTextValue = label0;
            }
            //newTextValue=animationElement.getAttribute(defaultTextAtt);
        }

        if (newTextValue != null) {

        	try {
        		DomUtil.updateTextElement(parentElement, newTextValue);
        	} catch (Exception e) {
        		e.printStackTrace();
        	}
        }
    }
}
