package com.github.boformer.proto;

import org.slf4j.Logger;
import org.spongepowered.api.Game;
import org.spongepowered.api.util.event.Subscribe;
import org.spongepowered.api.event.state.PreInitializationEvent;
import org.spongepowered.api.event.state.ServerStoppingEvent;
import org.spongepowered.api.plugin.Plugin;

import com.github.boformer.proto.config.ConfigManager;
import com.github.boformer.proto.data.DataManager;
import com.github.boformer.proto.event.PlayerEventHandler;
import com.github.boformer.proto.worldedit.WorldEditConnector;

/**
 * The Plugin for Sponge. 
 * 
 * <p>This class listens for game state changes and provides access to data and config managers.</p>
 */
@Plugin(id = "Proto", name = "Proto", version = "1.0.0")
public class ProtoPlugin
{
	//TODO change some constructors to protected when possible, or use interfaces?
	//TODO call events
	//TODO create data for loaded worlds in config on startup
	//TODO worldgen plot regeneration
	
	private Game game;
	private Logger logger;

	private ConfigManager configManager;
	private DataManager dataManager;
	
	private WorldEditConnector worldEditConnector;
	
	/**
	 * Called when server starts up.
	 * 
	 * @param event The server initialization event
	 */
	@Subscribe
	public void onInit(PreInitializationEvent event)
	{
		game = event.getGame();
		logger = event.getPluginLog();

		// init ConfigManager
		configManager = new ConfigManager(this, event.getRecommendedConfigurationFile());
		configManager.initialize();

		// init DataManager
		dataManager = new DataManager(this);
		
		try
		{
			dataManager.initialize();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return;
		}

		// register events
		game.getEventManager().register(this, new PlayerEventHandler(this));
		
		if(true)//TODO worldedit installed?
		{
			worldEditConnector = new WorldEditConnector(this, game, null/*TODO*/);
			worldEditConnector.initialize();
		}
	}

	/**
	 * Called when server shuts down.
	 * 
	 * @param event The server stop event
	 */
	@Subscribe
	public void onStop(ServerStoppingEvent event)
	{
		try
		{
			dataManager.stop();
		}
		catch (Exception e)
		{
			logger.error("Error while shutting down data services. Some values may not have been saved. Details: " + e.getMessage());
			e.printStackTrace();
		}

		// TODO server stop
	}

	/**
	 * Gets the {@link ConfigManager}.
	 * 
	 * @return The config manager
	 */
	public ConfigManager getConfigManager()
	{
		return configManager;
	}
	
	/**
	 * Gets the {@link DataManager}.
	 * 
	 * @return The data manager
	 */
	public DataManager getDataManager()
	{
		return dataManager;
	}
	
	
	public WorldEditConnector getWorldEditConnector()
	{
		return worldEditConnector;
	}

	/**
	 * Gets the Logger for this plugin.
	 * 
	 * @return The plugin logger
	 */
	public Logger getLogger()
	{
		return logger;
	}

}

@@ -310,16 +310,69 @@
 .Plugin;
+%0Aimport org.spongepowered.api.plugin.PluginContainer;
 %0A%0Aimport
@@ -592,16 +592,105 @@
 nnector;
+%0Aimport com.sk89q.worldedit.WorldEdit;%0Aimport com.sk89q.worldedit.bukkit.WorldEditPlugin;
 %0A%0A/**%0A *
@@ -1908,47 +1908,380 @@
 %09%0A%09%09
-if(true)//TODO worldedit installed?%0A%09%09%7B
+PluginContainer worldEditPluginContainer = game.getPluginManager().getPlugin(%22WorldEdit%22).orNull();%0A%09%09%0A%09%09// is WorldEdit installed?%0A%09%09if(worldEditPluginContainer != null)%0A%09%09%7B%0A%09%09%09//TODO Cast to sponge WorldEditPlugin, not the Bukkit version%0A%09%09%09WorldEdit worldEdit = ((/*Placeholder from Bukkit!*/ WorldEditPlugin) worldEditPluginContainer.getInstance()).getWorldEdit();%0A%09%09%09
 %0A%09%09%09
@@ -2340,20 +2340,17 @@
 me, 
-null/*TODO*/
+worldEdit
 );%0A%09

check if worldEdit is installed