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

from inspect import cleandoc

import datetime
import dateutil.parser
import holidays

# pylint: disable=wrong-import-position
from AbstractCommand import AbstractCommand
from AbstractPublicWS import AbstractPublicWS
class CommandFSOpen(AbstractCommand,AbstractPublicWS):
    TRIGGER=NAME = "is-it-open"
    CONFIG = {"display_name": "somebot-command", "auto_complete": True,
              "auto_complete_hint": "",
             }
    USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Is fsinf open?"

    cmd_state = msg_state = "Wos waas i" # unknown

    
    def __init__(self, team_id, channel_id):
        super().__init__(team_id)
        self.channel_id = channel_id


    def on_POST(self, request, data):
        request.respond_cmd_temp("## Is FSInf open?\n\n"+self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].cmd_state)


    def on_public_POST(self, request, data):
        if "ubahn_warnung" in data:
            day=datetime.date.today()
            if "## :white_check_mark: FsInf opening - Kumm ume!" in self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].msg_state.strip() and not (day.weekday() == 5 or day.weekday() == 6 or holidays.AT().get(day)):
                self.bot.api.create_post(self.channel_id, data["ubahn_warnung"], props={"somecleaner_autodelete":"day"})
            return

        if data["cmd_state"] != self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].cmd_state or data["msg_state"] != self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].msg_state:
            if self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].msg_state != "Wos waas i":
                self.bot.api.create_post(self.channel_id, data["msg_state"], props={"somecleaner_autodelete":"day"})
                if "## :white_check_mark: FsInf opening - Kumm ume!" in data["msg_state"]:
                    try:
                        self.bot.api._post("/v4/commands/execute", data={"channel_id":"e7ed3sdhbbba5xi86uaqbc4f8o", "command":"/fsinf-control open"});
                    except:
                        pass
                else:
                    try:
                        self.bot.api._post("/v4/commands/execute", data={"channel_id":"e7ed3sdhbbba5xi86uaqbc4f8o", "command":"/fsinf-control close"});
                    except:
                        pass

            self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].cmd_state=data["cmd_state"]
            self.bot.modules[list(self.bot.modules.keys())[0]][self.TRIGGER].msg_state=data["msg_state"]
            request.respond_public(200, {"status":"changed"})
        else:
            request.respond_public(200, {"status":"unchanged"})

