package net.minecraft.src;

import net.lax1dude.eaglercraft.internal.vfs2.VFile2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.logging.Logger;

public class SaveHandler implements ISaveHandler {
	private static final Logger logger = Logger.getLogger("Minecraft");
	private final VFile2 saveDirectory;
	private final VFile2 playersDirectory;
	private final long now = System.currentTimeMillis();

	public SaveHandler(VFile2 var1, String var2, boolean var3) {
		this.saveDirectory = new VFile2(var1, var2);
		//this.saveDirectory.mkdirs();
		this.playersDirectory = new VFile2(this.saveDirectory, "players");
		if(var3) {
			//this.playersDirectory.mkdirs();
		}

		this.checkSessionLock();
	}

	private void checkSessionLock() {
//		try {
//			VFile2 var1 = new VFile2(this.saveDirectory, "session.lock");
//			DataOutputStream var2 = new DataOutputStream(new FileOutputStream(var1));
//
//			try {
//				var2.writeLong(this.now);
//			} finally {
//				var2.close();
//			}
//
//		} catch (IOException var7) {
//			var7.printStackTrace();
//			throw new RuntimeException("Failed to check session lock, aborting");
//		}
	}

	protected VFile2 getSaveDirectory() {
		return this.saveDirectory;
	}

	public void func_22150_b() {
//		try {
//			VFile2 var1 = new VFile2(this.saveDirectory, "session.lock");
//			DataInputStream var2 = new DataInputStream(new FileInputStream(var1));
//
//			try {
//				if(var2.readLong() != this.now) {
//					throw new MinecraftException("The save is being accessed from another location, aborting");
//				}
//			} finally {
//				var2.close();
//			}
//
//		} catch (IOException var7) {
//			throw new MinecraftException("Failed to check session lock, aborting");
//		}
	}

	public IChunkLoader getChunkLoader(WorldProvider var1) {
		if(var1 instanceof WorldProviderHell) {
			VFile2 var2 = new VFile2(this.saveDirectory, "DIM-1");
//			var2.mkdirs();
			return new ChunkLoader(var2, true);
		} else {
			return new ChunkLoader(this.saveDirectory, true);
		}
	}

	public WorldInfo loadWorldInfo() {
		VFile2 var1 = new VFile2(this.saveDirectory, "level.dat");
		if(var1.exists()) {
			try {
				NBTTagCompound var2 = CompressedStreamTools.readCompressed(var1.getInputStream());
				NBTTagCompound var3 = var2.getCompoundTag("Data");
				return new WorldInfo(var3);
			} catch (Exception var4) {
				var4.printStackTrace();
			}
		}

		return null;
	}

	public void saveWorldInfoAndPlayer(WorldInfo var1, List var2) {
		NBTTagCompound var3 = var1.getNBTTagCompoundWithPlayer(var2);
		NBTTagCompound var4 = new NBTTagCompound();
		var4.setTag("Data", var3);

		try {
			VFile2 var5 = new VFile2(this.saveDirectory, "level.dat_new");
			VFile2 var6 = new VFile2(this.saveDirectory, "level.dat_old");
			VFile2 var7 = new VFile2(this.saveDirectory, "level.dat");
			CompressedStreamTools.writeGzippedCompoundToOutputStream(var4, var5.getOutputStream());
			if(var6.exists()) {
				var6.delete();
			}

			var7.renameTo(var6);
			if(var7.exists()) {
				var7.delete();
			}

			var5.renameTo(var7);
			if(var5.exists()) {
				var5.delete();
			}
		} catch (Exception var8) {
			var8.printStackTrace();
		}

	}

	public void saveWorldInfo(WorldInfo var1) {
		NBTTagCompound var2 = var1.getNBTTagCompound();
		NBTTagCompound var3 = new NBTTagCompound();
		var3.setTag("Data", var2);

		try {
			VFile2 var4 = new VFile2(this.saveDirectory, "level.dat_new");
			VFile2 var5 = new VFile2(this.saveDirectory, "level.dat_old");
			VFile2 var6 = new VFile2(this.saveDirectory, "level.dat");
			CompressedStreamTools.writeGzippedCompoundToOutputStream(var3, var4.getOutputStream());
			if(var5.exists()) {
				var5.delete();
			}

			var6.renameTo(var5);
			if(var6.exists()) {
				var6.delete();
			}

			var4.renameTo(var6);
			if(var4.exists()) {
				var4.delete();
			}
		} catch (Exception var7) {
			var7.printStackTrace();
		}

	}
}
