from ....tl.mtproto_request import MTProtoRequest


class PaymentReceipt(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    payments.paymentReceipt#500911e1 flags:# date:date bot_id:int invoice:Invoice provider_id:int info:flags.0?PaymentRequestedInfo shipping:flags.1?ShippingOption currency:string total_amount:long credentials_title:string users:Vector<User> = payments.PaymentReceipt"""

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

    def __init__(self, date, bot_id, invoice, provider_id, currency, total_amount, credentials_title, users, info=None, shipping=None):
        """
        :param date: Telegram type: «date».
        :param bot_id: Telegram type: «int».
        :param invoice: Telegram type: «Invoice».
        :param provider_id: Telegram type: «int».
        :param info: Telegram type: «PaymentRequestedInfo».
        :param shipping: Telegram type: «ShippingOption».
        :param currency: Telegram type: «string».
        :param total_amount: Telegram type: «long».
        :param credentials_title: Telegram type: «string».
        :param users: Telegram type: «User». Must be a list.

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

        self.date = date
        self.bot_id = bot_id
        self.invoice = invoice
        self.provider_id = provider_id
        self.info = info
        self.shipping = shipping
        self.currency = currency
        self.total_amount = total_amount
        self.credentials_title = credentials_title
        self.users = users

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

        writer.tgwrite_date(self.date)
        writer.write_int(self.bot_id)
        self.invoice.on_send(writer)
        writer.write_int(self.provider_id)
        if self.info:
            self.info.on_send(writer)

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

        writer.tgwrite_string(self.currency)
        writer.write_long(self.total_amount)
        writer.tgwrite_string(self.credentials_title)
        writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
        writer.write_int(len(self.users))
        for users_item in self.users:
            users_item.on_send(writer)

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

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

        self.date = reader.tgread_date()
        self.bot_id = reader.read_int()
        self.invoice = reader.tgread_object()
        self.provider_id = reader.read_int()
        if (flags & (1 << 0)) != 0:
            self.info = reader.tgread_object()

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

        self.currency = reader.tgread_string()
        self.total_amount = reader.read_long()
        self.credentials_title = reader.tgread_string()
        reader.read_int()  # Vector's constructor ID
        self.users = []  # Initialize an empty list
        users_len = reader.read_int()
        for _ in range(users_len):
            users_item = reader.tgread_object()
            self.users.append(users_item)

    def __repr__(self):
        return 'payments.paymentReceipt#500911e1 flags:# date:date bot_id:int invoice:Invoice provider_id:int info:flags.0?PaymentRequestedInfo shipping:flags.1?ShippingOption currency:string total_amount:long credentials_title:string users:Vector<User> = payments.PaymentReceipt'

    def __str__(self):
        return '(payments.paymentReceipt (ID: 0x500911e1) = (date={}, bot_id={}, invoice={}, provider_id={}, info={}, shipping={}, currency={}, total_amount={}, credentials_title={}, users={}))'.format(str(self.date), str(self.bot_id), str(self.invoice), str(self.provider_id), str(self.info), str(self.shipping), str(self.currency), str(self.total_amount), str(self.credentials_title), None if not self.users else [str(_) for _ in self.users])
