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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.w3c.dom.Element;

import com.ht.svg.util.DomUtil;

/**
 * the listener to the changes of data for the "text" animation node
 *
 * @author ITRIS, Jordi SUC
 */
public class TextOnDateTime extends AbstractAnimationDataListener {

    private SimpleDateFormat simpleDateFormat;
    //private final long period = 24 * 3600000 + 100;
    private final long period = 1000;
//    private String id = null;

    /**
     * the constructor of the class
     *
     * @param animationHandler
     * @param animationElement an animation node
     */
    public TextOnDateTime(AnimatHandler animationHandler, Element animationElement) {

        super(animationHandler, animationElement);
//        id = ID_PREFIX + DomUtil.generateID();
//        parentElement.setAttribute("id", id);

        if (animationElement != null) {
            String pattern = animationElement.getAttribute("pattern");
            simpleDateFormat = new SimpleDateFormat(pattern);

        }

        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {

                        try {
							String date = simpleDateFormat.format(new Date());
							//((ElementImpl) (TextOnDateTime.this.animationElement)).eval("AnimatController.activeTextAnimat('" + id + "'," + date + ");");
							DomUtil.updateTextElement(parentElement, date);
						} catch (Exception e) {
							e.printStackTrace();
						}
            }
        }, 0, period);
    }

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