import persistence

from persistence import Driver

_MWPOS_BASE_HMAC = 0x18122008


class UserHelper:
    def __init__(self, mbcontext):
        # type: (msgbus.MBEasyContext) -> None
        self.mbcontext = mbcontext
        self.conn = None  # type: persistence.Connection

    def __enter__(self):
        self.conn = Driver().open(self.mbcontext)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.conn:
            self.conn.close()
        return exc_val

    def get_all_users(self):
        # type: () -> dict
        return {x.get_entry(0): (x.get_entry(1), x.get_entry(2), x.get_entry(3), x.get_entry(4))
                for x in self.conn.select("""select UserId, LongName, Level, Status from Users""")}

    def get_all_logged_users(self):
        query = """select operatorid
                     from posctrl.UserSession
                    where closetime IS NULL"""

        select = self.conn.select(query)
        return {x.get_entry(0) for x in select}
