package csa.tiletools.tiles;

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

public class  TileData
{
	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 String mImageDataFile="";
	protected String mImageDataName="";
	protected boolean mForceTileSize=false;
	protected boolean mTileActionLoop=false;
	protected boolean mTileActionCanBeBroken=false;
	protected String mTileActionSound="";
	protected Vector<String> mTileActionIDs=new Vector<String>();
	protected Vector<String> mTileActionTiles=new Vector<String>();
	protected String mTileSetFileName="";
	protected boolean mIsTransitionSource=false;
	protected int mTransitionPriority=0;
	protected int mTransitionType=0;
	protected Vector<String> mTransitionChildrenNames=new Vector<String>();
	protected Vector<String> mVariantNames=new Vector<String>();
	protected int mVariantPercent=0;
	protected boolean mIsCompositeTile=false;
	protected String mCompositeFile="";
	protected String mCompositeClass="";
	protected String mCompositeName="";
	protected int mISOHeight=0;
	protected int mISOHeightOffset=0;
	protected int mISOBaseline=0;
	protected int mISOInherentOffset=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 String getImageDataFile()
	{
		return mImageDataFile;
	}
	public void setImageDataFile(String ImageDataFile)
	{
		mImageDataFile=ImageDataFile;
	}
	public String getImageDataName()
	{
		return mImageDataName;
	}
	public void setImageDataName(String ImageDataName)
	{
		mImageDataName=ImageDataName;
	}
	public boolean getForceTileSize()
	{
		return mForceTileSize;
	}
	public void setForceTileSize(boolean ForceTileSize)
	{
		mForceTileSize=ForceTileSize;
	}
	public boolean getTileActionLoop()
	{
		return mTileActionLoop;
	}
	public void setTileActionLoop(boolean TileActionLoop)
	{
		mTileActionLoop=TileActionLoop;
	}
	public boolean getTileActionCanBeBroken()
	{
		return mTileActionCanBeBroken;
	}
	public void setTileActionCanBeBroken(boolean TileActionCanBeBroken)
	{
		mTileActionCanBeBroken=TileActionCanBeBroken;
	}
	public String getTileActionSound()
	{
		return mTileActionSound;
	}
	public void setTileActionSound(String TileActionSound)
	{
		mTileActionSound=TileActionSound;
	}
	public Vector<String> getTileActionIDs()
	{
		return mTileActionIDs;
	}
	public void setTileActionIDs(Vector<String> TileActionIDs)
	{
		mTileActionIDs=TileActionIDs;
	}
	public Vector<String> getTileActionTiles()
	{
		return mTileActionTiles;
	}
	public void setTileActionTiles(Vector<String> TileActionTiles)
	{
		mTileActionTiles=TileActionTiles;
	}
	public String getTileSetFileName()
	{
		return mTileSetFileName;
	}
	public void setTileSetFileName(String TileSetFileName)
	{
		mTileSetFileName=TileSetFileName;
	}
	public boolean getIsTransitionSource()
	{
		return mIsTransitionSource;
	}
	public void setIsTransitionSource(boolean IsTransitionSource)
	{
		mIsTransitionSource=IsTransitionSource;
	}
	public int getTransitionPriority()
	{
		return mTransitionPriority;
	}
	public void setTransitionPriority(int TransitionPriority)
	{
		mTransitionPriority=TransitionPriority;
	}
	public int getTransitionType()
	{
		return mTransitionType;
	}
	public void setTransitionType(int TransitionType)
	{
		mTransitionType=TransitionType;
	}
	public Vector<String> getTransitionChildrenNames()
	{
		return mTransitionChildrenNames;
	}
	public void setTransitionChildrenNames(Vector<String> TransitionChildrenNames)
	{
		mTransitionChildrenNames=TransitionChildrenNames;
	}
	public Vector<String> getVariantNames()
	{
		return mVariantNames;
	}
	public void setVariantNames(Vector<String> VariantNames)
	{
		mVariantNames=VariantNames;
	}
	public int getVariantPercent()
	{
		return mVariantPercent;
	}
	public void setVariantPercent(int VariantPercent)
	{
		mVariantPercent=VariantPercent;
	}
	public boolean getIsCompositeTile()
	{
		return mIsCompositeTile;
	}
	public void setIsCompositeTile(boolean IsCompositeTile)
	{
		mIsCompositeTile=IsCompositeTile;
	}
	public String getCompositeFile()
	{
		return mCompositeFile;
	}
	public void setCompositeFile(String CompositeFile)
	{
		mCompositeFile=CompositeFile;
	}
	public String getCompositeClass()
	{
		return mCompositeClass;
	}
	public void setCompositeClass(String CompositeClass)
	{
		mCompositeClass=CompositeClass;
	}
	public String getCompositeName()
	{
		return mCompositeName;
	}
	public void setCompositeName(String CompositeName)
	{
		mCompositeName=CompositeName;
	}
	public int getISOHeight()
	{
		return mISOHeight;
	}
	public void setISOHeight(int ISOHeight)
	{
		mISOHeight=ISOHeight;
	}
	public int getISOHeightOffset()
	{
		return mISOHeightOffset;
	}
	public void setISOHeightOffset(int ISOHeightOffset)
	{
		mISOHeightOffset=ISOHeightOffset;
	}
	public int getISOBaseline()
	{
		return mISOBaseline;
	}
	public void setISOBaseline(int ISOBaseline)
	{
		mISOBaseline=ISOBaseline;
	}
	public int getISOInherentOffset()
	{
		return mISOInherentOffset;
	}
	public void setISOInherentOffset(int ISOInherentOffset)
	{
		mISOInherentOffset=ISOInherentOffset;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<TileData>\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<IMAGEDATAFILE>"+UtilityString.toXML(mImageDataFile)+"</IMAGEDATAFILE>\n");
		s.append( "\t\t<IMAGEDATENAME>"+UtilityString.toXML(mImageDataName)+"</IMAGEDATENAME>\n");
		s.append( "\t\t<ForceTileSize>"+mForceTileSize+"</ForceTileSize>\n");
		s.append( "\t\t<TileActionLoop>"+mTileActionLoop+"</TileActionLoop>\n");
		s.append( "\t\t<TileActionCanBeBroken>"+mTileActionCanBeBroken+"</TileActionCanBeBroken>\n");
		s.append( "\t\t<TileActionSound>"+UtilityString.toXML(mTileActionSound)+"</TileActionSound>\n");
		s.append( "\t\t<TileActionIDss>\n");
		for (int i=0;i<mTileActionIDs.size(); i++)
		{
			s.append( "\t\t\t<TileActionIDs>"+UtilityString.toXML(mTileActionIDs.elementAt(i))+"</TileActionIDs>\n");
		}
		s.append( "\t\t</TileActionIDss>\n");
		s.append( "\t\t<TileActionTiless>\n");
		for (int i=0;i<mTileActionTiles.size(); i++)
		{
			s.append( "\t\t\t<TileActionTiles>"+UtilityString.toXML(mTileActionTiles.elementAt(i))+"</TileActionTiles>\n");
		}
		s.append( "\t\t</TileActionTiless>\n");
		s.append( "\t\t<TileSetFileName>"+UtilityString.toXML(mTileSetFileName)+"</TileSetFileName>\n");
		s.append( "\t\t<IsTransitionSource>"+mIsTransitionSource+"</IsTransitionSource>\n");
		s.append( "\t\t<TransitionPriority>"+mTransitionPriority+"</TransitionPriority>\n");
		s.append( "\t\t<TransitionType>"+mTransitionType+"</TransitionType>\n");
		s.append( "\t\t<TransitionChildrenNamess>\n");
		for (int i=0;i<mTransitionChildrenNames.size(); i++)
		{
			s.append( "\t\t\t<TransitionChildrenNames>"+UtilityString.toXML(mTransitionChildrenNames.elementAt(i))+"</TransitionChildrenNames>\n");
		}
		s.append( "\t\t</TransitionChildrenNamess>\n");
		s.append( "\t\t<VariantNamess>\n");
		for (int i=0;i<mVariantNames.size(); i++)
		{
			s.append( "\t\t\t<VariantNames>"+UtilityString.toXML(mVariantNames.elementAt(i))+"</VariantNames>\n");
		}
		s.append( "\t\t</VariantNamess>\n");
		s.append( "\t\t<VariantPercent>"+mVariantPercent+"</VariantPercent>\n");
		s.append( "\t\t<IsCompositeTile>"+mIsCompositeTile+"</IsCompositeTile>\n");
		s.append( "\t\t<CompositeFile>"+UtilityString.toXML(mCompositeFile)+"</CompositeFile>\n");
		s.append( "\t\t<CompositeClass>"+UtilityString.toXML(mCompositeClass)+"</CompositeClass>\n");
		s.append( "\t\t<CompositeName>"+UtilityString.toXML(mCompositeName)+"</CompositeName>\n");
		s.append( "\t\t<ISOHeight>"+mISOHeight+"</ISOHeight>\n");
		s.append( "\t\t<ISOHeightOffset>"+mISOHeightOffset+"</ISOHeightOffset>\n");
		s.append( "\t\t<ISOBaseline>"+mISOBaseline+"</ISOBaseline>\n");
		s.append( "\t\t<ISOInherentOffset>"+mISOInherentOffset+"</ISOInherentOffset>\n");
		s.append( "\t</TileData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static TileDataXMLHandler XMLHANDLER = new TileDataXMLHandler();
	public static TileDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<TileData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllTileData>\n");
			Iterator<TileData> iter = col.iterator();
			while (iter.hasNext())
			{
				TileData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllTileData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, TileData> getHashMapFromXML(String filename)
	{
		HashMap<String, TileData> filters = new HashMap<String, TileData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			TileDataXMLHandler h = TileData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"TileData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
