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="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))
