package csa.tiletools.particle;

import csa.util.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

public class  ParticleEngineData
{
	protected String mClass="";
	public String mName="";
	protected Vector<String> mEmitters=new Vector<String>();
	protected int mWidth=0;
	protected int mHeight=0;
	protected int mTickResolution=0;
	public String getName()
	{
		return mName;
	}
	public void setName(String n)
	{
		mName=n;
	}
	public String getCClass()
	{
		return mClass;
	}
	public void setCClass(String c)
	{
		mClass=c;
	}
	public Vector<String> getEmitters()
	{
		return mEmitters;
	}
	public void setEmitters(Vector<String> Emitters)
	{
		mEmitters=Emitters;
	}
	public int getWidth()
	{
		return mWidth;
	}
	public void setWidth(int Width)
	{
		mWidth=Width;
	}
	public int getHeight()
	{
		return mHeight;
	}
	public void setHeight(int Height)
	{
		mHeight=Height;
	}
	public int getTickResolution()
	{
		return mTickResolution;
	}
	public void setTickResolution(int TickResolution)
	{
		mTickResolution=TickResolution;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<ParticleEngineData>\n");
		s.append( "\t\t<Class>"+UtilityString.toXML(mClass)+"</Class>\n");
		s.append( "\t\t<Name>"+UtilityString.toXML(mName)+"</Name>\n");
		s.append( "\t\t<Emitterss>\n");
		for (int i=0;i<mEmitters.size(); i++)
		{
			s.append( "\t\t\t<Emitters>"+UtilityString.toXML(mEmitters.elementAt(i))+"</Emitters>\n");
		}
		s.append( "\t\t</Emitterss>\n");
		s.append( "\t\t<Width>"+mWidth+"</Width>\n");
		s.append( "\t\t<Height>"+mHeight+"</Height>\n");
		s.append( "\t\t<TickResolution>"+mTickResolution+"</TickResolution>\n");
		s.append( "\t</ParticleEngineData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static ParticleEngineDataXMLHandler XMLHANDLER = new ParticleEngineDataXMLHandler();
	public static ParticleEngineDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<ParticleEngineData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllParticleEngineData>\n");
			Iterator<ParticleEngineData> iter = col.iterator();
			while (iter.hasNext())
			{
				ParticleEngineData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllParticleEngineData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, ParticleEngineData> getHashMapFromXML(String filename)
	{
		HashMap<String, ParticleEngineData> filters = new HashMap<String, ParticleEngineData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			ParticleEngineDataXMLHandler h = ParticleEngineData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"ParticleEngineData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
