# Mattermost Bot.
#  Copyright (c) 2016-2020 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
#  inspired by @bearza
#  published under MIT-License


from inspect import cleandoc

from AbstractCommand import AbstractCommand
class CoreCommandUse(AbstractCommand):
    TRIGGER = "use"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
              "auto_complete_hint": "[<feature>]",
             }
    CONFIG["auto_complete_desc"] = CONFIG["description"] = "Display a hint (how) to use a feature."
    USEINFO = cleandoc("""
        ``/use [<topic>]`` can be used to retrieve information features this instance offers.
        It can also be used to hint somebody, that they are not using a commonly used feature (correctly). :)
        """)

    TOPICS = {
        "latex": cleandoc("""
            Mattermost supports TeX math:
            ``````
            ```tex
            g_2(n) = n^2 \\cdot \\sqrt[3]{n^6 - 1}
            ```
            ``````

            ```tex
            g_2(n) = n^2 \\cdot \\sqrt[3]{n^6 - 1}
            ```
            """),
        "threads": cleandoc("""
            Mattermost supports threads. They make communication way more overseeable and structured.
            We all <3 to use threads. You too, even if you dont know it yet. :)
            You can open the thread-view with shift+up or by clicking the small arrow to the right on a post.
            If you see a repeated offender, consider using ``/use threads`` or ``/threads`` with its many parameters.
            """),
    }


    def on_register(self):
        self.bot.USETOPICS.update(self.TOPICS)
        self._create_slash_command()


    def on_POST(self, request, data):
        topic = data["text"].strip()

        if topic == "debug":
            import pprint
            pprint.pprint(self.bot.USETOPICS)
            request.cmd_respond_text_chan("Debug info dumped to stdout.")

        elif topic == "":
            msg = "``\n+ ``/use ".join([t for t in sorted(self.bot.USETOPICS.keys())])
            request.cmd_respond_text_chan("#### ``Did you know...``\nYou can use ``/use <topic>`` to display a helpful message about a feature of this instance. Known topics are:\n+ ``/use "+msg+"``")

        elif topic not in self.bot.USETOPICS:
            request.cmd_respond_text_chan("#### Did you know that this bot doesnt know anything about: ``"+topic+"``?\nIf this is important, contact @someone")

        else:
            request.cmd_respond_text_chan("#### ``Did you know...``\n"+self.bot.USETOPICS[topic]+"\n``/use "+topic+"``")
