package org.horrgs.twitterbot.commands;

import org.horrgs.twitterbot.HorrgsTwitter;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.TwitterException;

import java.util.HashMap;

/**
 * Created by horrg on 9/19/2015.
 */
public class CommandHandler implements SubCommand {
    /*private static CommandHandler instance = new CommandHandler();
    public static CommandHandler getInstance() { return instance; }*/
    public HashMap<String, SubCommand> commands;

    public CommandHandler() {
        commands = new HashMap<>();
        loadCommands();
    }

    public void loadCommands() {
        commands.put("binary", new Binary());
        commands.put("define", new Define());
        commands.put("flipcoin", new FlipCoin());
        commands.put("help", new Help());
        commands.put("news", new News());
        commands.put("thingstheysay", new ThingsTheySay());
        commands.put("weather", new Weather());
    }

    @Override
    public void onCommand(Status status, String[] args) {
        if(status.getUser().getScreenName().equals("Horrgs")) {
            if(status.getText().startsWith("%")) {
                String[] strings = status.getText().split(" ");
                args = strings;
                String cmd = args[0].replace("%", "");
                System.out.println(cmd);
                try {
                    if(!commands.containsKey(cmd)) {
                        System.out.println("KK");
                        StatusUpdate statusUpdate = new StatusUpdate("Command \"" + cmd + "\" doesn't exist.");
                        HorrgsTwitter.twitter.updateStatus(statusUpdate);
                        return;
                    } else {
                        System.out.println("RR");
                        try {
                            commands.get(cmd).onCommand(status, args);
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    }
                } catch (TwitterException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @Override
    public String getHelp() {
        return "tweetbot.user.performcommand";
    }

    @Override
    public String getPermission() {
        return "tweetbot.user.performcommand";
    }
}

@@ -78,16 +78,62 @@
 witter;%0A
+import org.horrgs.twitterbot.util.TweetTools;%0A
 import t
@@ -1455,58 +1455,8 @@
 ) %7B%0A
-                        System.out.println(%22KK%22);%0A
     
@@ -1726,31 +1726,82 @@
     
-System.out.println(%22RR%22
+try %7B%0A                            SubCommand subCommand = commands.get(cmd
 );%0A 
@@ -1815,35 +1815,118 @@
                 
+ 
-try
+   if(TweetTools.hasPermission(status.getUser().getId(), subCommand.getPermission()))
  %7B%0A             
@@ -1940,33 +1940,30 @@
         
-commands.get(cmd)
+    subCommand
 .onComma
@@ -1980,16 +1980,46 @@
  args);%0A
+                            %7D%0A
         

Implement hasPermission method for cmd auth