package top.quantic.sentry.discord;

import org.ocpsoft.prettytime.shade.edu.emory.mathcs.backport.java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.obj.IMessage;
import sx.blah.discord.util.DiscordException;
import top.quantic.sentry.discord.command.Command;
import top.quantic.sentry.discord.command.CommandBuilder;
import top.quantic.sentry.discord.command.CommandContext;
import top.quantic.sentry.discord.module.CommandSupplier;

import java.util.List;

import static top.quantic.sentry.config.Constants.INSTANCE_KEY;
import static top.quantic.sentry.discord.util.DiscordUtil.*;

@Component
public class PowerSwitch implements CommandSupplier {

    private static final Logger log = LoggerFactory.getLogger(PowerSwitch.class);

    @Override
    public List<Command> getCommands() {
        return Collections.singletonList(logout());
    }

    private Command logout() {
        return CommandBuilder.of("logout")
            .describedAs("Disconnect from Discord")
            .in("Power")
            .nonParsed()
            .secured()
            .onExecute(context -> {
                IMessage message = context.getMessage();
                String[] args = context.getArgs();
                if (args != null && args.length >= 2) {
                    String id = ourBotId(message.getClient());
                    // shutdown if the args match this bot but not this instance
                    if (args[0].equals(id) && !args[1].equals(INSTANCE_KEY)) {
                        deleteMessage(message);
                        doLogout(context);
                    } else {
                        log.debug("Ignoring logout request - Expected args {} and anything but {}", id, INSTANCE_KEY);
                    }
                } else if (args == null || args.length == 0) {
                    deleteMessage(message);
                    answer(message, ":wave:");
                    doLogout(context);
                }
            }).build();
    }

    private void doLogout(CommandContext context) {
        IDiscordClient client = context.getMessage().getClient();
        log.info("[{}] Logging out via command", client.getOurUser().getName());
        if (client.isLoggedIn()) {
            try {
                client.logout();
            } catch (DiscordException e) {
                log.warn("Could not logout bot", e);
            }
        }
    }
}

@@ -1372,28 +1372,8 @@
 null
- && args.length %3E= 2
 ) %7B%0A
@@ -1431,24 +1431,81 @@
 tClient());%0A
+                    String%5B%5D array = args%5B0%5D.split(%22 %22);%0A
             
@@ -1599,18 +1599,40 @@
   if (ar
-gs
+ray.length %3E= 2 && array
 %5B0%5D.equa
@@ -1644,18 +1644,19 @@
 ) && !ar
-gs
+ray
 %5B1%5D.equa
@@ -1842,25 +1842,9 @@
 ring
- logout request -
+:
  Exp
@@ -1946,46 +1946,8 @@
 else
- if (args == null %7C%7C args.length == 0)
  %7B%0A 

Fix logout args parsing