package csa.tiletools.sprites;

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

public class  SpriteData
{
	protected String mClass="";
	public String mName="";
	protected int mWidth=0;
	protected int mHeight=0;
	protected int mType=0;
	protected Vector<String> mAtrributeName=new Vector<String>();
	protected Vector<String> mAttributeType=new Vector<String>();
	protected Vector<String> mAttributeValue=new Vector<String>();
	protected String mID="";
	protected String mDescription="";
	protected boolean mForceSpriteSize=false;
	protected Vector<String> mActionString=new Vector<String>();
	protected Vector<String> mImageSetName=new Vector<String>();
	protected Vector<String> mActionDirectionImageName=new Vector<String>();
	protected boolean mStepsAbsolut=false;
	protected Vector<Integer> mActionDirectionAnimationStepDeltaX=new Vector<Integer>();
	protected Vector<Integer> mActionDirectionAnimationStepDeltaY=new Vector<Integer>();
	protected Vector<String> mSubsequentAction=new Vector<String>();
	protected Vector<Boolean> mLoopAction=new Vector<Boolean>();
	protected String mPoolName="";
	protected boolean mAutomaticMovment=false;
	protected boolean mUpdateWhenNotInViewport=false;
	protected boolean mBackShadow=false;
	protected int mBackThickness=0;
	protected int mBackAntialiazing=0;
	protected int mBack_R=0;
	protected int mBack_G=0;
	protected int mBack_B=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 getWidth()
	{
		return mWidth;
	}
	public void setWidth(int Width)
	{
		mWidth=Width;
	}
	public int getHeight()
	{
		return mHeight;
	}
	public void setHeight(int Height)
	{
		mHeight=Height;
	}
	public int getType()
	{
		return mType;
	}
	public void setType(int Type)
	{
		mType=Type;
	}
	public Vector<String> getAtrributeName()
	{
		return mAtrributeName;
	}
	public void setAtrributeName(Vector<String> AtrributeName)
	{
		mAtrributeName=AtrributeName;
	}
	public Vector<String> getAttributeType()
	{
		return mAttributeType;
	}
	public void setAttributeType(Vector<String> AttributeType)
	{
		mAttributeType=AttributeType;
	}
	public Vector<String> getAttributeValue()
	{
		return mAttributeValue;
	}
	public void setAttributeValue(Vector<String> AttributeValue)
	{
		mAttributeValue=AttributeValue;
	}
	public String getID()
	{
		return mID;
	}
	public void setID(String ID)
	{
		mID=ID;
	}
	public String getDescription()
	{
		return mDescription;
	}
	public void setDescription(String Description)
	{
		mDescription=Description;
	}
	public boolean getForceSpriteSize()
	{
		return mForceSpriteSize;
	}
	public void setForceSpriteSize(boolean ForceSpriteSize)
	{
		mForceSpriteSize=ForceSpriteSize;
	}
	public Vector<String> getActionString()
	{
		return mActionString;
	}
	public void setActionString(Vector<String> ActionString)
	{
		mActionString=ActionString;
	}
	public Vector<String> getImageSetName()
	{
		return mImageSetName;
	}
	public void setImageSetName(Vector<String> ImageSetName)
	{
		mImageSetName=ImageSetName;
	}
	public Vector<String> getActionDirectionImageName()
	{
		return mActionDirectionImageName;
	}
	public void setActionDirectionImageName(Vector<String> ActionDirectionImageName)
	{
		mActionDirectionImageName=ActionDirectionImageName;
	}
	public boolean getStepsAbsolut()
	{
		return mStepsAbsolut;
	}
	public void setStepsAbsolut(boolean StepsAbsolut)
	{
		mStepsAbsolut=StepsAbsolut;
	}
	public Vector<Integer> getActionDirectionAnimationStepDeltaX()
	{
		return mActionDirectionAnimationStepDeltaX;
	}
	public void setActionDirectionAnimationStepDeltaX(Vector<Integer> ActionDirectionAnimationStepDeltaX)
	{
		mActionDirectionAnimationStepDeltaX=ActionDirectionAnimationStepDeltaX;
	}
	public Vector<Integer> getActionDirectionAnimationStepDeltaY()
	{
		return mActionDirectionAnimationStepDeltaY;
	}
	public void setActionDirectionAnimationStepDeltaY(Vector<Integer> ActionDirectionAnimationStepDeltaY)
	{
		mActionDirectionAnimationStepDeltaY=ActionDirectionAnimationStepDeltaY;
	}
	public Vector<String> getSubsequentAction()
	{
		return mSubsequentAction;
	}
	public void setSubsequentAction(Vector<String> SubsequentAction)
	{
		mSubsequentAction=SubsequentAction;
	}
	public Vector<Boolean> getLoopAction()
	{
		return mLoopAction;
	}
	public void setLoopAction(Vector<Boolean> LoopAction)
	{
		mLoopAction=LoopAction;
	}
	public String getPoolName()
	{
		return mPoolName;
	}
	public void setPoolName(String PoolName)
	{
		mPoolName=PoolName;
	}
	public boolean getAutomaticMovment()
	{
		return mAutomaticMovment;
	}
	public void setAutomaticMovment(boolean AutomaticMovment)
	{
		mAutomaticMovment=AutomaticMovment;
	}
	public boolean getUpdateWhenNotInViewport()
	{
		return mUpdateWhenNotInViewport;
	}
	public void setUpdateWhenNotInViewport(boolean UpdateWhenNotInViewport)
	{
		mUpdateWhenNotInViewport=UpdateWhenNotInViewport;
	}
	public boolean getBackShadow()
	{
		return mBackShadow;
	}
	public void setBackShadow(boolean BackShadow)
	{
		mBackShadow=BackShadow;
	}
	public int getBackThickness()
	{
		return mBackThickness;
	}
	public void setBackThickness(int BackThickness)
	{
		mBackThickness=BackThickness;
	}
	public int getBackAntialiazing()
	{
		return mBackAntialiazing;
	}
	public void setBackAntialiazing(int BackAntialiazing)
	{
		mBackAntialiazing=BackAntialiazing;
	}
	public int getBack_R()
	{
		return mBack_R;
	}
	public void setBack_R(int Back_R)
	{
		mBack_R=Back_R;
	}
	public int getBack_G()
	{
		return mBack_G;
	}
	public void setBack_G(int Back_G)
	{
		mBack_G=Back_G;
	}
	public int getBack_B()
	{
		return mBack_B;
	}
	public void setBack_B(int Back_B)
	{
		mBack_B=Back_B;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<SpriteData>\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<WIDTH>"+mWidth+"</WIDTH>\n");
		s.append( "\t\t<HEIGHT>"+mHeight+"</HEIGHT>\n");
		s.append( "\t\t<TYPE>"+mType+"</TYPE>\n");
		s.append( "\t\t<ATTRIBUTNAMEs>\n");
		for (int i=0;i<mAtrributeName.size(); i++)
		{
			s.append( "\t\t\t<ATTRIBUTNAME>"+UtilityString.toXML(mAtrributeName.elementAt(i))+"</ATTRIBUTNAME>\n");
		}
		s.append( "\t\t</ATTRIBUTNAMEs>\n");
		s.append( "\t\t<ATTRIBUTETYPEs>\n");
		for (int i=0;i<mAttributeType.size(); i++)
		{
			s.append( "\t\t\t<ATTRIBUTETYPE>"+UtilityString.toXML(mAttributeType.elementAt(i))+"</ATTRIBUTETYPE>\n");
		}
		s.append( "\t\t</ATTRIBUTETYPEs>\n");
		s.append( "\t\t<ATTRIBUTEVALUEs>\n");
		for (int i=0;i<mAttributeValue.size(); i++)
		{
			s.append( "\t\t\t<ATTRIBUTEVALUE>"+UtilityString.toXML(mAttributeValue.elementAt(i))+"</ATTRIBUTEVALUE>\n");
		}
		s.append( "\t\t</ATTRIBUTEVALUEs>\n");
		s.append( "\t\t<ID>"+UtilityString.toXML(mID)+"</ID>\n");
		s.append( "\t\t<DESCRIPTION>"+UtilityString.toXML(mDescription)+"</DESCRIPTION>\n");
		s.append( "\t\t<ForceSpriteSize>"+mForceSpriteSize+"</ForceSpriteSize>\n");
		s.append( "\t\t<ActionStrings>\n");
		for (int i=0;i<mActionString.size(); i++)
		{
			s.append( "\t\t\t<ActionString>"+UtilityString.toXML(mActionString.elementAt(i))+"</ActionString>\n");
		}
		s.append( "\t\t</ActionStrings>\n");
		s.append( "\t\t<ImageSetNames>\n");
		for (int i=0;i<mImageSetName.size(); i++)
		{
			s.append( "\t\t\t<ImageSetName>"+UtilityString.toXML(mImageSetName.elementAt(i))+"</ImageSetName>\n");
		}
		s.append( "\t\t</ImageSetNames>\n");
		s.append( "\t\t<ActionDirectionImageNames>\n");
		for (int i=0;i<mActionDirectionImageName.size(); i++)
		{
			s.append( "\t\t\t<ActionDirectionImageName>"+UtilityString.toXML(mActionDirectionImageName.elementAt(i))+"</ActionDirectionImageName>\n");
		}
		s.append( "\t\t</ActionDirectionImageNames>\n");
		s.append( "\t\t<StepsAbsolut>"+mStepsAbsolut+"</StepsAbsolut>\n");
		s.append( "\t\t<ActionDirectionAnimationStepDeltaXs>\n");
		for (int i=0;i<mActionDirectionAnimationStepDeltaX.size(); i++)
		{
			s.append( "\t\t\t<ActionDirectionAnimationStepDeltaX>"+mActionDirectionAnimationStepDeltaX.elementAt(i)+"</ActionDirectionAnimationStepDeltaX>\n");
		}
		s.append( "\t\t</ActionDirectionAnimationStepDeltaXs>\n");
		s.append( "\t\t<ActionDirectionAnimationStepDeltaYs>\n");
		for (int i=0;i<mActionDirectionAnimationStepDeltaY.size(); i++)
		{
			s.append( "\t\t\t<ActionDirectionAnimationStepDeltaY>"+mActionDirectionAnimationStepDeltaY.elementAt(i)+"</ActionDirectionAnimationStepDeltaY>\n");
		}
		s.append( "\t\t</ActionDirectionAnimationStepDeltaYs>\n");
		s.append( "\t\t<SubsequentActions>\n");
		for (int i=0;i<mSubsequentAction.size(); i++)
		{
			s.append( "\t\t\t<SubsequentAction>"+UtilityString.toXML(mSubsequentAction.elementAt(i))+"</SubsequentAction>\n");
		}
		s.append( "\t\t</SubsequentActions>\n");
		s.append( "\t\t<LoopActions>\n");
		for (int i=0;i<mLoopAction.size(); i++)
		{
			s.append( "\t\t\t<LoopAction>"+mLoopAction.elementAt(i)+"</LoopAction>\n");
		}
		s.append( "\t\t</LoopActions>\n");
		s.append( "\t\t<PoolName>"+UtilityString.toXML(mPoolName)+"</PoolName>\n");
		s.append( "\t\t<AutomaticMovment>"+mAutomaticMovment+"</AutomaticMovment>\n");
		s.append( "\t\t<UpdateWhenNotInViewport>"+mUpdateWhenNotInViewport+"</UpdateWhenNotInViewport>\n");
		s.append( "\t\t<BackShadow>"+mBackShadow+"</BackShadow>\n");
		s.append( "\t\t<BackThickness>"+mBackThickness+"</BackThickness>\n");
		s.append( "\t\t<BackAntialiazing>"+mBackAntialiazing+"</BackAntialiazing>\n");
		s.append( "\t\t<Back_R>"+mBack_R+"</Back_R>\n");
		s.append( "\t\t<Back_G>"+mBack_G+"</Back_G>\n");
		s.append( "\t\t<Back_B>"+mBack_B+"</Back_B>\n");
		s.append( "\t</SpriteData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static SpriteDataXMLHandler XMLHANDLER = new SpriteDataXMLHandler();
	public static SpriteDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<SpriteData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllSpriteData>\n");
			Iterator<SpriteData> iter = col.iterator();
			while (iter.hasNext())
			{
				SpriteData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllSpriteData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, SpriteData> getHashMapFromXML(String filename)
	{
		HashMap<String, SpriteData> filters = new HashMap<String, SpriteData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			SpriteDataXMLHandler h = SpriteData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"SpriteData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
