package com.github.masahitojp.botan.listener.cron;

import com.github.masahitojp.botan.Robot;
import com.github.masahitojp.botan.listener.BotanMessageListenerRegister;
import com.github.masahitojp.botan.message.BotanMessageSimple;
import com.google.gson.Gson;
import it.sauronsoftware.cron4j.Scheduler;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@SuppressWarnings("unused")
public class CronListener implements BotanMessageListenerRegister {

    public static ConcurrentHashMap<String, CronJob> hashMap = new ConcurrentHashMap<>();
    public static String dump() {
        final Gson gson = new Gson();
        final List<CronJob> jobs = hashMap.values().stream().map(data -> data).collect(Collectors.toList());
        return gson.toJson(jobs);
    }

    @Override
    public void register(final Robot robot) {
        final String to = "general@conference.cappybara-xmpp.xmpp.slack.com";
        final String body = robot.getName() + " echo 週報 OR 勉強会　開始５分前です";

        final Scheduler scheduler = new Scheduler();
        // every minute.
        final String id = scheduler.schedule("* * * * *", () -> {
            robot.receive(new BotanMessageSimple(body, to, to, to, -1));
        });
        hashMap.put(id, new CronJob("* * * * *", to, body));

        robot.respond(
                "job\\s+add\\s+\"(?<schedule>.+)\"\\s+(?<message>.+)$",
                "",
                message -> {
                    final String jobId = scheduler.schedule(message.getMatcher().group("schedule"), () -> {
                        message.reply(message.getMatcher().group("message"));
                    });
                    hashMap.put(jobId, new CronJob(message.getMatcher().group("schedule"), message.getFrom(), message.getMatcher().group("message")));
                    message.reply(jobId);
                }
        );

        robot.respond(
                "jobs ls",
                "show job list",
                botanMessage -> {
                    String str = "";
                    for (final Map.Entry<String, CronJob> a : hashMap.entrySet()) {
                        str += String.format("%s: \"%s\" %s\n", a.getKey(), a.getValue().schedule, a.getValue().message);
                    }
                    botanMessage.reply(str);
                }
        );

        robot.respond(
                "job rm (?<id>\\.+)",
                "show job list",
                botanMessage -> {
                        final String idStr = botanMessage.getMatcher().group("id");
                        scheduler.deschedule(idStr);
                        hashMap.remove(idStr);
                        botanMessage.reply("job rm successful");
                }
        );


        // start cron4j scheduler.
        scheduler.start();

        robot.beforeShutdown(() -> {
            scheduler.stop();
            return true;
        });
    }
}

@@ -1962,12 +1962,15 @@
 %22job
-s 
+%5C%5Cs+
 ls
+$
 %22,%0A 
@@ -2059,23 +2059,52 @@
     
-String str = %22%22
+final StringBuilder sb = new StringBuilder()
 ;%0A  
@@ -2214,14 +2214,17 @@
    s
-tr += 
+b.append(
 Stri
@@ -2308,16 +2308,17 @@
 message)
+)
 ;%0A      
@@ -2325,32 +2325,89 @@
               %7D%0A
+                    final String result = sb.toString();%0A
                 
@@ -2429,19 +2429,22 @@
 e.reply(
-str
+result
 );%0A     
@@ -2515,23 +2515,28 @@
 %22job
- rm 
+%5C%5Cs+rm%5C%5Cs+
 (?%3Cid%3E
-%5C%5C
 .+)
+$
 %22,%0A 
@@ -2547,32 +2547,39 @@
            %22
-show job
+remove job from
  list%22,%0A    

fix Cron job remove