/*
 * Created on 27 janv. 2005
 */
package com.ht.svg.animat;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import netscape.javascript.JSException;

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

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

/**
 * the listener to the changes of data for the "text on mesure" animation node
 *
 * @author ITRIS, Jordi SUC
 */
public class TextOnMeasure extends AbstractAnimationDataListener {
	private static final Logger log = LoggerFactory.getLogger(TextOnMeasure.class);

    /**
     * the value of the tag attribute of the animation node
     */
    private String tagVarName = "";
    /**
     * the pattern for displaying the value
     */
    private String pattern = "";
    /**
     * the last text value
     */
    private String lastTextValue;
    /**
     * the format for writing the received values
     */
    private DecimalFormat patternFormat;
    /**
     * whether the listener is enabled or not
     */
    protected boolean isEnabled = true;
//    private String id = null;

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

        if (animationElement != null) {

            //getting the pattern
            pattern = animationElement.getAttribute("pattern");

            try {
                //sets the format used to convert numbers into a string
                DecimalFormatSymbols symbols = new DecimalFormatSymbols();
                symbols.setDecimalSeparator('.');
                patternFormat = new DecimalFormat(pattern, symbols);
            } catch (Exception ex) {
                patternFormat = null;
            }

            if (patternFormat == null) {

                patternFormat = FormatStore.format;
            }
        }

    }

    /**
     * the method called when the data to which the listener is registered, is
     * modified
     */
    @Override
    public void dataChanged(String ...varNames) {

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

        //the new value
        String newTextValue = "";

        if (Double.isNaN(newValue)) {
            return;
        } else {

            newTextValue = patternFormat.format(newValue);
        }

        if (newTextValue != null && !newTextValue.equals(lastTextValue)) {

        	try {
        		//((ElementImpl) animationElement).eval("AnimatController.activeTextAnimat('" + id + "','" + fnewTextValue + "');");
        		DomUtil.updateTextElement(parentElement, newTextValue);
        		lastTextValue = newTextValue;
        	} catch (JSException jSException) {
        	}

        }

    }
}
