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



from AbstractCommand import *
class CommandSpoiler(AbstractCommand):
    TRIGGER = "spoiler"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
              "auto_complete_hint": "<spoiler-text>",
             }
    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Creates an empty post with spoiler-text. Use /spoiler-adv for advanced features (add to existing message/replace/remove)"


    def on_POST(self, request, data):
        if len(data["text"]) > 7000:
            request.cmd_respond_text_temp("``/spoiler`` failed: Spoiler-text must be smaller than 7000 characters :(\nYour text is: "+str(len(data["text"]))+" characters long.")
            return

        att = [{
            "text": "This post contains spoilers.",
            "actions": [{
                "name": ":ghost: show me the spoilers :ghost:",
                "integration": {"url": self.URL+"/interactive", "context": {"action": "show_spoiler", "spoiler":data['text'].strip()}}
                }]
            }]

        request.cmd_respond_text_chan("", {"attachments":att})


    def on_POST_interactive(self, request, data):
        if data["context"]["action"] == "show_spoiler":
            request.respond(200, {"skip_slack_parsing":True, "ephemeral_text": data["context"]["spoiler"]})
