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



# pylint: disable=wrong-import-position
from AbstractCommand import AbstractCommand
class TACommandAnnounceAll(AbstractCommand):
    TRIGGER = "ta-announce-all"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
              "auto_complete_hint": "<text>",
             }
    CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PUBLIC+"Posts <text> into every teams' announcement channel where the poster is a TA. [TEAM_ADMIN]"


    def on_POST(self, request, data):
        posted_to = ""

        for team in self.bot.api.get_teams():
            user = self.bot.api.get_team_member(team["id"], data["user_id"], exc=False)

            if ("roles" in user and "team_admin" in user["roles"]) or self._require_system_admin(data, exc=False):
                channel = self.bot.api.get_channel_by_name(team["id"], "town-square")
                self.bot.api.create_post(channel["id"], data["text"])
                posted_to += "\n+ "+team["name"]

        if posted_to != "":
            request.respond_cmd_temp("## :white_check_mark: Success! :)\n#### Posted to:"+posted_to)
        else:
            request.respond_cmd_err("Did not post anything. Are you a Team-Admin anywhere?")
