package j3deditor.bin.util;

import java.io.File;

import java.io.FileOutputStream;
import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


/**
 * Utilities for manipulating with files on disk.
 * 
 * @author Risto Seene
 */
public class Deployer{


	/**
	 * Creates the specified directory and all
	 * necessary parent directories if it does not exist yet.
	 * 
	 * @param dir
	 * 	directory that will be created
	 * @throws Exception
	 * 	if unable to create the specified directory
	 */
	public static void createDirectory(File dir) throws Exception{
		if(dir != null && !dir.exists()){
			dir.mkdirs();
		}
	}


	/**
	 * Creates the specified file and all necessary
	 * parent directories if it does not exist yet.
	 * 
	 * @param file
	 * 	file that will be created
	 * @throws Exception
	 * 	if unable to create the specified file
	 */
	public static void createFile(File file) throws Exception{
		if(file != null && !file.exists()){
			createDirectory(file.getParentFile());
			file.createNewFile();
		}
	}


	/**
	 * Returns the list of available language files from the given directory.
	 * Contains language names and two letter language codes.
	 * 
	 * @param dir
	 * 	directory where the files are located
	 * @return Returns the list of available language files
	 * @throws Exception
	 * 	if an exception occurs
	 */
	protected static String[][] updateLanguageList(File dir) throws Exception{
		ArrayList<String[]> languages = new ArrayList<String[]>();

		if(dir.exists() && dir.isDirectory()){
			File[] files = dir.listFiles();
			for(int i = 0; i < files.length; i++){
				String fileName = files[i].getName();
				if(files[i].isFile() && fileName.startsWith("lang_") && !fileName.contains(" ") && fileName.endsWith(".xml")){
					XMLParser p = new XMLParser(files[i]);
					String name = p.getRootAttribute("Language");
					if(name != null){
						String mark = files[i].getName().substring(files[i].getName().indexOf("_") + 1, files[i].getName().lastIndexOf(".xml"));
						languages.add(new String[]{ name, mark.toUpperCase() });
					}
				}
			}
		}

		return languages.toArray(new String[languages.size()][2]);
	}
	
	
	/**
	 * Extracts the contents of the specified ZIP file.
	 * Can be used to extract ZIP files from a JAR file.
	 * 
	 * @param in
	 * 	opened stream from a ZIP file
	 * @param dir
	 * 	directory where the contents will be extracted to
	 * @throws Exception
	 * 	if unable to extract the specified ZIP file
	 */
	public static void extractZip(InputStream in, File dir) throws Exception{
		ZipInputStream zin = new ZipInputStream(in);
	          ZipEntry entry;
		
		createDirectory(dir);
		
		while((entry = zin.getNextEntry()) != null){
			System.out.println("Extracting: " + entry);
		          String fileName = dir.getCanonicalPath() + File.separator + entry.getName();
		          File outFile = new File(fileName);
			
			if(entry.isDirectory()){
				createDirectory(outFile);
				System.out.println("Create Folder " + outFile);
			} else {
			          createDirectory(outFile.getParentFile());
				OutputStream out = new FileOutputStream(outFile);
			
				byte[] buffer = new byte[1024];
				int length;
				while((length=zin.read(buffer)) > 0){
					out.write(buffer, 0, length);
				}
				
				out.close();
			}
		}
		
	          zin.close();
	          in.close();
	}

}