/*
 * Created on 2004-09-04
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.skins;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;

/**
 * This class holds informations about one skin for the emulator
 * 
 * @author mikolaj
 */
public class SkinEntry {
    final static String MTIME_PREFIX="mtime.skinscache."; 
    final static String PROPERTY_PREFIX="skinscache."; 
    final static String NAME_POSTFIX=".name"; 
    
    /**
     * Constructs an entry for the on skin option 
     */
    SkinEntry() {
        super();
        id=null;
        fName=getDefaultString();
    }

    /**
     * Path to the root of the skin data.
     */
    public Path rootPath;
    
    static public String getDefaultString()
    {
        return "Emulator's default"; 
    }
    
    /**
     * @return The default entry which means that the emulator
     * skin won't be altered
     */
    static public SkinEntry getDefault()
    {
        if (fDefault==null)
            fDefault=new SkinEntry();
        return fDefault;
    }
    
    public void prepareXmlReader()
    	throws CoreException
    {
    	try {
    	    File file=new File(rootPath.append("skin.xml").toString());
    		SAXParser parser=SAXParserFactory.newInstance().newSAXParser();
    		fXmlReader=new SkinXmlReader();
    		parser.parse(file, fXmlReader);
    	}
    	catch (Exception e)
    	{
    	    Status s=new Status(IStatus.ERROR, SymbdevPlugin.getUniqueIdentifier(),
    	            SymbdevPlugin.ERROR_EPOC_SKIN, "Error loading skin: "+e.getMessage(), e);
    	    throw new CoreException(s);
    	}        
    }
    
    public synchronized String getName()
    {
        if (fName==null)
        {
            if (id==null)
            {
                fName=getDefaultString();
                return fName;
            }
            
//            System.out.println("First access to name of skin "+id);
            long mtime=SymbdevPlugin.getDefault().getPluginPreferences().getLong(MTIME_PREFIX+id+NAME_POSTFIX);
            String cachedName=SymbdevPlugin.getDefault().getPluginPreferences().getString(PROPERTY_PREFIX+id+NAME_POSTFIX);
            long fileMTime=new File(getSkinXml().toString()).lastModified();
            if (cachedName==null || cachedName.equals("") || fileMTime!=mtime)
            {
//                System.out.println("Must load name of skin "+id);
                
                try {
                    prepareXmlReader();
                } catch (CoreException e) {
                    SymbdevPlugin.getDefault().getLog().log(e.getStatus());
                    e.printStackTrace();
                    fName="(error loading name)";
                    return fName;
                }
                
                fName=fXmlReader.fName;
                if (fName==null)
                {
                    fName=id;
                } else
                {
                    SymbdevPlugin.getDefault().getPluginPreferences().setValue(MTIME_PREFIX+id+NAME_POSTFIX, fileMTime);
                    SymbdevPlugin.getDefault().getPluginPreferences().setValue(PROPERTY_PREFIX+id+NAME_POSTFIX, fName);                    
                }
            } else fName=cachedName;
            return fName;
        }
        return fName;
    }
    
    public IPath getSkinXml()
    {
        return rootPath.append("skin.xml");
    }
    
    public String getId()
    {
        return id;
    }
    
    String id;
    String fName;
    SkinXmlReader fXmlReader;
    static SkinEntry fDefault;
}
