package csa.tiletools.map;

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

public class  TileGrowthData
{
	protected String mClass="";
	public String mName="";
	protected Vector<String> mTileCollectionFilename=new Vector<String>();
	protected Vector<String> mTileCollectionClass=new Vector<String>();
	protected Vector<String> mTypesToGrowFrom=new Vector<String>();
	protected Vector<String> mTypesToGrow=new Vector<String>();
	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> getTileCollectionFilename()
	{
		return mTileCollectionFilename;
	}
	public void setTileCollectionFilename(Vector<String> TileCollectionFilename)
	{
		mTileCollectionFilename=TileCollectionFilename;
	}
	public Vector<String> getTileCollectionClass()
	{
		return mTileCollectionClass;
	}
	public void setTileCollectionClass(Vector<String> TileCollectionClass)
	{
		mTileCollectionClass=TileCollectionClass;
	}
	public Vector<String> getTypesToGrowFrom()
	{
		return mTypesToGrowFrom;
	}
	public void setTypesToGrowFrom(Vector<String> TypesToGrowFrom)
	{
		mTypesToGrowFrom=TypesToGrowFrom;
	}
	public Vector<String> getTypesToGrow()
	{
		return mTypesToGrow;
	}
	public void setTypesToGrow(Vector<String> TypesToGrow)
	{
		mTypesToGrow=TypesToGrow;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<TileGrowthData>\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<TileCollectionFilenames>\n");
		for (int i=0;i<mTileCollectionFilename.size(); i++)
		{
			s.append( "\t\t\t<TileCollectionFilename>"+UtilityString.toXML(mTileCollectionFilename.elementAt(i))+"</TileCollectionFilename>\n");
		}
		s.append( "\t\t</TileCollectionFilenames>\n");
		s.append( "\t\t<TileCollectionClasss>\n");
		for (int i=0;i<mTileCollectionClass.size(); i++)
		{
			s.append( "\t\t\t<TileCollectionClass>"+UtilityString.toXML(mTileCollectionClass.elementAt(i))+"</TileCollectionClass>\n");
		}
		s.append( "\t\t</TileCollectionClasss>\n");
		s.append( "\t\t<TypesToGrowFroms>\n");
		for (int i=0;i<mTypesToGrowFrom.size(); i++)
		{
			s.append( "\t\t\t<TypesToGrowFrom>"+UtilityString.toXML(mTypesToGrowFrom.elementAt(i))+"</TypesToGrowFrom>\n");
		}
		s.append( "\t\t</TypesToGrowFroms>\n");
		s.append( "\t\t<TypesToGrows>\n");
		for (int i=0;i<mTypesToGrow.size(); i++)
		{
			s.append( "\t\t\t<TypesToGrow>"+UtilityString.toXML(mTypesToGrow.elementAt(i))+"</TypesToGrow>\n");
		}
		s.append( "\t\t</TypesToGrows>\n");
		s.append( "\t</TileGrowthData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static TileGrowthDataXMLHandler XMLHANDLER = new TileGrowthDataXMLHandler();
	public static TileGrowthDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<TileGrowthData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllTileGrowthData>\n");
			Iterator<TileGrowthData> iter = col.iterator();
			while (iter.hasNext())
			{
				TileGrowthData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllTileGrowthData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, TileGrowthData> getHashMapFromXML(String filename)
	{
		HashMap<String, TileGrowthData> filters = new HashMap<String, TileGrowthData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			TileGrowthDataXMLHandler h = TileGrowthData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"TileGrowthData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
