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  ParticleData
{
	protected String mClass="";
	public String mName="";
	protected int mGenerateEachTick=0;
	protected float mFade=0f;
	protected float mRotate=0f;
	protected String mEmitterName="";
	protected int mEmitterID=0;
	protected int mPositionX=0;
	protected int mPositionY=0;
	protected int mWidth=0;
	protected int mHeight=0;
	protected int mLife=0;
	protected float mLifeReduce=0f;
	protected int mEmitPositionX=0;
	protected int mEmitPositionY=0;
	protected boolean mEmitPositionXRandom=false;
	protected boolean mEmitPositionYRandom=false;
	protected boolean mUseTint=false;
	protected int mstartColor=0;
	protected int mendColor=0;
	protected boolean mcolorAdditive=false;
	protected String mParticleImageName="";
	protected String mParticleEmitterName="";
	protected float mStartVelocity1=0f;
	protected float mStartVelocity2=0f;
	protected int mStartAngle1=0;
	protected float mAccelerationX=0f;
	protected float mAccelerationY=0f;
	protected float mtangentialAcceleration=0f;
	protected float mradialAcceleration=0f;
	protected int mStartAngle2=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 int getGenerateEachTick()
	{
		return mGenerateEachTick;
	}
	public void setGenerateEachTick(int GenerateEachTick)
	{
		mGenerateEachTick=GenerateEachTick;
	}
	public float getFade()
	{
		return mFade;
	}
	public void setFade(float Fade)
	{
		mFade=Fade;
	}
	public float getRotate()
	{
		return mRotate;
	}
	public void setRotate(float Rotate)
	{
		mRotate=Rotate;
	}
	public String getEmitterName()
	{
		return mEmitterName;
	}
	public void setEmitterName(String EmitterName)
	{
		mEmitterName=EmitterName;
	}
	public int getEmitterID()
	{
		return mEmitterID;
	}
	public void setEmitterID(int EmitterID)
	{
		mEmitterID=EmitterID;
	}
	public int getPositionX()
	{
		return mPositionX;
	}
	public void setPositionX(int PositionX)
	{
		mPositionX=PositionX;
	}
	public int getPositionY()
	{
		return mPositionY;
	}
	public void setPositionY(int PositionY)
	{
		mPositionY=PositionY;
	}
	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 getLife()
	{
		return mLife;
	}
	public void setLife(int Life)
	{
		mLife=Life;
	}
	public float getLifeReduce()
	{
		return mLifeReduce;
	}
	public void setLifeReduce(float LifeReduce)
	{
		mLifeReduce=LifeReduce;
	}
	public int getEmitPositionX()
	{
		return mEmitPositionX;
	}
	public void setEmitPositionX(int EmitPositionX)
	{
		mEmitPositionX=EmitPositionX;
	}
	public int getEmitPositionY()
	{
		return mEmitPositionY;
	}
	public void setEmitPositionY(int EmitPositionY)
	{
		mEmitPositionY=EmitPositionY;
	}
	public boolean getEmitPositionXRandom()
	{
		return mEmitPositionXRandom;
	}
	public void setEmitPositionXRandom(boolean EmitPositionXRandom)
	{
		mEmitPositionXRandom=EmitPositionXRandom;
	}
	public boolean getEmitPositionYRandom()
	{
		return mEmitPositionYRandom;
	}
	public void setEmitPositionYRandom(boolean EmitPositionYRandom)
	{
		mEmitPositionYRandom=EmitPositionYRandom;
	}
	public boolean getUseTint()
	{
		return mUseTint;
	}
	public void setUseTint(boolean UseTint)
	{
		mUseTint=UseTint;
	}
	public int getstartColor()
	{
		return mstartColor;
	}
	public void setstartColor(int startColor)
	{
		mstartColor=startColor;
	}
	public int getendColor()
	{
		return mendColor;
	}
	public void setendColor(int endColor)
	{
		mendColor=endColor;
	}
	public boolean getcolorAdditive()
	{
		return mcolorAdditive;
	}
	public void setcolorAdditive(boolean colorAdditive)
	{
		mcolorAdditive=colorAdditive;
	}
	public String getParticleImageName()
	{
		return mParticleImageName;
	}
	public void setParticleImageName(String ParticleImageName)
	{
		mParticleImageName=ParticleImageName;
	}
	public String getParticleEmitterName()
	{
		return mParticleEmitterName;
	}
	public void setParticleEmitterName(String ParticleEmitterName)
	{
		mParticleEmitterName=ParticleEmitterName;
	}
	public float getStartVelocity1()
	{
		return mStartVelocity1;
	}
	public void setStartVelocity1(float StartVelocity1)
	{
		mStartVelocity1=StartVelocity1;
	}
	public float getStartVelocity2()
	{
		return mStartVelocity2;
	}
	public void setStartVelocity2(float StartVelocity2)
	{
		mStartVelocity2=StartVelocity2;
	}
	public int getStartAngle1()
	{
		return mStartAngle1;
	}
	public void setStartAngle1(int StartAngle1)
	{
		mStartAngle1=StartAngle1;
	}
	public float getAccelerationX()
	{
		return mAccelerationX;
	}
	public void setAccelerationX(float AccelerationX)
	{
		mAccelerationX=AccelerationX;
	}
	public float getAccelerationY()
	{
		return mAccelerationY;
	}
	public void setAccelerationY(float AccelerationY)
	{
		mAccelerationY=AccelerationY;
	}
	public float gettangentialAcceleration()
	{
		return mtangentialAcceleration;
	}
	public void settangentialAcceleration(float tangentialAcceleration)
	{
		mtangentialAcceleration=tangentialAcceleration;
	}
	public float getradialAcceleration()
	{
		return mradialAcceleration;
	}
	public void setradialAcceleration(float radialAcceleration)
	{
		mradialAcceleration=radialAcceleration;
	}
	public int getStartAngle2()
	{
		return mStartAngle2;
	}
	public void setStartAngle2(int StartAngle2)
	{
		mStartAngle2=StartAngle2;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<ParticleData>\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<GenerateEachTick>"+mGenerateEachTick+"</GenerateEachTick>\n");
		s.append( "\t\t<Fade>"+mFade+"</Fade>\n");
		s.append( "\t\t<Rotate>"+mRotate+"</Rotate>\n");
		s.append( "\t\t<EmitterName>"+UtilityString.toXML(mEmitterName)+"</EmitterName>\n");
		s.append( "\t\t<EmitterID>"+mEmitterID+"</EmitterID>\n");
		s.append( "\t\t<PositionX>"+mPositionX+"</PositionX>\n");
		s.append( "\t\t<PositionY>"+mPositionY+"</PositionY>\n");
		s.append( "\t\t<Width>"+mWidth+"</Width>\n");
		s.append( "\t\t<Height>"+mHeight+"</Height>\n");
		s.append( "\t\t<Life>"+mLife+"</Life>\n");
		s.append( "\t\t<LifeReduce>"+mLifeReduce+"</LifeReduce>\n");
		s.append( "\t\t<EmitPositionX>"+mEmitPositionX+"</EmitPositionX>\n");
		s.append( "\t\t<EmitPositionY>"+mEmitPositionY+"</EmitPositionY>\n");
		s.append( "\t\t<EmitPositionXRandom>"+mEmitPositionXRandom+"</EmitPositionXRandom>\n");
		s.append( "\t\t<EmitPositionYRandom>"+mEmitPositionYRandom+"</EmitPositionYRandom>\n");
		s.append( "\t\t<UseTint>"+mUseTint+"</UseTint>\n");
		s.append( "\t\t<startColor>"+mstartColor+"</startColor>\n");
		s.append( "\t\t<endColor>"+mendColor+"</endColor>\n");
		s.append( "\t\t<colorAdditive>"+mcolorAdditive+"</colorAdditive>\n");
		s.append( "\t\t<ParticleImageName>"+UtilityString.toXML(mParticleImageName)+"</ParticleImageName>\n");
		s.append( "\t\t<ParticleEmitterName>"+UtilityString.toXML(mParticleEmitterName)+"</ParticleEmitterName>\n");
		s.append( "\t\t<StartVelocity1>"+mStartVelocity1+"</StartVelocity1>\n");
		s.append( "\t\t<StartVelocity2>"+mStartVelocity2+"</StartVelocity2>\n");
		s.append( "\t\t<StartAngle1>"+mStartAngle1+"</StartAngle1>\n");
		s.append( "\t\t<AccelerationX>"+mAccelerationX+"</AccelerationX>\n");
		s.append( "\t\t<AccelerationY>"+mAccelerationY+"</AccelerationY>\n");
		s.append( "\t\t<tangentialAcceleration>"+mtangentialAcceleration+"</tangentialAcceleration>\n");
		s.append( "\t\t<radialAcceleration>"+mradialAcceleration+"</radialAcceleration>\n");
		s.append( "\t\t<StartAngle2>"+mStartAngle2+"</StartAngle2>\n");
		s.append( "\t</ParticleData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static ParticleDataXMLHandler XMLHANDLER = new ParticleDataXMLHandler();
	public static ParticleDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<ParticleData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllParticleData>\n");
			Iterator<ParticleData> iter = col.iterator();
			while (iter.hasNext())
			{
				ParticleData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllParticleData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, ParticleData> getHashMapFromXML(String filename)
	{
		HashMap<String, ParticleData> filters = new HashMap<String, ParticleData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			ParticleDataXMLHandler h = ParticleData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"ParticleData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
