package j3deditor.bin.util;

import j3deditor.bin.hierarchy.HierarchyManager;

import j3deditor.bin.util.startup.StartUp;

import java.io.InputStream;


/**
 * Central module that initializes
 * and manages tools needed by various
 * classes.
 *
 * @author Risto Seene
 */
public class Utilities{

	/** path of the main data directory inside the applications JAR file. */
	public static final String mainData = "/j3deditor/data/";
	
	/** path of the default data directory inside the applications JAR file. */
	public static final String defaultData = "/j3deditor/data/default/";
	
	/** path of the compressed data directory inside the applications JAR file. */
	public static final String compressedData = "/j3deditor/data/compressed/";
	
	/** path of the 3d data directory inside the applications JAR file. */
	public static final String threeDData = "/j3deditor/data/3d/";

	/** an instance of <code>Utilities</code>. */
	public static Utilities util;
	
	/** an instance of <code>Messaging</code>. */
	public static Messaging out = new Messaging();
	
	/** an instance of <code>ImageLoader</code>. */
	public static ImageLoader image = new ImageLoader();

	/** an instance of <code>UserData</code>. */
	public static UserData userdata;

	/** an instance of <code>Translator</code>. */
	public static Translator translator;

	/** Object that manages hierarchy. */
	public static HierarchyManager hierarchy;


	/**
	 * Creates an instance of <code>Utilities</code>
	 * and initializes all tools.
	 *
	 * @param start
	 *	reference to the splash-screen component created by main entry point
	 */
	public Utilities(StartUp start){
		util = this;
		
		try{
			start.setInfo("Utilities (loading user settings)");
		} catch(Throwable t1){
		}
		userdata = new UserData();

		try{
			start.setInfo("Utilities (loading translation files)");
		} catch(Throwable t2){
		}
		translator = new Translator(getLanguage());

		try{
			start.setInfo("Initializing utilities...");
		} catch(Throwable t3){
		}
		hierarchy = new HierarchyManager();
	}
	
	
	/**
	 * Returns the stream of the given resource.
	 * 
	 * @param path
	 * 	path of the resource
	 * @return Returns the stream of the given resource
	 */
	public InputStream getResource(String path){
		return this.getClass().getResourceAsStream(path);
	}


	/**
	 * Returns part of the file name of the translation file
	 * that represents the selected language.
	 *
	 * @return Returns part of the file name of the translation file
	 */
	private String getLanguage(){
		String language = null;
		try{
			language = (String)(userdata.getData("language"));
		} catch(Throwable t){
			language = "default";
		}
		return language;
	}
	
	
	/**
	 * Extracts zipped resources from the applications JAR file
	 * to the given directory.
	 * 
	 * @param filePath
	 * 	ZIP file inside the applications JAR file
	 * @param destPath
	 * 	destination directory
	 */
	public void extractZipResources(String filePath, String destPath){
		try{
			Deployer.extractZip(this.getResource(filePath), UserData.getFile(destPath));
		} catch(Throwable t){
			//TODO
			Utilities.out.throwError(t);
		}
	}

}
