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  MapData
{
	protected String mClass="";
	public String mName="";
	protected Vector<String> mLayerData=new Vector<String>();
	protected int mSizeX=0;
	protected int mSizeY=0;
	protected boolean mSizeByTiles=false;
	protected boolean mWrapHorizontal=false;
	protected boolean mWrapVertical=false;
	protected boolean mAllowTileOverlap=false;
	protected boolean msizePerLayer=false;
	protected String mGridSettings="";
	protected int mLayerSaveTyp=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> getLayerData()
	{
		return mLayerData;
	}
	public void setLayerData(Vector<String> LayerData)
	{
		mLayerData=LayerData;
	}
	public int getSizeX()
	{
		return mSizeX;
	}
	public void setSizeX(int SizeX)
	{
		mSizeX=SizeX;
	}
	public int getSizeY()
	{
		return mSizeY;
	}
	public void setSizeY(int SizeY)
	{
		mSizeY=SizeY;
	}
	public boolean getSizeByTiles()
	{
		return mSizeByTiles;
	}
	public void setSizeByTiles(boolean SizeByTiles)
	{
		mSizeByTiles=SizeByTiles;
	}
	public boolean getWrapHorizontal()
	{
		return mWrapHorizontal;
	}
	public void setWrapHorizontal(boolean WrapHorizontal)
	{
		mWrapHorizontal=WrapHorizontal;
	}
	public boolean getWrapVertical()
	{
		return mWrapVertical;
	}
	public void setWrapVertical(boolean WrapVertical)
	{
		mWrapVertical=WrapVertical;
	}
	public boolean getAllowTileOverlap()
	{
		return mAllowTileOverlap;
	}
	public void setAllowTileOverlap(boolean AllowTileOverlap)
	{
		mAllowTileOverlap=AllowTileOverlap;
	}
	public boolean getsizePerLayer()
	{
		return msizePerLayer;
	}
	public void setsizePerLayer(boolean sizePerLayer)
	{
		msizePerLayer=sizePerLayer;
	}
	public String getGridSettings()
	{
		return mGridSettings;
	}
	public void setGridSettings(String GridSettings)
	{
		mGridSettings=GridSettings;
	}
	public int getLayerSaveTyp()
	{
		return mLayerSaveTyp;
	}
	public void setLayerSaveTyp(int LayerSaveTyp)
	{
		mLayerSaveTyp=LayerSaveTyp;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s .append("\t<MapData>\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<LayerDatas>\n");
		for (int i=0;i<mLayerData.size(); i++)
		{
			s.append( "\t\t\t<LayerData>"+UtilityString.toXML(mLayerData.elementAt(i))+"</LayerData>\n");
		}
		s.append( "\t\t</LayerDatas>\n");
		s.append( "\t\t<sizeX>"+mSizeX+"</sizeX>\n");
		s.append( "\t\t<sizeY>"+mSizeY+"</sizeY>\n");
		s.append( "\t\t<sizeByTiles>"+mSizeByTiles+"</sizeByTiles>\n");
		s.append( "\t\t<wrapHorizontal>"+mWrapHorizontal+"</wrapHorizontal>\n");
		s.append( "\t\t<wrapVertical>"+mWrapVertical+"</wrapVertical>\n");
		s.append( "\t\t<AllowTileOverlap>"+mAllowTileOverlap+"</AllowTileOverlap>\n");
		s.append( "\t\t<sizePerLayer>"+msizePerLayer+"</sizePerLayer>\n");
		s.append( "\t\t<GridSettings>"+UtilityString.toXML(mGridSettings)+"</GridSettings>\n");
		s.append( "\t\t<LayerSaveTyp>"+mLayerSaveTyp+"</LayerSaveTyp>\n");
		s.append( "\t</MapData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static MapDataXMLHandler XMLHANDLER = new MapDataXMLHandler();
	public static MapDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<MapData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllMapData>\n");
			Iterator<MapData> iter = col.iterator();
			while (iter.hasNext())
			{
				MapData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllMapData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, MapData> getHashMapFromXML(String filename)
	{
		HashMap<String, MapData> filters = new HashMap<String, MapData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			MapDataXMLHandler h = MapData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"MapData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
