package com.skcraft.plume.module;

import com.google.inject.Inject;
import com.sk89q.intake.Command;
import com.sk89q.intake.Require;
import com.skcraft.plume.command.Sender;
import com.skcraft.plume.common.util.config.Config;
import com.skcraft.plume.common.util.config.InjectConfig;
import com.skcraft.plume.common.util.module.Module;
import com.skcraft.plume.util.Messages;
import com.skcraft.plume.util.VariableMessageBuilder;
import com.skcraft.plume.util.VariableMessageBuilder.MessageContext;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import ninja.leaping.configurate.objectmapping.Setting;

import static com.skcraft.plume.common.util.SharedLocale.tr;

@Module(name = "motd")
public class WelcomeMessage {

    @InjectConfig("motd") private Config<MessageConfig> config;
    @Inject private VariableMessageBuilder messageBuilder;

    @Command(aliases = "motd", desc = "Show the configured message of the day")
    @Require("plume.motd.motd")
    public void motd(@Sender ICommandSender sender) {
        String message = config.get().message;
        if (message != null && !message.isEmpty()) {
            MessageContext context = new MessageContext();
            if (sender instanceof EntityPlayer) {
                context.setPlayer((EntityPlayer) sender);
            }
            Messages.sendMessage(sender, messageBuilder.interpolate(message, context));
        } else {
            sender.addChatMessage(Messages.error(tr("motd.notConfigured")));
        }
    }

    @SubscribeEvent
    public void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        String message = config.get().message;
        if (message != null && !message.isEmpty()) {
            MessageContext context = new MessageContext().setPlayer(event.player);
            Messages.sendMessage(event.player, messageBuilder.interpolate(message, context));
        }
    }

    private static class MessageConfig {
        @Setting(comment = "The message to show in chat on join to players")
        public String message = "\u00A76Online: \u00A77({online.count})\u00A7e {online.names}";
    }

}

@@ -493,16 +493,78 @@
 ontext;%0A
+import com.skcraft.plume.util.concurrent.TickExecutorService;%0A
 import c
@@ -812,16 +812,148 @@
 tting;%0A%0A
+import java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 import s
@@ -1060,16 +1060,109 @@
 sage %7B%0A%0A
+    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(0);%0A%0A
     @Inj
@@ -1275,16 +1275,77 @@
 Builder;
+%0A    @Inject private TickExecutorService tickExecutorService;
 %0A%0A    @C
@@ -1620,262 +1620,23 @@
     
-MessageContext context = new MessageContext();%0A            if (sender instanceof EntityPlayer) %7B%0A                context.setPlayer((EntityPlayer) sender);%0A            %7D%0A            Messages.sendMessage(sender, messageBuilder.interpolate(message, context)
+sendMotd(sender
 );%0A 
@@ -1955,78 +1955,670 @@
     
-MessageContext context = new MessageContext().setPlayer(event.player);
+int delay = config.get().delay;%0A            if (delay %3C= 0) %7B%0A                sendMotd(event.player);%0A            %7D else %7B%0A                EntityPlayer player = event.player;%0A                scheduler.schedule(() -%3E tickExecutorService.execute(() -%3E sendMotd(player)), delay, TimeUnit.MILLISECONDS);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void sendMotd(ICommandSender sender) %7B%0A        String message = config.get().message;%0A        if (message != null && !message.isEmpty()) %7B%0A            MessageContext context = new MessageContext();%0A            if (sender instanceof EntityPlayer) %7B%0A                context.setPlayer((EntityPlayer) sender);%0A            %7D
 %0A   
@@ -2647,26 +2647,20 @@
 Message(
-event.play
+send
 er, mess
@@ -2932,16 +2932,133 @@
 names%7D%22;
+%0A%0A        @Setting(comment = %22The number of milliseconds to delay send of the MOTD%22)%0A        public int delay = 2000;
 %0A    %7D%0A%0A

Add an optional delay for sending the MOTD.