package org.etsi.mts.tdl.ui.wizard;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.osgi.framework.Bundle;

public class TemplateHelper {
	enum Libraries {
		Standard, HTTP 
	}
	public static String getLibrary(String name) {
		String library = "";
		try {
			System.out.println("Get standard library...");
			String absolutePath = Path.of("templates/"+name+".tdltx").toFile().getAbsolutePath();
			System.out.println("  Path: " + absolutePath);
			String source = "templates/"+name+".tdltx";
			URI uri = new File(source).toURI();
			if (Platform.isRunning()) {
				System.out.println("Running as plugin...");
				Bundle bundle = Platform.getBundle("org.etsi.mts.tdl.tx.ui");
				URL url = bundle.getEntry(source);
				uri = FileLocator.toFileURL(url).toURI();
			} else {
				System.out.println("Get from class localtion...");
				String binPath = TemplateContent.class.getClass().getProtectionDomain().getCodeSource()
						.getLocation().getPath();
				String projectPath = new File(binPath).getParent();
				uri = new File(((projectPath + "/") + source)).toURI();
			}
			library = Files.readString(Path.of(uri));
		} catch (Throwable _e) {
			throw Exceptions.sneakyThrow(_e);
		}
		return library;
	}

}
