package net.blay09.mods.eirairc.addon;

import cpw.mods.fml.common.Optional;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.blay09.mods.eirairc.api.event.IRCChannelChatEvent;
import net.blay09.mods.eirairc.api.event.IRCUserJoinEvent;
import net.blay09.mods.eirairc.api.event.IRCUserLeaveEvent;
import net.blay09.mods.eirairc.api.event.RelayChat;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import org.dynmap.DynmapCommonAPI;
import org.dynmap.DynmapCommonAPIListener;

/**
 * Created by Blay09 on 07.02.2015.
 */
@Optional.Interface(iface = "org.dynmap.DynmapCommonAPIListener", modid = "Dynmap")
public class DynmapWebChatAddon extends DynmapCommonAPIListener {

	public static class WebChatSender implements ICommandSender {
		public final String source;
		public final String name;

		public WebChatSender(String source, String name) {
			this.source = source;
			this.name = name;
		}

		@Override
		public String getCommandSenderName() {
			return "[Dynmap] " + name;
		}

		@Override
		public IChatComponent func_145748_c_() {
			return new ChatComponentText(this.getCommandSenderName());
		}

		@Override
		public void addChatMessage(IChatComponent p_145747_1_) {}

		@Override
		public boolean canCommandSenderUseCommand(int p_70003_1_, String p_70003_2_) {
			return false;
		}

		@Override
		public ChunkCoordinates getPlayerCoordinates() {
			return new ChunkCoordinates(0, 0, 0);
		}

		@Override
		public World getEntityWorld() {
			return MinecraftServer.getServer().getEntityWorld();
		}
	}

	private DynmapCommonAPI api;

	public DynmapWebChatAddon() {
		DynmapCommonAPIListener.register(this);
	}

	@Override
	@Optional.Method(modid = "Dynmap")
	public void apiEnabled(DynmapCommonAPI api) {
		this.api = api;
		MinecraftForge.EVENT_BUS.register(this);
	}

	@Override
	public void apiDisabled(DynmapCommonAPI api) {
		this.api = null;
		MinecraftForge.EVENT_BUS.unregister(this);
	}

	@Optional.Method(modid = "Dynmap")
	@SubscribeEvent
	public void onChannelChat(IRCChannelChatEvent event) {
		api.postPlayerMessageToWeb(event.sender.getName(), event.sender.getName(), event.message);
	}

	@Optional.Method(modid = "Dynmap")
	@SubscribeEvent
	public void onIRCUserJoin(IRCUserJoinEvent event) {
		api.postPlayerJoinQuitToWeb(event.user.getName(), event.user.getName(), true);
	}

	@Optional.Method(modid = "Dynmap")
	@SubscribeEvent
	public void onIRCUserLeave(IRCUserLeaveEvent event) {
		api.postPlayerJoinQuitToWeb(event.user.getName(), event.user.getName(), false);
	}

	@Optional.Method(modid = "Dynmap")
	@SubscribeEvent
	public void onIRCUserQuit(IRCUserLeaveEvent event) {
		api.postPlayerJoinQuitToWeb(event.user.getName(), event.user.getName(), false);
	}

	@Override
	@Optional.Method(modid = "Dynmap")
	public boolean webChatEvent(String source, String name, String message) {
		MinecraftForge.EVENT_BUS.post(new RelayChat(new WebChatSender(source, name), message));
		return true;
	}
}

@@ -1232,15 +1232,65 @@
 n %22%5B
-Dynmap%5D
+%22 + source + %22%5D%22 + ((name != null && !name.isEmpty()) ? %22
  %22 +
@@ -1290,24 +1290,30 @@
 ? %22 %22 + name
+ : %22%22)
 ;%0A%09%09%7D%0A%0A%09%09@Ov
@@ -2114,16 +2114,52 @@
 verride%0A
+%09@Optional.Method(modid = %22Dynmap%22)%0A
 %09public 
@@ -2262,24 +2262,41 @@
 (this);%0A%09%7D%0A%0A
+%09@SubscribeEvent%0A
 %09@Optional.M
@@ -2319,33 +2319,16 @@
 ynmap%22)%0A
-%09@SubscribeEvent%0A
 %09public 
@@ -2464,32 +2464,49 @@
 t.message);%0A%09%7D%0A%0A
+%09@SubscribeEvent%0A
 %09@Optional.Metho
@@ -2525,33 +2525,16 @@
 ynmap%22)%0A
-%09@SubscribeEvent%0A
 %09public 
@@ -2655,32 +2655,49 @@
 e(), true);%0A%09%7D%0A%0A
+%09@SubscribeEvent%0A
 %09@Optional.Metho
@@ -2716,33 +2716,16 @@
 ynmap%22)%0A
-%09@SubscribeEvent%0A
 %09public 

Fix dynmap name formatting.