package net.minecraft.src;

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

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.zip.GZIPInputStream;

public class SaveConverterMcRegion extends SaveFormatOld {
	public SaveConverterMcRegion(VFile2 var1) {
		super(var1);
	}

	public String formatName() {
		return "Scaevolus' McRegion";
	}

	public void setFilename(String name){
		super.setFilename(name);
	}

	public List getWorldList() {
		ArrayList arraylist = new ArrayList();
		List<VFile2> files = worldFile.listFiles(true);
		VFile2 afile[] = files.toArray(new VFile2[0]);
		int i = afile.length;
		for (int j = 0; j < i; j++) {
			VFile2 file = afile[j];
			if (!file.dirExists()) {
				continue;
			}
			String s = file.getName();
			WorldInfo worldinfo = getWorldInfoForWorld(s);
			if (worldinfo == null) {
				continue;
			}
			boolean flag = worldinfo.getSaveVersion() != 19132;
			String s1 = worldinfo.getWorldName();
			if (s1 == null || MathHelper.stringNullOrLengthZero(s1)) {
				s1 = s;
			}
			arraylist.add(new SaveFormatComparator(s, s1, worldinfo.getLastTimePlayed(), worldinfo.getSizeOnDisk(), flag));
		}

		return arraylist;
	}

	public void flushCache() {
		RegionFileCache.func_22192_a();
	}

	public ISaveHandler getSaveLoader(String var1, boolean var2) {
		return new SaveOldDir(this.worldFile, var1, var2);
	}

	public boolean isOldMapFormat(String var1) {
		WorldInfo var2 = this.getWorldInfoForWorld(var1);
		return var2 != null && var2.getSaveVersion() == 0;
	}

	public boolean convertMapFormat(String var1, IProgressUpdate var2) {
		var2.setLoadingProgress(0);
		ArrayList var3 = new ArrayList();
		ArrayList var4 = new ArrayList();
		ArrayList var5 = new ArrayList();
		ArrayList var6 = new ArrayList();
		VFile2 var7 = new VFile2(this.worldFile, var1);
		VFile2 var8 = new VFile2(var7, "DIM-1");
		System.out.println("Scanning folders...");
		this.func_22183_a(var7, var3, var4);
		if(var8.exists()) {
			this.func_22183_a(var8, var5, var6);
		}

		int var9 = var3.size() + var5.size() + var4.size() + var6.size();
		System.out.println("Total conversion count is " + var9);
		this.func_22181_a(var7, var3, 0, var9, var2);
		this.func_22181_a(var8, var5, var3.size(), var9, var2);
		WorldInfo var10 = this.getWorldInfoForWorld(var1);
		var10.setSaveVersion(19132);
		ISaveHandler var11 = this.getSaveLoader(var1, false);
		var11.saveWorldInfo(var10);
		this.func_22182_a(var4, var3.size() + var5.size(), var9, var2);
		if(var8.exists()) {
			this.func_22182_a(var6, var3.size() + var5.size() + var4.size(), var9, var2);
		}

		return true;
	}

	private void func_22183_a(VFile2 var1, ArrayList var2, ArrayList var3) {
		ChunkFolderPattern var4 = new ChunkFolderPattern(null);
		ChunkFilePattern var5 = new ChunkFilePattern(null);
		VFile2[] var6 = (VFile2[]) var1.listFiles(true).toArray();
		VFile2[] var7 = var6;
		int var8 = var6.length;

		for(int var9 = 0; var9 < var8; ++var9) {
			VFile2 var10 = var7[var9];
			var3.add(var10);
			VFile2[] var11 = (VFile2[]) var10.listFiles(true).toArray();
			VFile2[] var12 = var11;
			int var13 = var11.length;

			for(int var14 = 0; var14 < var13; ++var14) {
				VFile2 var15 = var12[var14];
				VFile2[] var16 = (VFile2[]) var15.listFiles(true).toArray();
				VFile2[] var17 = var16;
				int var18 = var16.length;

				for(int var19 = 0; var19 < var18; ++var19) {
					VFile2 var20 = var17[var19];
					var2.add(new FileMatcher(var20));
				}
			}
		}

	}

	private void func_22181_a(VFile2 var1, ArrayList var2, int var3, int var4, IProgressUpdate var5) {
		Collections.sort(var2);
		byte[] var6 = new byte[4096];
		Iterator var7 = var2.iterator();

		while(var7.hasNext()) {
			FileMatcher var8 = (FileMatcher)var7.next();
			int var9 = var8.func_22323_b();
			int var10 = var8.func_22321_c();
			RegionFile var11 = RegionFileCache.func_22193_a(var1, var9, var10);
			if(!var11.func_22202_c(var9 & 31, var10 & 31)) {
				try {
					DataInputStream var12 = new DataInputStream(new GZIPInputStream(var8.func_22324_a().getInputStream()));
					DataOutputStream var13 = var11.func_22205_b(var9 & 31, var10 & 31);
					boolean var14 = false;

					while(true) {
						int var17 = var12.read(var6);
						if(var17 == -1) {
							var13.close();
							var12.close();
							break;
						}

						var13.write(var6, 0, var17);
					}
				} catch (IOException var15) {
					var15.printStackTrace();
				}
			}

			++var3;
			int var16 = (int)Math.round(100.0D * (double)var3 / (double)var4);
			var5.setLoadingProgress(var16);
		}

		RegionFileCache.func_22192_a();
	}

	private void func_22182_a(ArrayList var1, int var2, int var3, IProgressUpdate var4) {
		Iterator var5 = var1.iterator();

		while(var5.hasNext()) {
			VFile2 var6 = (VFile2)var5.next();
			VFile2[] var7 = (VFile2[]) var6.listFiles(true).toArray();
			func_22179_a(var7);
			var6.delete();
			++var2;
			int var8 = (int)Math.round(100.0D * (double)var2 / (double)var3);
			var4.setLoadingProgress(var8);
		}

	}
}
