package top.quantic.sentry.discord;

import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sx.blah.discord.handle.obj.IMessage;
import top.quantic.sentry.discord.core.Command;
import top.quantic.sentry.discord.core.CommandBuilder;
import top.quantic.sentry.discord.module.CommandSupplier;
import top.quantic.sentry.domain.Setting;
import top.quantic.sentry.service.SettingService;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static top.quantic.sentry.discord.util.DiscordUtil.answer;

@Component
public class Twitch implements CommandSupplier {

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

    private final SettingService settingService;

    @Autowired
    public Twitch(SettingService settingService) {
        this.settingService = settingService;
    }

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

    private Command twitch() {
        OptionParser parser = new OptionParser();
        OptionSpec<String> nonOptSpec = parser.nonOptions("Twitch IDs separated by space").ofType(String.class);
        OptionSpec<Void> addSpec = parser.accepts("add", "Add streamers");
        OptionSpec<Void> removeSpec = parser.accepts("remove", "Remove streamers");
        OptionSpec<Void> listSpec = parser.accepts("list", "List streamers");
        OptionSpec<String> leagueSpec = parser.accepts("league", "Set the league data")
            .withRequiredArg().defaultsTo("all");
        OptionSpec<String> divSpec = parser.accepts("div", "Set the division data")
            .withRequiredArg().defaultsTo("none");
        return CommandBuilder.of("twitch")
            .describedAs("Manage twitch tracked streamers")
            .in("Integrations")
            .parsedBy(parser)
            .secured()
            .onExecute(context -> {
                IMessage message = context.getMessage();
                OptionSet o = context.getOptionSet();
                List<String> ids = o.valuesOf(nonOptSpec);
                String league = o.valueOf(leagueSpec);
                String div = o.valueOf(divSpec);
                String key = "twitch." + league.replace(".", "_") + "." + div.replace(".", "_");
                if (o.has(addSpec)) {
                    ids.forEach(id -> settingService.createSetting(guild(message), key, id));
                    answer(message, "Added: " + ids.stream().collect(Collectors.joining(", ")));
                } else if (o.has(removeSpec)) {
                    settingService.findByGuildAndKey(guild(message), key)
                        .forEach(setting -> settingService.delete(setting.getId()));
                    answer(message, "Removed: " + ids.stream().collect(Collectors.joining(", ")));
                } else if (o.has(listSpec)) {
                    answer(message, "Streamers: " + settingService.findByGuildAndKey(guild(message), key).stream()
                        .map(this::streamerInfo)
                        .collect(Collectors.joining(", ")));
                }
            }).build();
    }

    private String streamerInfo(Setting setting) {
         String[] parts = setting.getKey().split("\\.");
         String league = null;
         String div = null;
         if (parts.length == 3) {
             league = parts[1];
             div = parts[2];
         } else if (parts.length == 2) {
             league = parts[1];
         }
         String result = "**" + setting.getValue() + "**";
         if (league != null) {
             result += " (" + league;
             if (div != null) {
                 result += " " + div;
             }
             result += ")";
         }
         return result;
    }

    private String guild(IMessage message) {
        if (message.getChannel().isPrivate()) {
            return "*";
        } else {
            return message.getGuild().getID();
        }
    }
}

@@ -2640,19 +2640,204 @@
 , %22Added
-: %22
+ to %22 + (league.equals(%22all%22) ? %22%22 : %22**%22 + league + %22** league %22)%0A                        + %22and %22 + (div.equals(%22none%22) ? %22%22 : %22**%22 + div + %22** division %22) + %22: %22%0A                       
  + ids.s
@@ -3131,18 +3131,18 @@
 %22Removed
-:
  
+*
 %22 + ids.
@@ -3175,32 +3175,38 @@
 s.joining(%22, %22))
+ + %22*%22
 );%0A             
@@ -3567,25 +3567,24 @@
 ) %7B%0A        
- 
 String%5B%5D par
@@ -3619,33 +3619,32 @@
 %22%5C%5C.%22);%0A        
- 
 String league = 
@@ -3641,33 +3641,32 @@
  league = null;%0A
- 
         String d
@@ -3680,25 +3680,24 @@
 ll;%0A        
- 
 if (parts.le
@@ -3705,25 +3705,24 @@
 gth == 3) %7B%0A
- 
             
@@ -3752,17 +3752,16 @@
         
- 
 div = pa
@@ -3768,17 +3768,16 @@
 rts%5B2%5D;%0A
- 
         
@@ -3812,33 +3812,32 @@
 ) %7B%0A            
- 
 league = parts%5B1
@@ -3831,33 +3831,32 @@
 gue = parts%5B1%5D;%0A
- 
         %7D%0A      
@@ -3853,25 +3853,24 @@
   %7D%0A        
- 
 String resul
@@ -3911,25 +3911,24 @@
 *%22;%0A        
- 
 if (league !
@@ -3929,33 +3929,32 @@
 ague != null) %7B%0A
- 
             resu
@@ -3982,25 +3982,24 @@
             
- 
 if (div != n
@@ -3997,33 +3997,32 @@
 (div != null) %7B%0A
- 
                 
@@ -4046,36 +4046,34 @@
 iv;%0A            
- 
 %7D%0A
- 
             resu
@@ -4083,36 +4083,34 @@
 += %22)%22;%0A        
- 
 %7D%0A
- 
         return r

Improve feedback on add/list operations