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


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

    @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()]
        priv = self.priv.in_private_channel(request.sender.char_id)
        if priv:
            if const_all:
                query += "and channel_id IN (1, 2, 3) "
            else:
                query += "and channel_id IN (1, 2) "
        else:
            query += "and channel_id = 3 "
        if min_level:
            query += "and p.level >= ? "
            params.append(min_level)
        if profession:
            query += "and p.profession = ? "
            params.append(self.util.get_profession(profession))
        if priv and not const_all:
            blob = self.online_display.format_by_channel_prof(query, params)
        else:
            blob = self.online_display.format_by_org(query, params)
        return self.online_display.format_blob(blob)
