package net.minecraft.world.storage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagShort;
import net.minecraft.world.WorldSavedData;

public class MapStorage
{
	private ISaveHandler saveHandler;
	private Map loadedDataMap = new HashMap();
	private List loadedDataList = new ArrayList();
	private Map idCounts = new HashMap();
	private static final String __OBFID = "CL_00000604";

	public MapStorage(ISaveHandler par1ISaveHandler)
	{
		this.saveHandler = par1ISaveHandler;
		this.loadIdCounts();
	}

	public WorldSavedData loadData(Class par1Class, String par2Str)
	{
		WorldSavedData worldsaveddata = (WorldSavedData)this.loadedDataMap.get(par2Str);

		if (worldsaveddata != null)
		{
			return worldsaveddata;
		}
		else
		{
			if (this.saveHandler != null)
			{
				try
				{
					File file1 = this.saveHandler.getMapFileFromName(par2Str);

					if (file1 != null && file1.exists())
					{
						try
						{
							worldsaveddata = (WorldSavedData)par1Class.getConstructor(new Class[] {String.class}).newInstance(new Object[] {par2Str});
						}
						catch (Exception exception)
						{
							throw new RuntimeException("Failed to instantiate " + par1Class.toString(), exception);
						}

						FileInputStream fileinputstream = new FileInputStream(file1);
						NBTTagCompound nbttagcompound = CompressedStreamTools.readCompressed(fileinputstream);
						fileinputstream.close();
						worldsaveddata.readFromNBT(nbttagcompound.getCompoundTag("data"));
					}
				}
				catch (Exception exception1)
				{
					exception1.printStackTrace();
				}
			}

			if (worldsaveddata != null)
			{
				this.loadedDataMap.put(par2Str, worldsaveddata);
				this.loadedDataList.add(worldsaveddata);
			}

			return worldsaveddata;
		}
	}

	public void setData(String par1Str, WorldSavedData par2WorldSavedData)
	{
		if (par2WorldSavedData == null)
		{
			throw new RuntimeException("Can\'t set null data");
		}
		else
		{
			if (this.loadedDataMap.containsKey(par1Str))
			{
				this.loadedDataList.remove(this.loadedDataMap.remove(par1Str));
			}

			this.loadedDataMap.put(par1Str, par2WorldSavedData);
			this.loadedDataList.add(par2WorldSavedData);
		}
	}

	public void saveAllData()
	{
		for (int i = 0; i < this.loadedDataList.size(); ++i)
		{
			WorldSavedData worldsaveddata = (WorldSavedData)this.loadedDataList.get(i);

			if (worldsaveddata.isDirty())
			{
				this.saveData(worldsaveddata);
				worldsaveddata.setDirty(false);
			}
		}
	}

	private void saveData(WorldSavedData par1WorldSavedData)
	{
		if (this.saveHandler != null)
		{
			try
			{
				File file1 = this.saveHandler.getMapFileFromName(par1WorldSavedData.mapName);

				if (file1 != null)
				{
					NBTTagCompound nbttagcompound = new NBTTagCompound();
					par1WorldSavedData.writeToNBT(nbttagcompound);
					NBTTagCompound nbttagcompound1 = new NBTTagCompound();
					nbttagcompound1.setTag("data", nbttagcompound);
					FileOutputStream fileoutputstream = new FileOutputStream(file1);
					CompressedStreamTools.writeCompressed(nbttagcompound1, fileoutputstream);
					fileoutputstream.close();
				}
			}
			catch (Exception exception)
			{
				exception.printStackTrace();
			}
		}
	}

	private void loadIdCounts()
	{
		try
		{
			this.idCounts.clear();

			if (this.saveHandler == null)
			{
				return;
			}

			File file1 = this.saveHandler.getMapFileFromName("idcounts");

			if (file1 != null && file1.exists())
			{
				DataInputStream datainputstream = new DataInputStream(new FileInputStream(file1));
				NBTTagCompound nbttagcompound = CompressedStreamTools.read(datainputstream);
				datainputstream.close();
				Iterator iterator = nbttagcompound.func_150296_c().iterator();

				while (iterator.hasNext())
				{
					String s = (String)iterator.next();
					NBTBase nbtbase = nbttagcompound.getTag(s);

					if (nbtbase instanceof NBTTagShort)
					{
						NBTTagShort nbttagshort = (NBTTagShort)nbtbase;
						short short1 = nbttagshort.func_150289_e();
						this.idCounts.put(s, Short.valueOf(short1));
					}
				}
			}
		}
		catch (Exception exception)
		{
			exception.printStackTrace();
		}
	}

	public int getUniqueDataId(String par1Str)
	{
		Short oshort = (Short)this.idCounts.get(par1Str);

		if (oshort == null)
		{
			oshort = Short.valueOf((short)0);
		}
		else
		{
			oshort = Short.valueOf((short)(oshort.shortValue() + 1));
		}

		this.idCounts.put(par1Str, oshort);

		if (this.saveHandler == null)
		{
			return oshort.shortValue();
		}
		else
		{
			try
			{
				File file1 = this.saveHandler.getMapFileFromName("idcounts");

				if (file1 != null)
				{
					NBTTagCompound nbttagcompound = new NBTTagCompound();
					Iterator iterator = this.idCounts.keySet().iterator();

					while (iterator.hasNext())
					{
						String s1 = (String)iterator.next();
						short short1 = ((Short)this.idCounts.get(s1)).shortValue();
						nbttagcompound.setShort(s1, short1);
					}

					DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(file1));
					CompressedStreamTools.write(nbttagcompound, dataoutputstream);
					dataoutputstream.close();
				}
			}
			catch (Exception exception)
			{
				exception.printStackTrace();
			}

			return oshort.shortValue();
		}
	}
}