package csa.tiletools.map;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import javax.swing.JOptionPane;
import java.util.*;
public class  LayerDataPool
{
	public static final String DEFAULT_XML_NAME = new String("LayerData.xml");
	private String mFileName = DEFAULT_XML_NAME;
	private HashMap<String, LayerData> mLayerData = new HashMap<String, LayerData>();
	private HashMap<String, String> mKlassenMap = new HashMap<String, String>();
	public LayerDataPool(String name)
	{
		mFileName = name;
		init();
	}
	public LayerDataPool()
	{
		init();
	}
	public void setFilename(String n)
	{
		mFileName=n;
	}
	private boolean init()
	{
		try
		{
			return load();
		}
		catch (Throwable e)
		{
			JOptionPane.showMessageDialog(null, e.toString() ,"Load Error LayerData...",  JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
	}
	public boolean load()
	{
            // first look if there is a ser file!
            String serName = csa.util.UtilityString.replace(csa.Global.mBaseDir+mFileName, "Data.xml", "Data.ser");
            mLayerData = new HashMap<String, LayerData>();
            File ft = new File(serName);
            if (ft.exists())
            {
                try
                {
                    Vector<LayerData> vData = new Vector<LayerData>();
                    FileInputStream inputStream = new FileInputStream(serName);
                    ObjectInputStream objectInput = new ObjectInputStream(inputStream);

                    vData = (Vector<LayerData>) objectInput.readObject();

                    // Ausgabekanal schließen
                    inputStream.close();

                    for (int i = 0; i < vData.size(); i++)
                    {
                        LayerData object = vData.elementAt(i);
                        mLayerData.put(object.mName, object);
                    }

                } catch (Throwable e)
                {
                    e.printStackTrace();
                }
                return true;
            }

            java.io.File f = new java.io.File(csa.Global.mBaseDir+mFileName);
		if (!f.exists()) return false;
		mLayerData = LayerData.getHashMapFromXML(mFileName);
		return true;
	}
	public void save(int saveTyp)
	{
		LayerData.saveCollectionAsXML(mFileName, mLayerData.values(), saveTyp);
		buildKlassenMap();
	}
	public void remove(LayerData st)
	{
		mLayerData.remove(st.mName);
	}
	public void put(LayerData st)
	{
		mLayerData.remove(st.mName);
		mLayerData.put(st.mName, st);
	}
	public void putAsNew(LayerData st)
	{
		mLayerData.put(st.mName, st);
	}
	public LayerData get(String key)
	{
		return mLayerData.get(key);
	}
	public HashMap<String, LayerData> getHashMap()
	{
		return mLayerData;
	}
	private void buildKlassenMap()
	{
		mKlassenMap = new HashMap<String, String>();
		Set entries = mLayerData.entrySet();
		Iterator it = entries.iterator();
		while (it.hasNext())
		{
			Map.Entry entry = (Map.Entry) it.next();
			LayerData value = (LayerData) entry.getValue();
			mKlassenMap.put(value.mClass, value.mClass);
		}
	}
	public HashMap<String, String> getKlassenHashMap()
	{
		buildKlassenMap();
		return mKlassenMap;
	}
	public HashMap<String, LayerData> getMapForKlasse(String klasse)
	{
		HashMap<String, LayerData> ret = new HashMap<String, LayerData>();
		Set entries = mLayerData.entrySet();
		Iterator it = entries.iterator();
		while (it.hasNext())
		{
			Map.Entry entry = (Map.Entry) it.next();
			LayerData value = (LayerData) entry.getValue();
			if (value.mClass.equalsIgnoreCase(klasse))
			{
				ret.put(value.mName, value);
			}
		}
		 return ret;
	}
}
