package net.mcft.copy.betterstorage.misc;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import cpw.mods.fml.common.registry.LanguageRegistry;

public class Localizations {
	
	private Localizations() {  }
	
	public static void load() {
		
		Pattern pattern = Pattern.compile(".*(assets/" + Constants.modId + "/lang/(.+)\\.(.+))");
		for (String file : getResources(pattern)) {
			Matcher matcher = pattern.matcher(file);
			matcher.find();
			String resource = "/" + matcher.group(1);
			String lang = matcher.group(2);
			boolean isXML = matcher.group(3).equalsIgnoreCase("xml");
			LanguageRegistry.instance().loadLocalization(resource, lang, isXML);
		}
		
	}
	
	private static Collection<String> getResources(Pattern pattern) {
		ArrayList<String> retval = new ArrayList<String>();
		String classPath = System.getProperty("java.class.path", ".");
		String[] classPathElements = classPath.split(File.pathSeparator);
		for (String element : classPathElements)
			retval.addAll(getResources(element, pattern));
		return retval;
	}
	
	private static Collection<String> getResources(String element, Pattern pattern) {
		ArrayList<String> retval = new ArrayList<String>();
		File file = new File(element);
		if (file.isDirectory())
			retval.addAll(getResourcesFromDirectory(file, pattern));
		else retval.addAll(getResourcesFromJarFile(file, pattern));
		return retval;
	}
	
	private static Collection<String> getResourcesFromJarFile(File file, Pattern pattern) {
		ArrayList<String> retval = new ArrayList<String>();
		ZipFile zf;
		try { zf = new ZipFile(file); }
		catch (ZipException e) { throw new Error(e); }
		catch (IOException e) { throw new Error(e); }
		Enumeration<? extends ZipEntry> entries = zf.entries();
		while (entries.hasMoreElements()) {
			ZipEntry ze = entries.nextElement();
			String fileName = ze.getName();
			boolean accept = pattern.matcher(fileName).matches();
			if (accept) retval.add(fileName);
		}
		try { zf.close(); }
		catch (IOException e) { throw new Error(e); }
		return retval;
	}
	
	private static Collection<String> getResourcesFromDirectory(File directory, Pattern pattern) {
		ArrayList<String> retval = new ArrayList<String>();
		File[] fileList = directory.listFiles();
		for (File file : fileList) {
			if (file.isDirectory()) {
				retval.addAll(getResourcesFromDirectory(file, pattern));
			} else try {
				String fileName = file.getCanonicalPath().replace('\\', '/');
				boolean accept = pattern.matcher(fileName).matches();
				if (accept) retval.add(fileName);
			} catch (final IOException e) { throw new Error(e); }
		}
		return retval;
	}
	
}

@@ -333,16 +333,101 @@
 pFile;%0A%0A
+import net.mcft.copy.betterstorage.BetterStorage;%0Aimport cpw.mods.fml.common.Loader;%0A
 import c
@@ -568,16 +568,118 @@
 () %7B%0A%09%09%0A
+%09%09File source = Loader.instance().getReversedModObjectList().get(BetterStorage.instance).getSource();%0A
 %09%09Patter
@@ -796,16 +796,24 @@
 sources(
+source, 
 pattern)
@@ -1098,381 +1098,87 @@
 ;%0A%09%09
-%7D%0A%09%09%0A%09%7D%0A%09%0A%09private static Collection%3CString%3E getResources(Pattern pattern) %7B%0A%09%09ArrayList%3CString%3E retval = new ArrayList%3CString%3E();%0A%09%09String classPath = System.getProperty(%22java.class.path%22, %22.%22);%0A%09%09String%5B%5D classPathElements = classPath.split(File.pathSeparator);%0A%09%09for (String element : classPathElements)%0A%09%09%09retval.addAll(getResources(element, pattern));%0A%09%09return retval;
+%09BetterStorage.log.info(%22Loaded localization file for '%22 + lang + %22'.%22);%0A%09%09%7D%0A%09%09
 %0A%09%7D%0A
@@ -1231,22 +1231,17 @@
 ces(
-String element
+File file
 , Pa
@@ -1315,41 +1315,8 @@
 ();%0A
-%09%09File file = new File(element);%0A
 %09%09if

Fix dynamic localization loading