from core.command_param_types import Const, Int, Any
from core.decorators import instance, command, event
from modules.standard.online.online_controller import OnlineController


@instance(name="online_controller", override=True)
class OrgOnlineController(OnlineController):

    @event(event_type="org_member_logon", description="Change online channel", is_hidden=True)
    def org_member_logon(self, _, event_data):
        self.awaiting_data.put([event_data, 'org', True])

    @event(event_type="org_member_logoff", description="Change online channel", is_hidden=True)
    def org_member_logoff(self, _, event_data):
        if self.bot.is_ready():
            self.awaiting_data.put([event_data, 'org', False])

    @command(command="logon", params=[Const("clear")], access_level="member",
             description="Clears your logon message")
    def logon_clear_cmd(self, request, message):
        self.account_service.set_logon(request.sender.char_id)
        return "Your logon message has been cleared."

    @command(command="logoff", params=[Const("clear")], access_level="member",
             description="Clears your logoff message")
    def logoff_clear_cmd(self, request, message):
        self.account_service.set_logoff(request.sender.char_id)
        return "Your logoff message has been cleared."

    @command(command="logon", params=[Any("message", is_optional=True)], access_level="member",
             description="Sets or shows your logon message")
    def logon_cmd(self, request, message):
        if message:
            self.account_service.set_logon(request.sender.char_id, message)
            return f"Your new logon message has been set: <grey>{message}</grey>"
        else:
            entry = self.account_service.get_entry(request.sender.char_id)
            if entry.logon:
                return f"Your current logon message is: <grey>{entry.logon}</grey>"
            return f"You do not have a logon message set."

    @command(command="logoff", params=[Any("message", is_optional=True)], access_level="member",
             description="Sets or shows your logoff message ")
    def logoff_cmd(self, request, message):
        if message:
            self.account_service.set_logoff(request.sender.char_id, message)
            return f"Your new logoff message has been set: <grey>{message}</grey>"
        else:
            entry = self.account_service.get_entry(request.sender.char_id)
            if entry.logon:
                return f"Your current logoff message is: <grey>{entry.logon}</grey>"
            return f"You do not have a logoff message set."

    @command(command="online", params=[Const('all', is_optional=True),
                                       Int("min_level", is_optional=True),
                                       Any("profession", is_optional=True)],
             description="shows online players",
             access_level="member")
    def online_all_cmd(self, request, const_all, min_level, profession):
        query = ""
        params = [self.bot.name, self.bot.get_char_id()]
        if const_all:
            query += "and channel_id IN (1, 2, 3) "
        else:
            query += "and channel_id IN (1, 2) "
        if min_level:
            query += "and p.level >= ? "
            params.append(min_level)
        if profession:
            query += "and p.profession = ? "
            params.append(self.util.get_profession(profession))
        blob = self.online_display.format_by_channel_main(query, params)
        request.reply(self.online_display.format_blob(blob))
