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  LayerData
{
	protected String mClass="";
	public String mName="";
	protected boolean mWrapHorizontal=false;
	protected boolean mWrapVertical=false;
	protected String mLayerName="";
	protected int mverticalSpeed=0;
	protected int mhorizontalSpeed=0;
	protected boolean mmustBePainted=false;
	protected boolean misBackgroundCollision=false;
	protected boolean misForgroundCollision=false;
	protected String mpurposeTyp="";
	protected boolean misChangeable=false;
	protected int mbackgroundColor=0;
	protected int mLayerNumber=0;
	protected Vector<Integer> mtileX=new Vector<Integer>();
	protected Vector<Integer> mtileY=new Vector<Integer>();
	protected boolean misBackground=false;
	protected Vector<String> mtileName=new Vector<String>();
	protected boolean misVisible=false;
	protected String mscrollBehaviour="";
	protected boolean mallowTileOverlap=false;
	protected Vector<String> musedTileCollection=new Vector<String>();
	protected int moffsetX=0;
	protected int moffsetY=0;
	protected boolean misLayerOrdered=false;
	protected boolean misLayerCompleteOrdered=false;
	protected boolean misAllTilesSameSize=false;
	protected boolean misRegularGrid=false;
	protected int msaveTyp=0;
	protected boolean monlyOneTileOnOnePosition=false;
	protected boolean mISOBlockWrap=false;
	protected Vector<String> mSpriteFilename=new Vector<String>();
	protected Vector<String> mSpriteName=new Vector<String>();
	protected Vector<Integer> mSpriteStartX=new Vector<Integer>();
	protected Vector<Integer> mSpriteStartY=new Vector<Integer>();
	protected Vector<String> mSpriteStartAction=new Vector<String>();
	protected Vector<String> mSpriteStartDirection=new Vector<String>();
	protected Vector<String> mSpriteCoordinator=new Vector<String>();
	protected String mFogFileName="";
	protected String mFogName="";
	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 boolean getWrapHorizontal()
	{
		return mWrapHorizontal;
	}
	public void setWrapHorizontal(boolean WrapHorizontal)
	{
		mWrapHorizontal=WrapHorizontal;
	}
	public boolean getWrapVertical()
	{
		return mWrapVertical;
	}
	public void setWrapVertical(boolean WrapVertical)
	{
		mWrapVertical=WrapVertical;
	}
	public String getLayerName()
	{
		return mLayerName;
	}
	public void setLayerName(String LayerName)
	{
		mLayerName=LayerName;
	}
	public int getverticalSpeed()
	{
		return mverticalSpeed;
	}
	public void setverticalSpeed(int verticalSpeed)
	{
		mverticalSpeed=verticalSpeed;
	}
	public int gethorizontalSpeed()
	{
		return mhorizontalSpeed;
	}
	public void sethorizontalSpeed(int horizontalSpeed)
	{
		mhorizontalSpeed=horizontalSpeed;
	}
	public boolean getmustBePainted()
	{
		return mmustBePainted;
	}
	public void setmustBePainted(boolean mustBePainted)
	{
		mmustBePainted=mustBePainted;
	}
	public boolean getisBackgroundCollision()
	{
		return misBackgroundCollision;
	}
	public void setisBackgroundCollision(boolean isBackgroundCollision)
	{
		misBackgroundCollision=isBackgroundCollision;
	}
	public boolean getisForgroundCollision()
	{
		return misForgroundCollision;
	}
	public void setisForgroundCollision(boolean isForgroundCollision)
	{
		misForgroundCollision=isForgroundCollision;
	}
	public String getpurposeTyp()
	{
		return mpurposeTyp;
	}
	public void setpurposeTyp(String purposeTyp)
	{
		mpurposeTyp=purposeTyp;
	}
	public boolean getisChangeable()
	{
		return misChangeable;
	}
	public void setisChangeable(boolean isChangeable)
	{
		misChangeable=isChangeable;
	}
	public int getbackgroundColor()
	{
		return mbackgroundColor;
	}
	public void setbackgroundColor(int backgroundColor)
	{
		mbackgroundColor=backgroundColor;
	}
	public int getLayerNumber()
	{
		return mLayerNumber;
	}
	public void setLayerNumber(int LayerNumber)
	{
		mLayerNumber=LayerNumber;
	}
	public Vector<Integer> gettileX()
	{
		return mtileX;
	}
	public void settileX(Vector<Integer> tileX)
	{
		mtileX=tileX;
	}
	public Vector<Integer> gettileY()
	{
		return mtileY;
	}
	public void settileY(Vector<Integer> tileY)
	{
		mtileY=tileY;
	}
	public boolean getisBackground()
	{
		return misBackground;
	}
	public void setisBackground(boolean isBackground)
	{
		misBackground=isBackground;
	}
	public Vector<String> gettileName()
	{
		return mtileName;
	}
	public void settileName(Vector<String> tileName)
	{
		mtileName=tileName;
	}
	public boolean getisVisible()
	{
		return misVisible;
	}
	public void setisVisible(boolean isVisible)
	{
		misVisible=isVisible;
	}
	public String getscrollBehaviour()
	{
		return mscrollBehaviour;
	}
	public void setscrollBehaviour(String scrollBehaviour)
	{
		mscrollBehaviour=scrollBehaviour;
	}
	public boolean getallowTileOverlap()
	{
		return mallowTileOverlap;
	}
	public void setallowTileOverlap(boolean allowTileOverlap)
	{
		mallowTileOverlap=allowTileOverlap;
	}
	public Vector<String> getusedTileCollection()
	{
		return musedTileCollection;
	}
	public void setusedTileCollection(Vector<String> usedTileCollection)
	{
		musedTileCollection=usedTileCollection;
	}
	public int getoffsetX()
	{
		return moffsetX;
	}
	public void setoffsetX(int offsetX)
	{
		moffsetX=offsetX;
	}
	public int getoffsetY()
	{
		return moffsetY;
	}
	public void setoffsetY(int offsetY)
	{
		moffsetY=offsetY;
	}
	public boolean getisLayerOrdered()
	{
		return misLayerOrdered;
	}
	public void setisLayerOrdered(boolean isLayerOrdered)
	{
		misLayerOrdered=isLayerOrdered;
	}
	public boolean getisLayerCompleteOrdered()
	{
		return misLayerCompleteOrdered;
	}
	public void setisLayerCompleteOrdered(boolean isLayerCompleteOrdered)
	{
		misLayerCompleteOrdered=isLayerCompleteOrdered;
	}
	public boolean getisAllTilesSameSize()
	{
		return misAllTilesSameSize;
	}
	public void setisAllTilesSameSize(boolean isAllTilesSameSize)
	{
		misAllTilesSameSize=isAllTilesSameSize;
	}
	public boolean getisRegularGrid()
	{
		return misRegularGrid;
	}
	public void setisRegularGrid(boolean isRegularGrid)
	{
		misRegularGrid=isRegularGrid;
	}
	public int getsaveTyp()
	{
		return msaveTyp;
	}
	public void setsaveTyp(int saveTyp)
	{
		msaveTyp=saveTyp;
	}
	public boolean getonlyOneTileOnOnePosition()
	{
		return monlyOneTileOnOnePosition;
	}
	public void setonlyOneTileOnOnePosition(boolean onlyOneTileOnOnePosition)
	{
		monlyOneTileOnOnePosition=onlyOneTileOnOnePosition;
	}
	public boolean getISOBlockWrap()
	{
		return mISOBlockWrap;
	}
	public void setISOBlockWrap(boolean ISOBlockWrap)
	{
		mISOBlockWrap=ISOBlockWrap;
	}
	public Vector<String> getSpriteFilename()
	{
		return mSpriteFilename;
	}
	public void setSpriteFilename(Vector<String> SpriteFilename)
	{
		mSpriteFilename=SpriteFilename;
	}
	public Vector<String> getSpriteName()
	{
		return mSpriteName;
	}
	public void setSpriteName(Vector<String> SpriteName)
	{
		mSpriteName=SpriteName;
	}
	public Vector<Integer> getSpriteStartX()
	{
		return mSpriteStartX;
	}
	public void setSpriteStartX(Vector<Integer> SpriteStartX)
	{
		mSpriteStartX=SpriteStartX;
	}
	public Vector<Integer> getSpriteStartY()
	{
		return mSpriteStartY;
	}
	public void setSpriteStartY(Vector<Integer> SpriteStartY)
	{
		mSpriteStartY=SpriteStartY;
	}
	public Vector<String> getSpriteStartAction()
	{
		return mSpriteStartAction;
	}
	public void setSpriteStartAction(Vector<String> SpriteStartAction)
	{
		mSpriteStartAction=SpriteStartAction;
	}
	public Vector<String> getSpriteStartDirection()
	{
		return mSpriteStartDirection;
	}
	public void setSpriteStartDirection(Vector<String> SpriteStartDirection)
	{
		mSpriteStartDirection=SpriteStartDirection;
	}
	public Vector<String> getSpriteCoordinator()
	{
		return mSpriteCoordinator;
	}
	public void setSpriteCoordinator(Vector<String> SpriteCoordinator)
	{
		mSpriteCoordinator=SpriteCoordinator;
	}
	public String getFogFileName()
	{
		return mFogFileName;
	}
	public void setFogFileName(String FogFileName)
	{
		mFogFileName=FogFileName;
	}
	public String getFogName()
	{
		return mFogName;
	}
	public void setFogName(String FogName)
	{
		mFogName=FogName;
	}
	private String exportXML()
	{
		StringBuffer s = new StringBuffer();
		s.append( "\t<LayerData>\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<WrapHorizontal>"+mWrapHorizontal+"</WrapHorizontal>\n");
		s.append( "\t\t<WrapVerticalHorizontal>"+mWrapVertical+"</WrapVerticalHorizontal>\n");
		s.append( "\t\t<LayerName>"+UtilityString.toXML(mLayerName)+"</LayerName>\n");
		s.append( "\t\t<verticalSpeed>"+mverticalSpeed+"</verticalSpeed>\n");
		s.append( "\t\t<horizontalSpeed>"+mhorizontalSpeed+"</horizontalSpeed>\n");
		s.append( "\t\t<mustBePainted>"+mmustBePainted+"</mustBePainted>\n");
		s.append( "\t\t<isBackgroundCollision>"+misBackgroundCollision+"</isBackgroundCollision>\n");
		s.append( "\t\t<isForgroundCollision>"+misForgroundCollision+"</isForgroundCollision>\n");
		s.append( "\t\t<purposeTyp>"+UtilityString.toXML(mpurposeTyp)+"</purposeTyp>\n");
		s.append( "\t\t<isChangeable>"+misChangeable+"</isChangeable>\n");
		s.append( "\t\t<backgroundColor>"+mbackgroundColor+"</backgroundColor>\n");
		s.append( "\t\t<LayerNumber>"+mLayerNumber+"</LayerNumber>\n");
		s.append( "\t\t<tileXs>\n");
		for (int i=0;i<mtileX.size(); i++)
		{
			s.append( "\t\t\t<tileX>"+mtileX.elementAt(i)+"</tileX>\n");
		}
		s.append( "\t\t</tileXs>\n");
		s.append( "\t\t<tileYs>\n");
		for (int i=0;i<mtileY.size(); i++)
		{
			s.append( "\t\t\t<tileY>"+mtileY.elementAt(i)+"</tileY>\n");
		}
		s.append( "\t\t</tileYs>\n");
		s.append( "\t\t<isBackground>"+misBackground+"</isBackground>\n");
		s.append( "\t\t<tileNames>\n");
		for (int i=0;i<mtileName.size(); i++)
		{
			s.append( "\t\t\t<tileName>"+UtilityString.toXML(mtileName.elementAt(i))+"</tileName>\n");
		}
		s.append( "\t\t</tileNames>\n");
		s.append( "\t\t<isVisible>"+misVisible+"</isVisible>\n");
		s.append( "\t\t<scrollBehaviour>"+UtilityString.toXML(mscrollBehaviour)+"</scrollBehaviour>\n");
		s.append( "\t\t<allowTileOverlap>"+mallowTileOverlap+"</allowTileOverlap>\n");
		s.append( "\t\t<usedTileCollections>\n");
		for (int i=0;i<musedTileCollection.size(); i++)
		{
			s.append( "\t\t\t<usedTileCollection>"+UtilityString.toXML(musedTileCollection.elementAt(i))+"</usedTileCollection>\n");
		}
		s.append( "\t\t</usedTileCollections>\n");
		s.append( "\t\t<offsetX>"+moffsetX+"</offsetX>\n");
		s.append( "\t\t<offsetY>"+moffsetY+"</offsetY>\n");
		s.append( "\t\t<isLayerOrdered>"+misLayerOrdered+"</isLayerOrdered>\n");
		s.append( "\t\t<isLayerCompleteOrdered>"+misLayerCompleteOrdered+"</isLayerCompleteOrdered>\n");
		s.append( "\t\t<isAllTilesSameSize>"+misAllTilesSameSize+"</isAllTilesSameSize>\n");
		s.append( "\t\t<isRegularGrid>"+misRegularGrid+"</isRegularGrid>\n");
		s.append( "\t\t<saveTyp>"+msaveTyp+"</saveTyp>\n");
		s.append( "\t\t<onlyOneTileOnOnePosition>"+monlyOneTileOnOnePosition+"</onlyOneTileOnOnePosition>\n");
		s.append( "\t\t<ISOBlockWrap>"+mISOBlockWrap+"</ISOBlockWrap>\n");
		s.append( "\t\t<SpriteFilenames>\n");
		for (int i=0;i<mSpriteFilename.size(); i++)
		{
			s.append( "\t\t\t<SpriteFilename>"+UtilityString.toXML(mSpriteFilename.elementAt(i))+"</SpriteFilename>\n");
		}
		s.append( "\t\t</SpriteFilenames>\n");
		s.append( "\t\t<SpriteNames>\n");
		for (int i=0;i<mSpriteName.size(); i++)
		{
			s.append( "\t\t\t<SpriteName>"+UtilityString.toXML(mSpriteName.elementAt(i))+"</SpriteName>\n");
		}
		s.append( "\t\t</SpriteNames>\n");
		s.append( "\t\t<SpriteStartXs>\n");
		for (int i=0;i<mSpriteStartX.size(); i++)
		{
			s.append( "\t\t\t<SpriteStartX>"+mSpriteStartX.elementAt(i)+"</SpriteStartX>\n");
		}
		s.append( "\t\t</SpriteStartXs>\n");
		s.append( "\t\t<SpriteStartYs>\n");
		for (int i=0;i<mSpriteStartY.size(); i++)
		{
			s.append( "\t\t\t<SpriteStartY>"+mSpriteStartY.elementAt(i)+"</SpriteStartY>\n");
		}
		s.append( "\t\t</SpriteStartYs>\n");
		s.append( "\t\t<SpriteStartActions>\n");
		for (int i=0;i<mSpriteStartAction.size(); i++)
		{
			s.append( "\t\t\t<SpriteStartAction>"+UtilityString.toXML(mSpriteStartAction.elementAt(i))+"</SpriteStartAction>\n");
		}
		s.append( "\t\t</SpriteStartActions>\n");
		s.append( "\t\t<SpriteStartDirections>\n");
		for (int i=0;i<mSpriteStartDirection.size(); i++)
		{
			s.append( "\t\t\t<SpriteStartDirection>"+UtilityString.toXML(mSpriteStartDirection.elementAt(i))+"</SpriteStartDirection>\n");
		}
		s.append( "\t\t</SpriteStartDirections>\n");
		s.append( "\t\t<SpriteCoordinators>\n");
		for (int i=0;i<mSpriteCoordinator.size(); i++)
		{
			s.append( "\t\t\t<SpriteCoordinator>"+UtilityString.toXML(mSpriteCoordinator.elementAt(i))+"</SpriteCoordinator>\n");
		}
		s.append( "\t\t</SpriteCoordinators>\n");
		s.append( "\t\t<FogFileName>"+UtilityString.toXML(mFogFileName)+"</FogFileName>\n");
		s.append( "\t\t<FogName>"+UtilityString.toXML(mFogName)+"</FogName>\n");
		s.append( "\t</LayerData>\n");
		return s.toString();
	}
	@Override public String toString()
	{
		return mName;
	}
	private static LayerDataXMLHandler XMLHANDLER = new LayerDataXMLHandler();
	public static LayerDataXMLHandler getXMLParseHandler()
	{
		return XMLHANDLER;
	}
	public static boolean saveCollectionAsXML(String filename, Collection<LayerData> col, int saveTyp)
	{
                if (saveTyp==1)
                {
                    Vector<LayerData> vData = new Vector<LayerData>();
                    Iterator<LayerData> iter = col.iterator();
                    while (iter.hasNext())
                    {
                        LayerData item = iter.next();
                        vData.addElement(item);
                    }
                    String serName = csa.util.UtilityString.replace(csa.Global.mBaseDir+filename, "Data.xml", "Data.ser");

                    OutputStream outputStream = null;
                    try
                    {
                        // Byteorientierten Ausgabekanal Ãffnen
                        outputStream = new FileOutputStream(serName);

                        // Objektausgabekanal fÃ¼r Serialisierung Ãffnen
                        ObjectOutputStream objectOutput = new ObjectOutputStream(outputStream);

                        objectOutput.writeObject(vData);

                        // Ausgabekanal schlieÃen
                        objectOutput.close();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    } finally
                    {
                        try
                        {
                            outputStream.close();
                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

                    return true;
                }
                try
		{
			PrintWriter pw = new PrintWriter(csa.Global.mBaseDir+filename, "UTF-8");
			pw.print("<?xml version=\"1.0\"?>\n");
			pw.print("<AllLayerData>\n");
			Iterator<LayerData> iter = col.iterator();
			while (iter.hasNext())
			{
				LayerData item = iter.next();
				pw.print(item.exportXML());
			}
			pw.print("</AllLayerData>\n");
			pw.close();
		}
		catch (IOException e)
		{
			System.err.println(e.toString());
			return false;
		}
		return true;
	}
	public static HashMap<String, LayerData> getHashMapFromXML(String filename)
	{
		HashMap<String, LayerData> filters = new HashMap<String, LayerData>();
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			LayerDataXMLHandler h = LayerData.getXMLParseHandler();
			saxParser.parse(csa.Global.mBaseDir+filename, h);
			filters = h.getLastHashMap();
		}
		catch (Throwable e)
		{
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, e.toString() ,"LayerData Load Error...",  JOptionPane.INFORMATION_MESSAGE);
		}
		return filters;
	}
}
