package me.footlights.core;

import java.io.*;
import java.net.URI;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Logger;

import com.google.common.collect.Maps;

import me.footlights.core.crypto.Keychain;
import me.footlights.core.plugin.PluginLoadException;
import me.footlights.core.plugin.PluginLoader;
import me.footlights.core.plugin.PluginWrapper;


public class Core implements Footlights
{
	public Core()
	{
		String configDirName = "~/.footlights/";

		File configDir = new File(configDirName);
		configDir.mkdir();

		keychain = new Keychain();
		final String keychainFileName = configDirName + "/keychain";

		try { keychain.importKeystoreFile(new FileInputStream(new File(keychainFileName))); }
		catch (Exception e)
		{
			Logger.getLogger(Core.class.getName())
				.warning("Unable to open keychain: " + e.getLocalizedMessage());
		}

		plugins          = Maps.newHashMap();
		uis              = new LinkedList<UI>();
	}


	public void registerUI(UI ui) { uis.add(ui); }
	public void deregisterUI(UI ui) { uis.remove(ui); }

	public Collection<PluginWrapper> plugins() { return plugins.values(); }


	/** Load a plugin and wrap it up in a convenient wrapper */
	public PluginWrapper loadPlugin(String name, URI uri) throws PluginLoadException
	{
		if(plugins.containsKey(uri)) return plugins.get(uri);

		Logger log = Logger.getLogger(uri.toString());
		PluginWrapper plugin = new PluginLoader().loadPlugin(name, uri, log);

		plugins.put(uri, plugin);
		for(UI ui : uis) ui.pluginLoaded(plugin);

		return plugin;
	}


	/** Unload a plugin; after calling this, set ALL references to null */
	public void unloadPlugin(PluginWrapper plugin)
	{
		URI key = null;
		for(Entry<URI,PluginWrapper> e : plugins.entrySet())
			if(e.getValue().equals(plugin))
			{
				key = e.getKey();
				break;
			}

		if(key == null) return;

		for(UI ui : uis) ui.pluginUnloading(plugins.get(key));
		plugins.remove(key);
	}


	// KernelInterface implementation
	public java.util.UUID generateUUID() { return java.util.UUID.randomUUID(); }


	/** Name of the Core {@link Logger}. */
	public static final String CORE_LOG_NAME = "me.footlights.core";

	/** Our keychain */
	private Keychain keychain;

	/** Loaded plugins */
	private Map<URI,PluginWrapper> plugins;

	/** UIs which might like to be informed of events */
	private List<UI> uis;
}

@@ -37,17 +37,31 @@
 java.io.
-*
+FileInputStream
 ;%0Aimport
@@ -92,17 +92,71 @@
 va.util.
-*
+Collection;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0Aimport
@@ -211,16 +211,56 @@
 ogger;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -591,16 +591,24 @@
 s/%22;%0A%0A%09%09
+java.io.
 File con
@@ -616,24 +616,32 @@
 igDir = new 
+java.io.
 File(configD
@@ -769,19 +769,24 @@
 ;%0A%0A%09%09try
- %7B 
+%0A%09%09%7B%0A%09%09%09
 keychain
@@ -805,16 +805,21 @@
 oreFile(
+%0A%09%09%09%09
 new File
@@ -830,20 +830,34 @@
 tStream(
-new 
+%0A%09%09%09%09%09new java.io.
 File(key
@@ -873,17 +873,19 @@
 Name)));
- 
+%0A%09%09
 %7D%0A%09%09catc
@@ -1085,26 +1085,26 @@
   = 
-new LinkedList%3CUI%3E
+Lists.newArrayList
 ();%0A

Clean up imports, etc., prepare to use File.