from ...tl.mtproto_request import MTProtoRequest


class Invoice(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    invoice#c30aa358 flags:# test:flags.0?true name_requested:flags.1?true phone_requested:flags.2?true email_requested:flags.3?true shipping_address_requested:flags.4?true flexible:flags.5?true currency:string prices:Vector<LabeledPrice> = Invoice"""

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

    def __init__(self, currency, prices, test=None, name_requested=None, phone_requested=None, email_requested=None, shipping_address_requested=None, flexible=None):
        """
        :param test: Telegram type: «true».
        :param name_requested: Telegram type: «true».
        :param phone_requested: Telegram type: «true».
        :param email_requested: Telegram type: «true».
        :param shipping_address_requested: Telegram type: «true».
        :param flexible: Telegram type: «true».
        :param currency: Telegram type: «string».
        :param prices: Telegram type: «LabeledPrice». Must be a list.

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

        self.test = test
        self.name_requested = name_requested
        self.phone_requested = phone_requested
        self.email_requested = email_requested
        self.shipping_address_requested = shipping_address_requested
        self.flexible = flexible
        self.currency = currency
        self.prices = prices

    def on_send(self, writer):
        writer.write_int(Invoice.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.test else 0
        flags |= (1 << 1) if self.name_requested else 0
        flags |= (1 << 2) if self.phone_requested else 0
        flags |= (1 << 3) if self.email_requested else 0
        flags |= (1 << 4) if self.shipping_address_requested else 0
        flags |= (1 << 5) if self.flexible else 0
        writer.write_int(flags)

        writer.tgwrite_string(self.currency)
        writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
        writer.write_int(len(self.prices))
        for prices_item in self.prices:
            prices_item.on_send(writer)

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

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

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

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

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

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

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

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

        self.currency = reader.tgread_string()
        reader.read_int()  # Vector's constructor ID
        self.prices = []  # Initialize an empty list
        prices_len = reader.read_int()
        for _ in range(prices_len):
            prices_item = reader.tgread_object()
            self.prices.append(prices_item)

    def __repr__(self):
        return 'invoice#c30aa358 flags:# test:flags.0?true name_requested:flags.1?true phone_requested:flags.2?true email_requested:flags.3?true shipping_address_requested:flags.4?true flexible:flags.5?true currency:string prices:Vector<LabeledPrice> = Invoice'

    def __str__(self):
        return '(invoice (ID: 0xc30aa358) = (test={}, name_requested={}, phone_requested={}, email_requested={}, shipping_address_requested={}, flexible={}, currency={}, prices={}))'.format(str(self.test), str(self.name_requested), str(self.phone_requested), str(self.email_requested), str(self.shipping_address_requested), str(self.flexible), str(self.currency), None if not self.prices else [str(_) for _ in self.prices])
