from telethon.tl.mtproto_request import MTProtoRequest


class Authorization(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    auth.authorization#cd050916 flags:None tmp_sessions:flags.0?int user:User = auth.Authorization"""

    # Telegram's constructor ID (and unique identifier) for this class
    constructor_id = 0xcd050916

    def __init__(self, user, tmp_sessions=None):
        """
        :param tmp_sessions: Telegram type: «int».
        :param user: Telegram type: «User».
        """
        super().__init__()

        self.tmp_sessions = tmp_sessions
        self.user = user

    def on_send(self, writer):
        writer.write_int(Authorization.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.tmp_sessions else 0
        writer.write_int(flags)

        if self.tmp_sessions:
            writer.write_int(self.tmp_sessions)

        self.user.on_send(writer)

    @staticmethod
    def empty():
        """Returns an "empty" instance (all attributes are None)"""
        return Authorization(None, None)

    def on_response(self, reader):
        flags = reader.read_int()

        if (flags & (1 << 0)) != 0:
            self.tmp_sessions = reader.read_int()

        self.user = reader.tgread_object()

    def __repr__(self):
        return 'auth.authorization#cd050916 flags:None tmp_sessions:flags.0?int user:User = auth.Authorization'

    def __str__(self):
        return '(auth.authorization (ID: 0xcd050916) = (tmp_sessions={}, user={}))'.format(str(self.tmp_sessions), str(self.user))
