package com.uvigo.mhp.ui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import org.havi.ui.*;
import org.havi.ui.event.*;
import javax.xml.bind.*;
import com.uvigo.mhp.xml.animation.*;
import com.uvigo.tMaestro.SCOCreator.tool.MainFrame;

public class HAnimated extends HStaticAnimation implements Serializable {
     public static final int TIME_ALWAYS = 0;
    public static final int TIME_OK = 1;
    public static final int TIME_WRONG = 2;
    public static final int TIME_DELAYED = 4;
    public static final int TIME_LIMITED = 6;
    private String contentFile;
    private JAXBContext jc;
    private MediaTracker trk;
    private int delayedTimeMode;
    private int delayedTime;
    
    public HAnimated() {
        trk=new MediaTracker(this);
        delayedTimeMode=TIME_ALWAYS;
        delayedTime=0;
        setDefaultSize(new Dimension(20,20));
        
        try {
            jc=JAXBContext.newInstance("com.uvigo.mhp.xml.animation");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
     public int getDelayedTimeMode() {
        return delayedTimeMode;
    }

    public void setDelayedTimeMode(int i) {

        int old = delayedTimeMode;
        if (i != old) {
            delayedTimeMode = i;
            repaint();
            firePropertyChange("delayedTimeMode", new Integer(old), new Integer(i));

        }
    }
    
    public int getDelayedTime() {
        return delayedTime;
    }

    public void setDelayedTime(int m) {
        int old = delayedTime;
        if (m != old) {
            delayedTime = m;
            firePropertyChange("delayedTime", new Integer(old), new Integer(m));
        }
    }
    public String getContentFile() {
        return contentFile;
    }
    
    public void setContentFile(String s) {
        String old=contentFile;
        //if (!(s.equals(old))) { //Necesario eliminarlo, ya que puede cambiar el contenido del fichero y no su ruta
        
        try {
           //M: imagen de inicializaci�n
            if ((s==null) || (s=="")){
               Image iI[]= new Image[1];
               iI[0]=Toolkit.getDefaultToolkit().getImage(getClass().getResource("/com/uvigo/mhp/ui/empty.jpg"));
               setAnimateContent(iI, HState.ALL_STATES);
               return;
            }
            
            //M: Direccionamiento relativo a "user.dir"
                String sBase= new String (MainFrame.oMainFrame.sDir+ "/");
            URL url=new URL("file:"+sBase+s);
            
            //Parte de s que corresponde a ruta (que no es fichero)
            String[] split = s.split("/");
            String sRuta=new String();
            int z=0;
            for (z=0;z<split.length-1;z++) {
                sRuta = sRuta.concat(split[z]+"/");
            }
            
            URLConnection con=url.openConnection();
            
            Unmarshaller u=jc.createUnmarshaller();
            u.setValidating(true);
            Animation animation=(Animation) u.unmarshal(con.getInputStream());
            java.util.List locators=animation.getImageLocator();
            
            Image itemp[]=new Image[locators.size()];
            for (int i=0;i<locators.size();i++) {
                String str=(String) locators.get(i);
                // Si se encuentra un localizador vacio, se da por terminada la lista
                if (str==null) break;
                if (str.equals("")) break;
                
                URL temp=new URL("file:"+sBase+sRuta+str);
                itemp[i]=Toolkit.getDefaultToolkit().getImage(temp);
                trk.addImage(itemp[i],i);
            }
            
            trk.waitForAll();
            
            // Si todo se ha cargado correctamente, se inicializa el contenido
            setAnimateContent(itemp, HState.ALL_STATES);
            
            contentFile=s;
            firePropertyChange("contentFile",old,s);
            start();
            repaint();
        } catch (Exception ex) {
            // El fichero de configuraci�n no se encuentra o no existe
            ex.printStackTrace();
        }
        //}
    }
}