package commands.chat;

import commands.Command;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.json.JSONException;
import org.json.JSONObject;
import utils.MSGS;
import utils.STATICS;

import java.io.*;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;



public class Cat implements Command {
    public boolean called(String[] args, MessageReceivedEvent event) {
        return false;
    }

    Timer spamTimer = new Timer();

    public void action(String[] args, MessageReceivedEvent event) {

        switch (args.length > 0 ? args[0].toLowerCase() : "") {

            case "spam":

                int delay;
                try {
                    delay = Integer.parseInt(args[1]);
                    if (delay < 5) {
                        delay = 5;
                        event.getTextChannel().sendMessage(MSGS.error.setDescription("The minumum time period is 5 seconds because of spam protection. ;)").build()).queue();
                    }
                } catch (Exception e) {
                    delay = 10;
                }

                List<String> counter = new ArrayList<>();
                spamTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        counter.add("");
                        event.getTextChannel().sendMessage("Cat number: `[" + (counter.size() + 1) + "]`\n" + getCat()).queue();
                        if (counter.size() > 999) {
                            spamTimer.cancel();
                            spamTimer = new Timer();
                        }
                    }
                }, 0, delay * 1000);

                break;

            case "s":
            case "stop":

                spamTimer.cancel();
                spamTimer = new Timer();

                break;

            default:
                event.getTextChannel().sendMessage(getCat()).queue();
                event.getMessage().delete().queue();
                break;

        }

    }

    public void executed(boolean success, MessageReceivedEvent event) {

    }

    public String help() {
        return "USAGE:\n" +
                "**cat**  -  `Returns a cute cat picture`\n" +
                "**cat spam <period in secs>**  -  `Spams cat pictures automatically`\n" +
                "**cat stop**  -  `Stops the cat spam`";
    }

    @Override
    public String description() {
        return "=^..^=";
    }

    @Override
    public String commandType() {
        return STATICS.CMDTYPE.chatutils;
    }

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
        InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = new JSONObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public static String getCat() {

        JSONObject json = null;
        try {
            json = readJsonFromUrl("http://random.cat/meow");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String outputMessage = null;
        try {
            outputMessage = json.get("file").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return outputMessage;
    }
}

@@ -383,16 +383,66 @@
 erTask;%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A
 %0A%0A%0Apubli
@@ -1253,20 +1253,21 @@
     
-List%3CString%3E
+AtomicInteger
  cou
@@ -1282,18 +1282,20 @@
 ew A
-rrayList%3C%3E
+tomicInteger
 ();%0A
@@ -1453,14 +1453,19 @@
 ter.
-add(%22%22
+getAndAdd(1
 );%0A 
@@ -1541,25 +1541,24 @@
 : %60%5B%22 + 
-(
 counter.
 size() +
@@ -1553,18 +1553,12 @@
 ter.
-size() + 1
+get(
 ) + 
@@ -1625,12 +1625,11 @@
 ter.
-size
+get
 () %3E
@@ -3233,51 +3233,8 @@
 ));%0A
-            String jsonText = readAll(rd);%0A
     
@@ -3278,16 +3278,19 @@
 ect(
-jsonText
+readAll(rd)
 );%0A 
@@ -3406,24 +3406,89 @@
  getCat() %7B%0A
+        %0A        String outputMessage = null; //Might be redudant
 %0A        JSO
@@ -3499,32 +3499,52 @@
 ect json = null;
+ //Might be redudant
 %0A        try %7B%0A 
@@ -3608,206 +3608,8 @@
 %22);%0A
-        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        String outputMessage = null;%0A        try %7B%0A
     
@@ -3644,16 +3644,22 @@
 .get
+String
 (%22file%22)
 .toS
@@ -3654,27 +3654,16 @@
 (%22file%22)
-.toString()
 ;%0A      
@@ -3673,16 +3673,30 @@
  catch (
+IOException %7C 
 JSONExce

Update Cat.java