package com.gmail.trentech.wirelessred;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.GameReloadEvent;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;

import com.gmail.trentech.wirelessred.commands.CommandManager;
import com.gmail.trentech.wirelessred.data.receiver.ImmutableReceiverData;
import com.gmail.trentech.wirelessred.data.receiver.Receiver;
import com.gmail.trentech.wirelessred.data.receiver.ReceiverBuilder;
import com.gmail.trentech.wirelessred.data.receiver.ReceiverData;
import com.gmail.trentech.wirelessred.data.receiver.ReceiverDataManipulatorBuilder;
import com.gmail.trentech.wirelessred.data.transmitter.ImmutableTransmitterData;
import com.gmail.trentech.wirelessred.data.transmitter.Transmitter;
import com.gmail.trentech.wirelessred.data.transmitter.TransmitterBuilder;
import com.gmail.trentech.wirelessred.data.transmitter.TransmitterData;
import com.gmail.trentech.wirelessred.data.transmitter.TransmitterDataManipulatorBuilder;
import com.gmail.trentech.wirelessred.init.Common;
import com.gmail.trentech.wirelessred.listeners.ReceiverListener;
import com.gmail.trentech.wirelessred.listeners.TransmitterListener;
import com.gmail.trentech.wirelessred.utils.Resource;
import com.google.inject.Inject;

import me.flibio.updatifier.Updatifier;

@Updatifier(repoName = Resource.NAME, repoOwner = Resource.AUTHOR, version = Resource.VERSION)
@Plugin(id = Resource.ID, name = Resource.NAME, version = Resource.VERSION, description = Resource.DESCRIPTION, authors = Resource.AUTHOR, url = Resource.URL, dependencies = { @Dependency(id = "Updatifier", optional = true), @Dependency(id = "pjc", optional = false) })
public class Main {

	@Inject @ConfigDir(sharedRoot = false)
    private Path path;

	@Inject
	private Logger log;

	private static PluginContainer plugin;
	private static Main instance;
	
	@Listener
	public void onPreInitializationEvent(GamePreInitializationEvent event) {
		plugin = Sponge.getPluginManager().getPlugin(Resource.ID).get();
		instance = this;

		try {			
			Files.createDirectories(path);		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Listener
	public void onInitialization(GameInitializationEvent event) {
		Common.initConfig();

		Sponge.getEventManager().registerListeners(this, new TransmitterListener());
		Sponge.getEventManager().registerListeners(this, new ReceiverListener());

		Sponge.getCommandManager().register(this, new CommandManager().cmdWR, "wr");

		Sponge.getDataManager().register(TransmitterData.class, ImmutableTransmitterData.class, new TransmitterDataManipulatorBuilder());
		Sponge.getDataManager().registerBuilder(Transmitter.class, new TransmitterBuilder());
		Sponge.getDataManager().register(ReceiverData.class, ImmutableReceiverData.class, new ReceiverDataManipulatorBuilder());
		Sponge.getDataManager().registerBuilder(Receiver.class, new ReceiverBuilder());

		Common.initData();
	}
	
	@Listener
	public void onReloadEvent(GameReloadEvent event) {
		Sponge.getEventManager().unregisterPluginListeners(getPlugin());

		Common.initConfig();
		
		Sponge.getEventManager().registerListeners(this, new TransmitterListener());
		Sponge.getEventManager().registerListeners(this, new ReceiverListener());
		
		//Common.initRecipes();
	}
	
	public Logger getLog() {
		return log;
	}

	public Path getPath() {
		return path;
	}
	
	public static PluginContainer getPlugin() {
		return plugin;
	}
	
	public static Main instance() {
		return instance;
	}
}
@@ -232,24 +232,77 @@
 ConfigDir;%0D%0A
+import org.spongepowered.api.data.DataRegistration;%0D%0A
 import org.s
@@ -3035,40 +3035,44 @@
 %0D%0A%09%09
-Sponge.getDataManager().register
+DataRegistration.builder().dataClass
 (Tra
@@ -3081,34 +3081,49 @@
 mitterData.class
-, 
+).immutableClass(
 ImmutableTransmi
@@ -3132,26 +3132,39 @@
 erData.class
-, 
+)%0D%0A%09%09%09.builder(
 new Transmit
@@ -3183,35 +3183,139 @@
 ulatorBuilder())
+.dataName(%22Transmitter%22).manipulatorId(%22wirelessred_transmitter%22).buildAndRegister(Main.getPlugin())
 ;%0D%0A
+%09%09%0D%0A
 %09%09Sponge.getData
@@ -3391,42 +3391,48 @@
 );%0D%0A
-%09%09Sponge.getDataManager().register
+%0D%0A%09%09DataRegistration.builder().dataClass
 (Rec
@@ -3446,18 +3446,33 @@
 ta.class
-, 
+).immutableClass(
 Immutabl
@@ -3486,26 +3486,39 @@
 erData.class
-, 
+)%0D%0A%09%09%09.builder(
 new Receiver
@@ -3538,25 +3538,123 @@
 orBuilder())
-;
+.dataName(%22Receiver%22).manipulatorId(%22wirelessred_receiver%22).buildAndRegister(Main.getPlugin());%0D%0A%09%09
 %0D%0A%09%09Sponge.g

Update Sponge API changes