from ....tl.mtproto_request import MTProtoRequest


class PaymentForm(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    payments.paymentForm#3f56aea3 flags:# can_save_credentials:flags.2?true password_missing:flags.3?true bot_id:int invoice:Invoice provider_id:int url:string native_provider:flags.4?string native_params:flags.4?DataJSON saved_info:flags.0?PaymentRequestedInfo saved_credentials:flags.1?PaymentSavedCredentials users:Vector<User> = payments.PaymentForm"""

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

    def __init__(self, bot_id, invoice, provider_id, url, users, can_save_credentials=None, password_missing=None, native_provider=None, native_params=None, saved_info=None, saved_credentials=None):
        """
        :param can_save_credentials: Telegram type: «true».
        :param password_missing: Telegram type: «true».
        :param bot_id: Telegram type: «int».
        :param invoice: Telegram type: «Invoice».
        :param provider_id: Telegram type: «int».
        :param url: Telegram type: «string».
        :param native_provider: Telegram type: «string».
        :param native_params: Telegram type: «DataJSON».
        :param saved_info: Telegram type: «PaymentRequestedInfo».
        :param saved_credentials: Telegram type: «PaymentSavedCredentials».
        :param users: Telegram type: «User». Must be a list.

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

        self.can_save_credentials = can_save_credentials
        self.password_missing = password_missing
        self.bot_id = bot_id
        self.invoice = invoice
        self.provider_id = provider_id
        self.url = url
        self.native_provider = native_provider
        self.native_params = native_params
        self.saved_info = saved_info
        self.saved_credentials = saved_credentials
        self.users = users

    def on_send(self, writer):
        writer.write_int(PaymentForm.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 2) if self.can_save_credentials else 0
        flags |= (1 << 3) if self.password_missing else 0
        flags |= (1 << 4) if self.native_provider else 0
        flags |= (1 << 4) if self.native_params else 0
        flags |= (1 << 0) if self.saved_info else 0
        flags |= (1 << 1) if self.saved_credentials else 0
        writer.write_int(flags)

        writer.write_int(self.bot_id)
        self.invoice.on_send(writer)
        writer.write_int(self.provider_id)
        writer.tgwrite_string(self.url)
        if self.native_provider:
            writer.tgwrite_string(self.native_provider)

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

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

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

        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 PaymentForm(None, None, None, None, None, None, None, None, None, None, None)

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

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

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

        self.bot_id = reader.read_int()
        self.invoice = reader.tgread_object()
        self.provider_id = reader.read_int()
        self.url = reader.tgread_string()
        if (flags & (1 << 4)) != 0:
            self.native_provider = reader.tgread_string()

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

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

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

        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.paymentForm#3f56aea3 flags:# can_save_credentials:flags.2?true password_missing:flags.3?true bot_id:int invoice:Invoice provider_id:int url:string native_provider:flags.4?string native_params:flags.4?DataJSON saved_info:flags.0?PaymentRequestedInfo saved_credentials:flags.1?PaymentSavedCredentials users:Vector<User> = payments.PaymentForm'

    def __str__(self):
        return '(payments.paymentForm (ID: 0x3f56aea3) = (can_save_credentials={}, password_missing={}, bot_id={}, invoice={}, provider_id={}, url={}, native_provider={}, native_params={}, saved_info={}, saved_credentials={}, users={}))'.format(str(self.can_save_credentials), str(self.password_missing), str(self.bot_id), str(self.invoice), str(self.provider_id), str(self.url), str(self.native_provider), str(self.native_params), str(self.saved_info), str(self.saved_credentials), None if not self.users else [str(_) for _ in self.users])
