package it.mice.voila.wizard.plugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;

public abstract class WizardAcceleoGenerator extends AbstractAcceleoGenerator {

	//private String moduleName = null;
	
	public WizardAcceleoGenerator() {
		super();
	}
	
	public WizardAcceleoGenerator(File target, String props) throws IOException {
		super();
//		this.moduleName = moduleName;
		propertiesFiles.add(props);
		List<String> arguments = new ArrayList<String>();
//		URL url = getClass().getResource("dummy.uml");
//		URI uri = URI.createURI(url.toString()); 
		URI fileURI = URI.createPlatformPluginURI("/it.mice.voila.wizard.plugin/dummy.uml",false);
		//Object dummy = EcoreFactory.eINSTANCE.createEPackage();
        initialize(fileURI, target, arguments);
	}

	/**
	 * If the module(s) called by this launcher require properties files, return
	 * their qualified path from here.Take note that the first added properties
	 * files will take precedence over subsequent ones if they contain conflicting
	 * keys.
	 * 
	 * @return The list of properties file we need to add to the generation context.
	 * @see java.util.ResourceBundle#getBundle(String)
	 * @generated NOT
	 */
	@Override
	public List<String> getProperties() {
		/*
		 * If you want to change the content of this method, do NOT forget to change the
		 * "@generated" tag in the Javadoc of this method to "@generated NOT". Without
		 * this new tag, any compilation of the Acceleo module with the main template
		 * that has caused the creation of this class will revert your modifications.
		 */

		/*
		 * TODO if your generation module requires access to properties files, add their
		 * qualified path to the list here.
		 * 
		 * Properties files can be located in an Eclipse plug-in or in the file system
		 * (all Acceleo projects are Eclipse plug-in). In order to use properties files
		 * located in an Eclipse plugin, you need to add the path of the properties
		 * files to the "propertiesFiles" list:
		 * 
		 * final String prefix = "platform:/plugin/"; final String pluginName =
		 * "org.eclipse.acceleo.module.sample"; final String packagePath =
		 * "/org/eclipse/acceleo/module/sample/properties/"; final String fileName =
		 * "default.properties"; propertiesFiles.add(prefix + pluginName + packagePath +
		 * fileName);
		 * 
		 * With this mechanism, you can load properties files from your plugin or from
		 * another plugin.
		 * 
		 * You may want to load properties files from the file system, for that you need
		 * to add the absolute path of the file:
		 * 
		 * propertiesFiles.add("C:\Users\MyName\MyFile.properties");
		 * 
		 * If you want to let your users add properties files located in the same folder
		 * as the model:
		 *
		 * if (EMFPlugin.IS_ECLIPSE_RUNNING && model != null && model.eResource() !=
		 * null) {
		 * propertiesFiles.addAll(AcceleoEngineUtils.getPropertiesFilesNearModel(model.
		 * eResource())); }
		 * 
		 * To learn more about Properties Files, have a look at the Acceleo
		 * documentation (Help -> Help Contents).
		 */
		return propertiesFiles;
//		URL uri = Thread.currentThread().getContextClassLoader().getResource("properties/voila2-default.properties");
//		String resName = uri.toExternalForm().substring(6);
//		propertiesFiles.add(resName);
//		return propertiesFiles;
	}

	/**
     * This can be used to update the resource set's package registry with all needed EPackages.
     * 
     * @param resourceSet
     *            The resource set which registry has to be updated.
     * @generated
     */
    @Override
    public void registerPackages(ResourceSet resourceSet) {
        super.registerPackages(resourceSet);
        if (!isInWorkspace(org.eclipse.uml2.uml.UMLPackage.class)) {
            resourceSet.getPackageRegistry().put(org.eclipse.uml2.uml.UMLPackage.eINSTANCE.getNsURI(), org.eclipse.uml2.uml.UMLPackage.eINSTANCE);
        }
        
        /*
         * If you want to change the content of this method, do NOT forget to change the "@generated"
         * tag in the Javadoc of this method to "@generated NOT". Without this new tag, any compilation
         * of the Acceleo module with the main template that has caused the creation of this class will
         * revert your modifications.
         */
        
        /*
         * If you need additional package registrations, you can register them here. The following line
         * (in comment) is an example of the package registration for UML.
         * 
         * You can use the method  "isInWorkspace(Class c)" to check if the package that you are about to
         * register is in the workspace.
         * 
         * To register a package properly, please follow the following conventions:
         *
         * If the package is located in another plug-in, already installed in Eclipse. The following content should
         * have been generated at the beginning of this method. Do not register the package using this mechanism if
         * the metamodel is located in the workspace.
         *  
         * if (!isInWorkspace(UMLPackage.class)) {
         *     // The normal package registration if your metamodel is in a plugin.
         *     resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
         * }
         * 
         * If the package is located in another project in your workspace, the plugin containing the package has not
         * been register by EMF and Acceleo should register it automatically. If you want to use the generator in
         * stand alone, the regular registration (seen a couple lines before) is needed.
         * 
         * To learn more about Package Registration, have a look at the Acceleo documentation (Help -> Help Contents).
         */
    }

	@Override
	public String[] getTemplateNames() {
		return new String[] {"main"};
	}
}
