# Mattermost APIv4 Bot.
# written 2019 by Ju <daju@fsinf.at>
# essential feedback by frunobulax

from random import randint
import re


from AbstractCommand import *
class CommandRollDice(AbstractCommand):
    TRIGGER = "roll"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
        "auto_complete_hint": "[n or ndm]",
    }
    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Roll dice n [n] or n dices m times [ndm]."


    def on_POST(self, request, data):
        if re.search("^([1-9]|[1-9][0-9]|100)$", data["text"]):
            msg = "Rolling a d" + data["text"] + ": " + str(randint(1, int(data["text"])))
        elif re.search("^([1-9])[d]([1-9]|[1-9][0-9]|100)$", data["text"]):
            msg = "Rolling a d" + re.findall("\d+", data["text"])[1] + " " + re.findall("\d+", data["text"])[0] + " times: "
            for x in range(int(re.findall("\d+", data["text"])[0])):
                msg = msg + str(randint(1, int(re.findall("\d+", data["text"])[1]))) + ", "
            msg = msg[:-2]
        else:
            request.cmd_respond_text_temp("Zulässige Parameter sind: 'n' oder 'ndm' wobei n [1-9] und m [1-100] Zahlen sind.")
            return

        request.cmd_respond_text_chan(msg)
