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  GridSettingsData
{
	protected String mClass="";
	public String mName="";
	protected Vector<Integer> meveryX=new Vector<Integer>();
	protected Vector<Integer> mXOffset=new Vector<Integer>();
	protected Vector<Integer> meveryY=new Vector<Integer>();
	protected Vector<Integer> mYOffset=new Vector<Integer>();
	protected int mDrawTyp=0;
	protected boolean mSHowHotSpot=false;
	protected int mGridColor=0;
	protected boolean mXAlternating=false;
	protected boolean mYAlternating=false;
	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<Integer> geteveryX()
	{
		return meveryX;
	}
	public void seteveryX(Vector<Integer> everyX)
	{
		meveryX=everyX;
	}
	public Vector<Integer> getXOffset()
	{
		return mXOffset;
	}
	public void setXOffset(Vector<Integer> XOffset)
	{
		mXOffset=XOffset;
	}
	public Vector<Integer> geteveryY()
	{
		return meveryY;
	}
	public void seteveryY(Vector<Integer> everyY)
	{
		meveryY=everyY;
	}
	public Vector<Integer> getYOffset()
	{
		return mYOffset;
	}
	public void setYOffset(Vector<Integer> YOffset)
	{
		mYOffset=YOffset;
	}
	public int getDrawTyp()
	{
		return mDrawTyp;
	}
	public void setDrawTyp(int DrawTyp)
	{
		mDrawTyp=DrawTyp;
	}
	public boolean getSHowHotSpot()
	{
		return mSHowHotSpot;
	}
	public void setSHowHotSpot(boolean SHowHotSpot)
	{
		mSHowHotSpot=SHowHotSpot;
	}
	public int getGridColor()
	{
		return mGridColor;
	}
	public void setGridColor(int GridColor)
	{
		mGridColor=GridColor;
	}
	public boolean getXAlternating()
	{
		return mXAlternating;
	}
	public void setXAlternating(boolean XAlternating)
	{
		mXAlternating=XAlternating;
	}
	public boolean getYAlternating()
	{
		return mYAlternating;
	}
	public void setYAlternating(boolean YAlternating)
	{
		mYAlternating=YAlternating;
	}
	private String exportXML()
	{
		String s = new String();
		s += "\t<GridSettingsData>\n";
		s += "\t\t<Class>"+UtilityString.toXML(mClass)+"</Class>\n";
		s += "\t\t<Name>"+UtilityString.toXML(mName)+"</Name>\n";
		s += "\t\t<everyXs>\n";
		for (int i=0;i<meveryX.size(); i++)
		{
			s += "\t\t\t<everyX>"+meveryX.elementAt(i)+"</everyX>\n";
		}
		s += "\t\t</everyXs>\n";
		s += "\t\t<XOffsets>\n";
		for (int i=0;i<mXOffset.size(); i++)
		{
			s += "\t\t\t<XOffset>"+mXOffset.elementAt(i)+"</XOffset>\n";
		}
		s += "\t\t</XOffsets>\n";
		s += "\t\t<everyYs>\n";
		for (int i=0;i<meveryY.size(); i++)
		{
			s += "\t\t\t<everyY>"+meveryY.elementAt(i)+"</everyY>\n";
		}
		s += "\t\t</everyYs>\n";
		s += "\t\t<YOffsets>\n";
		for (int i=0;i<mYOffset.size(); i++)
		{
			s += "\t\t\t<YOffset>"+mYOffset.elementAt(i)+"</YOffset>\n";
		}
		s += "\t\t</YOffsets>\n";
		s += "\t\t<DrawTyp>"+mDrawTyp+"</DrawTyp>\n";
		s += "\t\t<SHowHotSpot>"+mSHowHotSpot+"</SHowHotSpot>\n";
		s += "\t\t<GridColor>"+mGridColor+"</GridColor>\n";
		s += "\t\t<XAlternating>"+mXAlternating+"</XAlternating>\n";
		s += "\t\t<YAlternating>"+mYAlternating+"</YAlternating>\n";
		s += "\t</GridSettingsData>\n";
		return s;
	}
	@Override public String toString()
	{
		return mName;
	}
	private static GridSettingsDataXMLHandler XMLHANDLER = new GridSettingsDataXMLHandler();
	public static GridSettingsDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<GridSettingsData> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllGridSettingsData>\n");
			Iterator<GridSettingsData> iter = col.iterator();
			while (iter.hasNext())
			{
				GridSettingsData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllGridSettingsData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, GridSettingsData> getHashMapFromXML(String filename)
	{
		HashMap<String, GridSettingsData> filters = new HashMap<String, GridSettingsData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			GridSettingsDataXMLHandler h = GridSettingsData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"GridSettingsData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
