from ....tl.mtproto_request import MTProtoRequest


class SavedInfo(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    payments.savedInfo#fb8fe43c flags:# has_saved_credentials:flags.1?true saved_info:flags.0?PaymentRequestedInfo = payments.SavedInfo"""

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

    def __init__(self, has_saved_credentials=None, saved_info=None):
        """
        :param has_saved_credentials: Telegram type: «true».
        :param saved_info: Telegram type: «PaymentRequestedInfo».

        Constructor for payments.SavedInfo: Instance of SavedInfo.
        """
        super().__init__()

        self.has_saved_credentials = has_saved_credentials
        self.saved_info = saved_info

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

        if self.saved_info:
            self.saved_info.on_send(writer)

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

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

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

        if (flags & (1 << 0)) != 0:
            self.saved_info = reader.tgread_object()

    def __repr__(self):
        return 'payments.savedInfo#fb8fe43c flags:# has_saved_credentials:flags.1?true saved_info:flags.0?PaymentRequestedInfo = payments.SavedInfo'

    def __str__(self):
        return '(payments.savedInfo (ID: 0xfb8fe43c) = (has_saved_credentials={}, saved_info={}))'.format(str(self.has_saved_credentials), str(self.saved_info))
