from ....tl.mtproto_request import MTProtoRequest


class SentCode(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    auth.sentCode#5e002502 flags:# phone_registered:flags.0?true type:auth.SentCodeType phone_code_hash:string next_type:flags.1?auth.CodeType timeout:flags.2?int = auth.SentCode"""

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

    def __init__(self, type, phone_code_hash, phone_registered=None, next_type=None, timeout=None):
        """
        :param phone_registered: Telegram type: «true».
        :param type: Telegram type: «auth.SentCodeType».
        :param phone_code_hash: Telegram type: «string».
        :param next_type: Telegram type: «auth.CodeType».
        :param timeout: Telegram type: «int».

        Constructor for auth.SentCode: Instance of SentCode.
        """
        super().__init__()

        self.phone_registered = phone_registered
        self.type = type
        self.phone_code_hash = phone_code_hash
        self.next_type = next_type
        self.timeout = timeout

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

        self.type.on_send(writer)
        writer.tgwrite_string(self.phone_code_hash)
        if self.next_type:
            self.next_type.on_send(writer)

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

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

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

        if (flags & (1 << 0)) != 0:
            self.phone_registered = True  # Arbitrary not-None value, no need to read since it is a flag

        self.type = reader.tgread_object()
        self.phone_code_hash = reader.tgread_string()
        if (flags & (1 << 1)) != 0:
            self.next_type = reader.tgread_object()

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

    def __repr__(self):
        return 'auth.sentCode#5e002502 flags:# phone_registered:flags.0?true type:auth.SentCodeType phone_code_hash:string next_type:flags.1?auth.CodeType timeout:flags.2?int = auth.SentCode'

    def __str__(self):
        return '(auth.sentCode (ID: 0x5e002502) = (phone_registered={}, type={}, phone_code_hash={}, next_type={}, timeout={}))'.format(str(self.phone_registered), str(self.type), str(self.phone_code_hash), str(self.next_type), str(self.timeout))
