package csa.tiletools.map;

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

public class  ColorTileMap
{
	protected String mClass="";
	public String mName="";
	protected Vector<Integer> mColor=new Vector<Integer>();
	protected Vector<BufferedImage> mImages=new Vector<BufferedImage>();
	protected Vector<String> mTileFile=new Vector<String>();
	protected Vector<String> mTileName=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<Integer> getColor()
	{
		return mColor;
	}
	public void setColor(Vector<Integer> Color)
	{
		mColor=Color;
	}
	public Vector<String> getTileFile()
	{
		return mTileFile;
	}
	public void setTileFile(Vector<String> TileFile)
	{
		mTileFile=TileFile;
	}
	public Vector<String> getTileName()
	{
		return mTileName;
	}
	public void setTileName(Vector<String> TileName)
	{
		mTileName=TileName;
	}
	private String exportXML()
	{
		String s = new String();
		s += "\t<ColorTileMap>\n";
		s += "\t\t<Class>"+UtilityString.toXML(mClass)+"</Class>\n";
		s += "\t\t<Name>"+UtilityString.toXML(mName)+"</Name>\n";
		s += "\t\t<Colors>\n";
		for (int i=0;i<mColor.size(); i++)
		{
			s += "\t\t\t<Color>"+mColor.elementAt(i)+"</Color>\n";
		}
		s += "\t\t</Colors>\n";
		s += "\t\t<TileFiles>\n";
		for (int i=0;i<mTileFile.size(); i++)
		{
			s += "\t\t\t<TileFile>"+UtilityString.toXML(mTileFile.elementAt(i))+"</TileFile>\n";
		}
		s += "\t\t</TileFiles>\n";
		s += "\t\t<TileNames>\n";
		for (int i=0;i<mTileName.size(); i++)
		{
			s += "\t\t\t<TileName>"+UtilityString.toXML(mTileName.elementAt(i))+"</TileName>\n";
		}
		s += "\t\t</TileNames>\n";
		s += "\t</ColorTileMap>\n";
		return s;
	}
	@Override public String toString()
	{
		return mName;
	}
	private static ColorTileMapXMLHandler XMLHANDLER = new ColorTileMapXMLHandler();
	public static ColorTileMapXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<ColorTileMap> col)
	{
		try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllColorTileMap>\n");
			Iterator<ColorTileMap> iter = col.iterator();
			while (iter.hasNext())
			{
				ColorTileMap item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllColorTileMap>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, ColorTileMap> getHashMapFromXML(String filename)
	{
		HashMap<String, ColorTileMap> filters = new HashMap<String, ColorTileMap>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			ColorTileMapXMLHandler h = ColorTileMap.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"ColorTileMap Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
