/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.modules.staffchat.commands;

import io.github.nucleuspowered.nucleus.Util;
import io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument;
import io.github.nucleuspowered.nucleus.internal.annotations.NoCooldown;
import io.github.nucleuspowered.nucleus.internal.annotations.NoCost;
import io.github.nucleuspowered.nucleus.internal.annotations.NoWarmup;
import io.github.nucleuspowered.nucleus.internal.annotations.Permissions;
import io.github.nucleuspowered.nucleus.internal.annotations.RegisterCommand;
import io.github.nucleuspowered.nucleus.internal.annotations.RunAsync;
import io.github.nucleuspowered.nucleus.internal.command.AbstractCommand;
import io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException;
import io.github.nucleuspowered.nucleus.internal.event.NucleusMessageChannelEvent;
import io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel;
import io.github.nucleuspowered.nucleus.modules.staffchat.StaffChatMessageChannel;
import io.github.nucleuspowered.nucleus.modules.staffchat.datamodules.StaffChatTransientModule;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.event.message.MessageEvent;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.action.TextActions;
import org.spongepowered.api.text.channel.MessageChannel;
import org.spongepowered.api.text.serializer.TextSerializers;

import java.util.Optional;

@Permissions(suggestedLevel = SuggestedLevel.MOD)
@RunAsync
@NoWarmup
@NoCooldown
@NoCost
@RegisterCommand({"staffchat", "sc", "a"})
public class StaffChatCommand extends AbstractCommand<CommandSource> {

    private final String message = "message";

    @Override
    public CommandElement[] getArguments() {
        return new CommandElement[] {
            GenericArguments.optional(new RemainingStringsArgument(Text.of(message)))
        };
    }

    @Override
    public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
        Optional<String> toSend = args.getOne(message);
        StaffChatMessageChannel scmc = StaffChatMessageChannel.getInstance();
        if (toSend.isPresent()) {
            Text rawMessage = TextSerializers.FORMATTING_CODE.deserialize(toSend.get());
            if (src instanceof Player) {
                Player pl = (Player)src;
                MessageChannelEvent.Chat event = new NucleusMessageChannelEvent(
                    Cause.source(pl).named(NamedCause.notifier(src)).build(),
                    scmc,
                    rawMessage,
                    new MessageEvent.MessageFormatter(Text.builder(pl.getName())
                        .onShiftClick(TextActions.insertText(pl.getName()))
                        .onClick(TextActions.suggestCommand("/msg " + pl.getName()))
                        .build(), rawMessage));

                if (!Sponge.getEventManager().post(event)) {
                    scmc.send(pl, Util.applyChatTemplate(event.getFormatter()));
                    return CommandResult.success();
                }

                throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.cancelled"));
            }

            scmc.send(src, TextSerializers.FORMATTING_CODE.deserialize(toSend.get()));
            return CommandResult.success();
        }

        if (!(src instanceof Player)) {
            throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat.consoletoggle"));
        }

        Player player = (Player)src;

        StaffChatTransientModule s = plugin.getUserDataManager().get(player).map(y -> y.getTransient(StaffChatTransientModule.class))
                .orElseGet(StaffChatTransientModule::new);

        boolean result = !(src.getMessageChannel() instanceof StaffChatMessageChannel);
        if (result) {
            s.setPreviousMessageChannel(player.getMessageChannel());
            src.setMessageChannel(StaffChatMessageChannel.getInstance());
        } else {
            src.setMessageChannel(s.getPreviousMessageChannel().orElse(MessageChannel.TO_ALL));
        }

        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.staffchat." + (result ? "on" : "off")));
        return CommandResult.success();
    }
}

@@ -219,54 +219,8 @@
 s;%0A%0A
-import io.github.nucleuspowered.nucleus.Util;%0A
 impo
@@ -666,79 +666,8 @@
 nd;%0A
-import io.github.nucleuspowered.nucleus.internal.annotations.RunAsync;%0A
 impo
@@ -821,91 +821,8 @@
 on;%0A
-import io.github.nucleuspowered.nucleus.internal.event.NucleusMessageChannelEvent;%0A
 impo
@@ -890,24 +890,96 @@
 estedLevel;%0A
+import io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils;%0A
 import io.gi
@@ -1149,45 +1149,8 @@
 le;%0A
-import org.spongepowered.api.Sponge;%0A
 impo
@@ -1516,25 +1516,17 @@
 api.
-event.cause.Cause
+text.Text
 ;%0Aim
@@ -1556,74 +1556,20 @@
 api.
-event.cause.NamedCause;%0Aimport org.spongepowered.api.event.message
+text.channel
 .Mes
@@ -1579,21 +1579,16 @@
 eChannel
-Event
 ;%0Aimport
@@ -1614,248 +1614,90 @@
 api.
-event.message.MessageEvent;%0Aimport org.spongepowered.api.text.Text;%0Aimport org.spongepowered.api.text.action.TextActions;%0Aimport org.spongepowered.api.text.channel.MessageChannel;%0Aimport org.spongepowered.api.text.serializer.TextSerializers
+text.chat.ChatTypes;%0Aimport org.spongepowered.api.util.annotation.NonnullByDefault
 ;%0A%0Ai
@@ -1777,18 +1777,8 @@
 OD)%0A
-@RunAsync%0A
 @NoW
@@ -1846,16 +1846,34 @@
 , %22a%22%7D)%0A
+@NonnullByDefault%0A
 public c
@@ -2365,1240 +2365,168 @@
     
-StaffChatMessageChannel scmc = StaffChatMessageChannel.getInstance();%0A        if (toSend.isPresent()) %7B%0A            Text rawMessage = TextSerializers.FORMATTING_CODE.deserialize(toSend.get());%0A            if (src instanceof Player) %7B%0A                Player pl = (Player)src;%0A                MessageChannelEvent.Chat event = new NucleusMessageChannelEvent(%0A                    Cause.source(pl).named(NamedCause.notifier(src)).build(),%0A                    scmc,%0A                    rawMessage,%0A                    new MessageEvent.MessageFormatter(Text.builder(pl.getName())%0A                        .onShiftClick(TextActions.insertText(pl.getName()))%0A                        .onClick(TextActions.suggestCommand(%22/msg %22 + pl.getName()))%0A                        .build(), rawMessage));%0A%0A                if (!Sponge.getEventManager().post(event)) %7B%0A                    scmc.send(pl, Util.applyChatTemplate(event.getFormatter()));%0A                    return CommandResult.success();%0A                %7D%0A%0A                throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat(%22command.staffchat.cancelled%22));%0A            %7D%0A%0A            scmc.send(src, TextSerializers.FORMATTING_CODE.deserialize(toSend.get()));
+if (toSend.isPresent()) %7B%0A            StaffChatMessageChannel.getInstance()%0A                .send(src, TextParsingUtils.addUrls(toSend.get()), ChatTypes.CHAT);%0A
 %0A   

Fix staff chat not working with URLs when using /a [message]