from ...tl.mtproto_request import MTProtoRequest


class PaymentRequestedInfo(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    paymentRequestedInfo#909c3f94 flags:# name:flags.0?string phone:flags.1?string email:flags.2?string shipping_address:flags.3?PostAddress = PaymentRequestedInfo"""

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

    def __init__(self, name=None, phone=None, email=None, shipping_address=None):
        """
        :param name: Telegram type: «string».
        :param phone: Telegram type: «string».
        :param email: Telegram type: «string».
        :param shipping_address: Telegram type: «PostAddress».

        Constructor for PaymentRequestedInfo: Instance of PaymentRequestedInfo.
        """
        super().__init__()

        self.name = name
        self.phone = phone
        self.email = email
        self.shipping_address = shipping_address

    def on_send(self, writer):
        writer.write_int(PaymentRequestedInfo.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.name else 0
        flags |= (1 << 1) if self.phone else 0
        flags |= (1 << 2) if self.email else 0
        flags |= (1 << 3) if self.shipping_address else 0
        writer.write_int(flags)

        if self.name:
            writer.tgwrite_string(self.name)

        if self.phone:
            writer.tgwrite_string(self.phone)

        if self.email:
            writer.tgwrite_string(self.email)

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

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

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

        if (flags & (1 << 0)) != 0:
            self.name = reader.tgread_string()

        if (flags & (1 << 1)) != 0:
            self.phone = reader.tgread_string()

        if (flags & (1 << 2)) != 0:
            self.email = reader.tgread_string()

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

    def __repr__(self):
        return 'paymentRequestedInfo#909c3f94 flags:# name:flags.0?string phone:flags.1?string email:flags.2?string shipping_address:flags.3?PostAddress = PaymentRequestedInfo'

    def __str__(self):
        return '(paymentRequestedInfo (ID: 0x909c3f94) = (name={}, phone={}, email={}, shipping_address={}))'.format(str(self.name), str(self.phone), str(self.email), str(self.shipping_address))
